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: incorrect typecheck error for multiple assignment #43348

Closed
rsc opened this issue Dec 23, 2020 · 7 comments
Closed

cmd/compile: incorrect typecheck error for multiple assignment #43348

rsc opened this issue Dec 23, 2020 · 7 comments
Assignees
Labels
Milestone

Comments

@rsc
Copy link
Contributor

@rsc rsc commented Dec 23, 2020

% cat /tmp/x.go
package p

func f() string

func _() {
	x := f(), 1, 2
}
% go tool compile /tmp/x.go
/tmp/x.go:6:4: assignment mismatch: 1 variables but f returns 3 values
% 

Narrator: f does not return 3 values.

Been this way back to Go 1.12 at least (I don't have earlier versions on my Mac laptop, and honestly I'm aamzed Go 1.12 still runs).

/cc @griesemer @mdempsky

@rsc rsc added the NeedsFix label Dec 23, 2020
@rsc rsc added this to the Go1.17 milestone Dec 23, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Dec 23, 2020

Go 1.11 is better:

foo.go:6:4: assignment mismatch: 1 variables but 3 values

gccgo says

foo.go:6:9: error: wrong number of initializations
    6 |         x := f(), 1, 2
      |         ^

Loading

@griesemer
Copy link
Contributor

@griesemer griesemer commented Dec 23, 2020

The compiler with types2 reports:

$ go tool compile -G main.go
main.go:6:2: x declared but not used
main.go:6:7: cannot initialize 1 variables with 3 values

I think this is about as good as the old error message.
(I agree that the error about x not being used doesn't need to be there. That's a separate issue.)

Loading

@griesemer griesemer self-assigned this Dec 23, 2020
@gopherbot
Copy link

@gopherbot gopherbot commented Dec 24, 2020

Change https://golang.org/cl/280112 mentions this issue: [dev.regabi] cmd/compile: cleanup assignment typechecking

Loading

gopherbot pushed a commit that referenced this issue Dec 25, 2020
The assignment type-checking code previously bounced around a lot
between the LHS and RHS sides of the assignment. But there's actually
a very simple, consistent pattern to how to type check assignments:

1. Check the RHS expression.

2. If the LHS expression is an identifier that was declared in this
statement and it doesn't have an explicit type, give it the RHS
expression's default type.

3. Check the LHS expression.

4. Try assigning the RHS expression to the LHS expression, adding
implicit conversions as needed.

This CL implements this algorithm, and refactors tcAssign and
tcAssignList to use a common implementation. It also fixes the error
messages to consistently say just "1 variable" or "1 value", rather
than occasionally "1 variables" or "1 values".

Fixes #43348.

Passes toolstash -cmp.

Change-Id: I749cb8d6ccbc7d22cd7cb0a381f58a39fc2696b5
Reviewed-on: https://go-review.googlesource.com/c/go/+/280112
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
@mdempsky
Copy link
Member

@mdempsky mdempsky commented Jan 18, 2021

This is fixed on dev.regabi:

$ go tool compile /tmp/x.go
/tmp/x.go:6:4: assignment mismatch: 1 variable but 3 values

Loading

@robpike
Copy link
Contributor

@robpike robpike commented Jan 31, 2021

@rsc I can report it goes at least go Go 1.4.

Loading

@tv42
Copy link

@tv42 tv42 commented Feb 1, 2021

This seems closed in error, if it's not fixed in master? Or has dev.regabi become a de facto master?

I just checked and 4436114 still has this bug.

Loading

@griesemer
Copy link
Contributor

@griesemer griesemer commented Feb 1, 2021

Per the comment above, this was fixed in dev.regabi. This will be fixed for Go 1.17.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants