Commit ae9c822f authored by David Heuschmann's avatar David Heuschmann Committed by Robert Griesemer

cmd/compile: use more specific error message for assignment mismatch

Show a more specifc error message in the form of "%d variables but %v
returns %d values" if an assignment mismatch occurs with a function
or method call on the right.

Fixes #27595

Change-Id: Ibc97d070662b08f150ac22d686059cf224e012ab
Reviewed-on: https://go-review.googlesource.com/135575
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 10aeb672
...@@ -3341,7 +3341,7 @@ func typecheckas(n *Node) { ...@@ -3341,7 +3341,7 @@ func typecheckas(n *Node) {
checkassign(n, n.Left) checkassign(n, n.Left)
if n.Right != nil && n.Right.Type != nil { if n.Right != nil && n.Right.Type != nil {
if n.Right.Type.IsFuncArgStruct() { if n.Right.Type.IsFuncArgStruct() {
yyerror("assignment mismatch: 1 variable but %d values", n.Right.Type.NumFields()) yyerror("assignment mismatch: 1 variable but %v returns %d values", n.Right.Left, n.Right.Type.NumFields())
// Multi-value RHS isn't actually valid for OAS; nil out // Multi-value RHS isn't actually valid for OAS; nil out
// to indicate failed typechecking. // to indicate failed typechecking.
n.Right.Type = nil n.Right.Type = nil
...@@ -3486,7 +3486,12 @@ func typecheckas2(n *Node) { ...@@ -3486,7 +3486,12 @@ func typecheckas2(n *Node) {
} }
mismatch: mismatch:
yyerror("assignment mismatch: %d variables but %d values", cl, cr) switch r.Op {
default:
yyerror("assignment mismatch: %d variable but %d values", cl, cr)
case OCALLFUNC, OCALLMETH, OCALLINTER:
yyerror("assignment mismatch: %d variables but %v returns %d values", cl, r.Left, cr)
}
// second half of dance // second half of dance
out: out:
......
...@@ -6,13 +6,13 @@ ...@@ -6,13 +6,13 @@
package p package p
var x int = three() // ERROR "1 variable but 3 values" var x int = three() // ERROR "assignment mismatch: 1 variable but three returns 3 values"
func f() { func f() {
var _ int = three() // ERROR "1 variable but 3 values" var _ int = three() // ERROR "assignment mismatch: 1 variable but three returns 3 values"
var a int = three() // ERROR "1 variable but 3 values" var a int = three() // ERROR "assignment mismatch: 1 variable but three returns 3 values"
a = three() // ERROR "1 variable but 3 values" a = three() // ERROR "assignment mismatch: 1 variable but three returns 3 values"
b := three() // ERROR "1 variable but 3 values" b := three() // ERROR "assignment mismatch: 1 variable but three returns 3 values"
_, _ = a, b _, _ = a, b
} }
......
// errorcheck
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
var a = twoResults() // ERROR "assignment mismatch: 1 variable but twoResults returns 2 values"
var b, c, d = twoResults() // ERROR "assignment mismatch: 3 variables but twoResults returns 2 values"
var e, f = oneResult() // ERROR "assignment mismatch: 2 variables but oneResult returns 1 values"
func twoResults() (int, int) {
return 1, 2
}
func oneResult() int {
return 1
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment