mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
10aeb672e0
commit
ae9c822f78
3 changed files with 31 additions and 7 deletions
|
|
@ -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) {
|
||||||
}
|
}
|
||||||
|
|
||||||
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 @@
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
test/fixedbugs/issue27595.go
Normal file
19
test/fixedbugs/issue27595.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
// 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
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue