Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rsc opened this issue Sep 22, 2021 · 3 comments
Closed

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

rsc opened this issue Sep 22, 2021 · 3 comments

Comments

@rsc
Copy link
Contributor

rsc commented Sep 22, 2021

% 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

@rsc rsc added this to the Go1.18 milestone Sep 22, 2021
@griesemer griesemer self-assigned this Sep 22, 2021
@rsc
Copy link
Contributor Author

rsc commented Sep 22, 2021

Here's a nearby mini-bug:

net/http/client_test.go:434:23: cannot initialize 3 variables with 1 values

Note 'values' instead of 'value'. (This was a, b, c := f() for a single-result f.)

@gopherbot
Copy link

Change https://golang.org/cl/351669 mentions this issue: cmd/compile: restore (mostly) original assignment error messages

@gopherbot
Copy link

Change https://golang.org/cl/351669 mentions this issue: cmd/compile: restore original assignment error messages

@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants