Skip to content

Commit 435652b

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: use "assignment mismatch: x variables but y values" error message
This matches current compiler behavior. For #55326. Change-Id: I7197cf4ce21e614291a1a2e1048dd78d0a232b64 Reviewed-on: https://go-review.googlesource.com/c/go/+/436175 Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Griesemer <[email protected]>
1 parent f1d281f commit 435652b

File tree

6 files changed

+26
-42
lines changed

6 files changed

+26
-42
lines changed

src/cmd/compile/internal/types2/assignments.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,7 @@ func (check *Checker) initVars(lhs []*Var, orig_rhs []syntax.Expr, returnStmt sy
353353
check.report(&err)
354354
return
355355
}
356-
if check.conf.CompilerErrorMessages {
357-
check.assignError(orig_rhs, len(lhs), len(rhs))
358-
} else {
359-
check.errorf(rhs[0], _WrongAssignCount, "cannot initialize %d variables with %d values", len(lhs), len(rhs))
360-
}
356+
check.assignError(orig_rhs, len(lhs), len(rhs))
361357
return
362358
}
363359

@@ -401,11 +397,7 @@ func (check *Checker) assignVars(lhs, orig_rhs []syntax.Expr) {
401397
return
402398
}
403399
}
404-
if check.conf.CompilerErrorMessages {
405-
check.assignError(orig_rhs, len(lhs), len(rhs))
406-
} else {
407-
check.errorf(rhs[0], _WrongAssignCount, "cannot assign %d values to %d variables", len(rhs), len(lhs))
408-
}
400+
check.assignError(orig_rhs, len(lhs), len(rhs))
409401
return
410402
}
411403

src/go/types/assignments.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,7 @@ func (check *Checker) initVars(lhs []*Var, origRHS []ast.Expr, returnStmt ast.St
346346
check.report(err)
347347
return
348348
}
349-
if compilerErrorMessages {
350-
check.assignError(origRHS, len(lhs), len(rhs))
351-
} else {
352-
check.errorf(rhs[0], _WrongAssignCount, "cannot initialize %d variables with %d values", len(lhs), len(rhs))
353-
}
349+
check.assignError(origRHS, len(lhs), len(rhs))
354350
return
355351
}
356352

@@ -384,11 +380,7 @@ func (check *Checker) assignVars(lhs, origRHS []ast.Expr) {
384380
return
385381
}
386382
}
387-
if compilerErrorMessages {
388-
check.assignError(origRHS, len(lhs), len(rhs))
389-
} else {
390-
check.errorf(rhs[0], _WrongAssignCount, "cannot assign %d values to %d variables", len(rhs), len(lhs))
391-
}
383+
check.assignError(origRHS, len(lhs), len(rhs))
392384
return
393385
}
394386

src/internal/types/testdata/check/decls1.go

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 "cannot initialize" */ .(int)
81+
u5, ok2, ok3 = iface /* ERROR "assignment mismatch" */ .(int)
8282
)
8383

8484
// Constant expression initializations

src/internal/types/testdata/check/issues0.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ func issue10979() {
105105
// issue11347
106106
// These should not crash.
107107
var a1, b1 /* ERROR cycle */ , c1 /* ERROR cycle */ b1 = 0 > 0<<""[""[c1]]>c1
108-
var a2, b2 /* ERROR cycle */ = 0 /* ERROR cannot initialize */ /* ERROR cannot initialize */ > 0<<""[b2]
109-
var a3, b3 /* ERROR cycle */ = int /* ERROR cannot initialize */ /* ERROR cannot initialize */ (1<<""[b3])
108+
var a2, b2 /* ERROR cycle */ = 0 /* ERROR assignment mismatch */ /* ERROR assignment mismatch */ > 0<<""[b2]
109+
var a3, b3 /* ERROR cycle */ = int /* ERROR assignment mismatch */ /* ERROR assignment mismatch */ (1<<""[b3])
110110

111111
// issue10260
112112
// Check that error messages explain reason for interface assignment failures.

src/internal/types/testdata/check/stmt0.go

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 "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
18+
a, b, c = 1 /* ERROR "assignment mismatch: 3 variables but 2 values" */ , 2
19+
a, b, c = 1 /* ERROR "assignment mismatch: 3 variables but 4 values" */ , 2, 3, 4
2020
_, _, _ = a, b, c
2121

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

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

3232
return /* ERROR "not enough return values\n\thave \(\)\n\twant \(int, int\)" */
3333
return 1 /* ERROR "not enough return values\n\thave \(number\)\n\twant \(int, int\)" */
@@ -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 "cannot initialize" */ , 2, 3, 4
46+
v0, v1, v2 := 1 /* ERROR "assignment mismatch" */ , 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 cannot assign [1-9]+ values to [1-9]+ variables */ , false
111+
m["foo"] = nil /* ERROR assignment mismatch: 1 variable but 2 values */ , 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 cannot assign [1-9]+ values to [1-9]+ variables */ , false
119+
<-c = 0 /* ERROR assignment mismatch: 1 variable but 2 values */ , false
120120

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

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

src/internal/types/testdata/check/vardecl.go

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

3131
var _ = g /* ERROR "multiple-value g" */ ()
3232
var _, _ = g()
33-
var _, _, _ = g /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ()
33+
var _, _, _ = g /* ERROR "assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?" */ ()
3434

3535
var _ = m["foo"]
3636
var _, _ = m["foo"]
37-
var _, _, _ = m /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ["foo"]
37+
var _, _, _ = m /* ERROR "assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?" */ ["foo"]
3838

3939
var _, _ int = 1, 2
4040
var _ int = 1, 2 /* ERROR "extra init expr 2" */
41-
var _, _ int = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
41+
var _, _ int = 1 /* ERROR "assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?" */
4242
var _, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
4343

4444
var (
4545
_, _ = 1, 2
4646
_ = 1, 2 /* ERROR "extra init expr 2" */
47-
_, _ = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
47+
_, _ = 1 /* ERROR "assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?" */
4848
_, _, _ /* ERROR "missing init expr for _" */ = 1, 2
4949

5050
_ = g /* ERROR "multiple-value g" */ ()
5151
_, _ = g()
52-
_, _, _ = g /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ()
52+
_, _, _ = g /* ERROR "assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?" */ ()
5353

5454
_ = m["foo"]
5555
_, _ = m["foo"]
56-
_, _, _ = m /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ["foo"]
56+
_, _, _ = m /* ERROR "assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?" */ ["foo"]
5757

5858
_, _ int = 1, 2
5959
_ int = 1, 2 /* ERROR "extra init expr 2" */
60-
_, _ int = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
60+
_, _ int = 1 /* ERROR "assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?" */
6161
_, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
6262
)
6363

@@ -171,7 +171,7 @@ func _() {
171171
func _() {
172172
var a, b, c int
173173
var x, y int
174-
x, y = a /* ERROR cannot assign [0-9]+ values to [0-9]+ variables */ , b, c
174+
x, y = a /* ERROR "assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?" */ , b, c
175175
_ = x
176176
_ = y
177177
}

0 commit comments

Comments
 (0)