Skip to content

Commit 1970e3e

Browse files
committed
go/types: restore original assignment error messages
This is the missing portion of the port of CL 351669 from types2 to go/types, now that we have a local flag to control for compiler error messages. Mostly a clean port but for adjustments to error reporting which requires error codes in go/types. Prerequisite for port of CL 364874. Change-Id: I5fc8c83003e4396351f42e9adb08f4ebc8a05653 Reviewed-on: https://go-review.googlesource.com/c/go/+/367195 Trust: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent f598e29 commit 1970e3e

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/go/types/assignments.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package types
88

99
import (
10+
"fmt"
1011
"go/ast"
1112
"go/token"
1213
)
@@ -237,6 +238,28 @@ func (check *Checker) assignVar(lhs ast.Expr, x *operand) Type {
237238
return x.typ
238239
}
239240

241+
func (check *Checker) assignError(rhs []ast.Expr, nvars, nvals int) {
242+
measure := func(x int, unit string) string {
243+
s := fmt.Sprintf("%d %s", x, unit)
244+
if x != 1 {
245+
s += "s"
246+
}
247+
return s
248+
}
249+
250+
vars := measure(nvars, "variable")
251+
vals := measure(nvals, "value")
252+
rhs0 := rhs[0]
253+
254+
if len(rhs) == 1 {
255+
if call, _ := unparen(rhs0).(*ast.CallExpr); call != nil {
256+
check.errorf(rhs0, _WrongAssignCount, "assignment mismatch: %s but %s returns %s", vars, call.Fun, vals)
257+
return
258+
}
259+
}
260+
check.errorf(rhs0, _WrongAssignCount, "assignment mismatch: %s but %s", vars, vals)
261+
}
262+
240263
// If returnPos is valid, initVars is called to type-check the assignment of
241264
// return expressions, and returnPos is the position of the return statement.
242265
func (check *Checker) initVars(lhs []*Var, origRHS []ast.Expr, returnPos token.Pos) {
@@ -260,7 +283,11 @@ func (check *Checker) initVars(lhs []*Var, origRHS []ast.Expr, returnPos token.P
260283
check.errorf(atPos(returnPos), _WrongResultCount, "wrong number of return values (want %d, got %d)", len(lhs), len(rhs))
261284
return
262285
}
263-
check.errorf(rhs[0], _WrongAssignCount, "cannot initialize %d variables with %d values", len(lhs), len(rhs))
286+
if compilerErrorMessages {
287+
check.assignError(origRHS, len(lhs), len(rhs))
288+
} else {
289+
check.errorf(rhs[0], _WrongAssignCount, "cannot initialize %d variables with %d values", len(lhs), len(rhs))
290+
}
264291
return
265292
}
266293

@@ -294,7 +321,11 @@ func (check *Checker) assignVars(lhs, origRHS []ast.Expr) {
294321
return
295322
}
296323
}
297-
check.errorf(rhs[0], _WrongAssignCount, "cannot assign %d values to %d variables", len(rhs), len(lhs))
324+
if compilerErrorMessages {
325+
check.assignError(origRHS, len(lhs), len(rhs))
326+
} else {
327+
check.errorf(rhs[0], _WrongAssignCount, "cannot assign %d values to %d variables", len(rhs), len(lhs))
328+
}
298329
return
299330
}
300331

src/go/types/expr.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,11 @@ func (check *Checker) singleValue(x *operand) {
16571657
// tuple types are never named - no need for underlying type below
16581658
if t, ok := x.typ.(*Tuple); ok {
16591659
assert(t.Len() != 1)
1660-
check.errorf(x, _TooManyValues, "%d-valued %s where single value is expected", t.Len(), x)
1660+
if compilerErrorMessages {
1661+
check.errorf(x, _TooManyValues, "multiple-value %s in single-value context", x)
1662+
} else {
1663+
check.errorf(x, _TooManyValues, "%d-valued %s where single value is expected", t.Len(), x)
1664+
}
16611665
x.mode = invalid
16621666
}
16631667
}

0 commit comments

Comments
 (0)