Skip to content

cmd/compile: = and := errors are unnecessarily different #48558

@rsc

Description

@rsc
% cat x.go
package p

func f() (int, int, int)

func g() {
	x, y := f()
	var a, b int
	a, b = f()
	_ = x
	_ = y
	_ = a
	_ = b
}
% go tool compile x.go
x.go:6:10: cannot initialize 2 variables with 3 values
x.go:8:9: cannot assign 3 values to 2 variables
% go1.17 tool compile x.go
x.go:6:7: assignment mismatch: 2 variables but f returns 3 values
x.go:8:7: assignment mismatch: 2 variables but f returns 3 values
% 

In the old type checker, we worked to make sure that we always say the number on the left hand side of the assignment before the number on the right hand side. Originally the text was literally “2 = 3”, but that was a bit cryptic. Even so, is helps to avoid reversing the numbers entirely.

We should therefore reword the current x.go:8 message so that 2 appears before 3.

It would also help, I think, to restore the name of the function returning multiple values. That may not be clear on a complex line. The character positioning helps, of course, but it requires a tool to interpret. If you're looking at a list of errors, it helps more to see 'f' mentioned in every error that's about f.

Here's a real-world example that illustrates the usability difference better than the trivial case above:

% go test net/url
# net/url [net/url.test]
net/url/url.go:924:16: cannot assign 3 values to 2 variables
net/url/url.go:932:17: cannot initialize 2 variables with 3 values
net/url/url.go:1005:21: cannot assign 3 values to 2 variables
net/url/url_test.go:2068:16: cannot assign 3 values to 2 variables
FAIL	net/url [build failed]
FAIL
% go test -gcflags=-G=0 net/url
# net/url [net/url.test]
net/url/url.go:924:14: assignment mismatch: 2 variables but strings.Cut returns 3 values
net/url/url.go:932:14: assignment mismatch: 2 variables but strings.Cut returns 3 values
net/url/url.go:1005:19: assignment mismatch: 2 variables but strings.Cut returns 3 values
net/url/url_test.go:2068:14: assignment mismatch: 2 variables but strings.Cut returns 3 values
FAIL	net/url [build failed]
FAIL
% 

/cc @griesemer @findleyr

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions