Skip to content

Commit faeda66

Browse files
committed
go/types: better error for assignment count mismatches
This matches the error message of cmd/compile (for assignments). Change-Id: I42a428f5d72f034e7b7e97b090a929e317e812af Reviewed-on: https://go-review.googlesource.com/38315 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 3c7a812 commit faeda66

5 files changed

Lines changed: 24 additions & 24 deletions

File tree

src/go/types/assignments.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (check *Checker) initVars(lhs []*Var, rhs []ast.Expr, returnPos token.Pos)
219219
check.errorf(returnPos, "wrong number of return values (want %d, got %d)", l, r)
220220
return
221221
}
222-
check.errorf(rhs[0].Pos(), "assignment count mismatch (%d vs %d)", l, r)
222+
check.errorf(rhs[0].Pos(), "cannot initialize %d variables with %d values", l, r)
223223
return
224224
}
225225

@@ -253,7 +253,7 @@ func (check *Checker) assignVars(lhs, rhs []ast.Expr) {
253253
}
254254
if l != r {
255255
check.useGetter(get, r)
256-
check.errorf(rhs[0].Pos(), "assignment count mismatch (%d vs %d)", l, r)
256+
check.errorf(rhs[0].Pos(), "cannot assign %d values to %d variables", r, l)
257257
return
258258
}
259259

src/go/types/testdata/decls1.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ var (
7878
u2 = iface.([]int)
7979
u3 = iface.(a /* ERROR "not a type" */ )
8080
u4, ok = iface.(int)
81-
u5, ok2, ok3 = iface /* ERROR "assignment count mismatch" */ .(int)
81+
u5, ok2, ok3 = iface /* ERROR "cannot initialize" */ .(int)
8282
)
8383

8484
// Constant expression initializations

src/go/types/testdata/issues.src

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ func issue10979() {
9898
// issue11347
9999
// These should not crash.
100100
var a1, b1 /* ERROR cycle */ , c1 /* ERROR cycle */ b1 = 0 > 0<<""[""[c1]]>c1
101-
var a2, b2 /* ERROR cycle */ = 0 /* ERROR mismatch */ /* ERROR mismatch */ > 0<<""[b2]
102-
var a3, b3 /* ERROR cycle */ = int /* ERROR mismatch */ /* ERROR mismatch */ (1<<""[b3])
101+
var a2, b2 /* ERROR cycle */ = 0 /* ERROR cannot initialize */ /* ERROR cannot initialize */ > 0<<""[b2]
102+
var a3, b3 /* ERROR cycle */ = int /* ERROR cannot initialize */ /* ERROR cannot initialize */ (1<<""[b3])
103103

104104
// issue10260
105105
// Check that error messages explain reason for interface assignment failures.

src/go/types/testdata/stmt0.src

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ func assignments0() (int, int) {
1515
f3 := func() (int, int, int) { return 1, 2, 3 }
1616

1717
a, b, c = 1, 2, 3
18-
a, b, c = 1 /* ERROR "assignment count mismatch" */ , 2
19-
a, b, c = 1 /* ERROR "assignment count mismatch" */ , 2, 3, 4
18+
a, b, c = 1 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ , 2
19+
a, b, c = 1 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ , 2, 3, 4
2020
_, _, _ = a, b, c
2121

2222
a = f0 /* ERROR "used as value" */ ()
2323
a = f1()
24-
a = f2 /* ERROR "assignment count mismatch" */ ()
24+
a = f2 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ()
2525
a, b = f2()
26-
a, b, c = f2 /* ERROR "assignment count mismatch" */ ()
26+
a, b, c = f2 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ()
2727
a, b, c = f3()
28-
a, b = f3 /* ERROR "assignment count mismatch" */ ()
28+
a, b = f3 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ()
2929

30-
a, b, c = <- /* ERROR "assignment count mismatch" */ ch
30+
a, b, c = <- /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ch
3131

3232
return /* ERROR "wrong number of return values" */
3333
return /* ERROR "wrong number of return values" */ 1
@@ -43,7 +43,7 @@ func assignments1() {
4343
c = s /* ERROR "cannot use .* in assignment" */
4444
s = b /* ERROR "cannot use .* in assignment" */
4545

46-
v0, v1, v2 := 1 /* ERROR "mismatch" */ , 2, 3, 4
46+
v0, v1, v2 := 1 /* ERROR "cannot initialize" */ , 2, 3, 4
4747
_, _, _ = v0, v1, v2
4848

4949
b = true
@@ -108,20 +108,20 @@ func assignments2() {
108108
s, b = m["foo"]
109109
_, d = m["bar"]
110110
m["foo"] = nil
111-
m["foo"] = nil /* ERROR assignment count mismatch */ , false
111+
m["foo"] = nil /* ERROR cannot assign [1-9]+ values to [1-9]+ variables */ , false
112112
_ = append(m["foo"])
113113
_ = append(m["foo"], true)
114114

115115
var c chan int
116116
_, b = <-c
117117
_, d = <-c
118118
<- /* ERROR cannot assign */ c = 0
119-
<-c = 0 /* ERROR assignment count mismatch */ , false
119+
<-c = 0 /* ERROR cannot assign [1-9]+ values to [1-9]+ variables */ , false
120120

121121
var x interface{}
122122
_, b = x.(int)
123123
x /* ERROR cannot assign */ .(int) = 0
124-
x.(int) = 0 /* ERROR assignment count mismatch */ , false
124+
x.(int) = 0 /* ERROR cannot assign [1-9]+ values to [1-9]+ variables */ , false
125125

126126
assignments2 /* ERROR used as value */ () = nil
127127
int /* ERROR not an expression */ = 0

src/go/types/testdata/vardecl.src

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,39 @@ var _ = f /* ERROR "used as value" */ ()
2828
// Identifier and expression arity must match.
2929
var _, _ = 1, 2
3030
var _ = 1, 2 /* ERROR "extra init expr 2" */
31-
var _, _ = 1 /* ERROR "assignment count mismatch" */
31+
var _, _ = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
3232
var _, _, _ /* ERROR "missing init expr for _" */ = 1, 2
3333

3434
var _ = g /* ERROR "2-valued g" */ ()
3535
var _, _ = g()
36-
var _, _, _ = g /* ERROR "assignment count mismatch" */ ()
36+
var _, _, _ = g /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ()
3737

3838
var _ = m["foo"]
3939
var _, _ = m["foo"]
40-
var _, _, _ = m /* ERROR "assignment count mismatch" */ ["foo"]
40+
var _, _, _ = m /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ["foo"]
4141

4242
var _, _ int = 1, 2
4343
var _ int = 1, 2 /* ERROR "extra init expr 2" */
44-
var _, _ int = 1 /* ERROR "assignment count mismatch" */
44+
var _, _ int = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
4545
var _, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
4646

4747
var (
4848
_, _ = 1, 2
4949
_ = 1, 2 /* ERROR "extra init expr 2" */
50-
_, _ = 1 /* ERROR "assignment count mismatch" */
50+
_, _ = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
5151
_, _, _ /* ERROR "missing init expr for _" */ = 1, 2
5252

5353
_ = g /* ERROR "2-valued g" */ ()
5454
_, _ = g()
55-
_, _, _ = g /* ERROR "assignment count mismatch" */ ()
55+
_, _, _ = g /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ()
5656

5757
_ = m["foo"]
5858
_, _ = m["foo"]
59-
_, _, _ = m /* ERROR "assignment count mismatch" */ ["foo"]
59+
_, _, _ = m /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ["foo"]
6060

6161
_, _ int = 1, 2
6262
_ int = 1, 2 /* ERROR "extra init expr 2" */
63-
_, _ int = 1 /* ERROR "assignment count mismatch" */
63+
_, _ int = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
6464
_, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
6565
)
6666

@@ -155,7 +155,7 @@ func (r T) _(a, b, c int) (u, v, w int) {
155155
func _() {
156156
var a, b, c int
157157
var x, y int
158-
x, y = a /* ERROR assignment count mismatch */ , b, c
158+
x, y = a /* ERROR cannot assign [0-9]+ values to [0-9]+ variables */ , b, c
159159
_ = x
160160
_ = y
161161
}

0 commit comments

Comments
 (0)