From ae9c822f78d5048aa4290b06a5a38f67aaf23dbe Mon Sep 17 00:00:00 2001 From: David Heuschmann Date: Sat, 15 Sep 2018 13:04:59 +0200 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/gc/typecheck.go | 9 +++++++-- test/fixedbugs/issue26616.go | 10 +++++----- test/fixedbugs/issue27595.go | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 test/fixedbugs/issue27595.go diff --git a/src/cmd/compile/internal/gc/typecheck.go b/src/cmd/compile/internal/gc/typecheck.go index 69dced00ac..24aba6bac4 100644 --- a/src/cmd/compile/internal/gc/typecheck.go +++ b/src/cmd/compile/internal/gc/typecheck.go @@ -3341,7 +3341,7 @@ func typecheckas(n *Node) { checkassign(n, n.Left) if n.Right != nil && n.Right.Type != nil { 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 // to indicate failed typechecking. n.Right.Type = nil @@ -3486,7 +3486,12 @@ func typecheckas2(n *Node) { } 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 out: diff --git a/test/fixedbugs/issue26616.go b/test/fixedbugs/issue26616.go index 46136dc68f..e5565b68ca 100644 --- a/test/fixedbugs/issue26616.go +++ b/test/fixedbugs/issue26616.go @@ -6,13 +6,13 @@ 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() { - var _ int = three() // ERROR "1 variable but 3 values" - var a int = three() // ERROR "1 variable but 3 values" - a = three() // ERROR "1 variable but 3 values" - b := 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 "assignment mismatch: 1 variable but three returns 3 values" + a = three() // ERROR "assignment mismatch: 1 variable but three returns 3 values" + b := three() // ERROR "assignment mismatch: 1 variable but three returns 3 values" _, _ = a, b } diff --git a/test/fixedbugs/issue27595.go b/test/fixedbugs/issue27595.go new file mode 100644 index 0000000000..af5c7a10d9 --- /dev/null +++ b/test/fixedbugs/issue27595.go @@ -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 +} -- 2.50.0