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

rsc opened this issue Dec 23, 2020 · 7 comments

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

rsc opened this issue Dec 23, 2020 · 7 comments


Copy link

@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
Copy link

@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
      |         ^


Copy link

@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.)


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

@gopherbot gopherbot commented Dec 24, 2020

Change mentions this issue: [dev.regabi] cmd/compile: cleanup assignment typechecking


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
Trust: Matthew Dempsky <>
Run-TryBot: Matthew Dempsky <>
TryBot-Result: Go Bot <>
Reviewed-by: Cuong Manh Le <>
Copy link

@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


Copy link

@robpike robpike commented Jan 31, 2021

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


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.


Copy link

@griesemer griesemer commented Feb 1, 2021

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


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

Successfully merging a pull request may close this issue.

None yet
7 participants