From dbdb3359b5b7cf948d74ba0f95e992ee98933bc5 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 3 Mar 2023 10:43:02 -0800 Subject: [PATCH] go/types, types2: use "undefined type" rather than "" in have/want error messages In assignments and return statements, if we have the wrong number of LHS or return values, we report the pattern that we have and the pattern that we want. For untyped constants we use "number" (to be not overly specific). For unknown types (due to earlier errors), now use "unknown type" rather than the (cryptic) "". Fixes #58742. Change-Id: I69c84ee29fb64badb0121e26a96f003b381024aa Reviewed-on: https://go-review.googlesource.com/c/go/+/473255 Reviewed-by: Ian Lance Taylor Reviewed-by: Robert Griesemer Auto-Submit: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/types2/assignments.go | 2 +- src/go/types/assignments.go | 2 +- .../types/testdata/fixedbugs/issue58742.go | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 src/internal/types/testdata/fixedbugs/issue58742.go diff --git a/src/cmd/compile/internal/types2/assignments.go b/src/cmd/compile/internal/types2/assignments.go index 35fb3f5b14..dd814c2e83 100644 --- a/src/cmd/compile/internal/types2/assignments.go +++ b/src/cmd/compile/internal/types2/assignments.go @@ -266,7 +266,7 @@ func (check *Checker) typesSummary(list []Type, variadic bool) string { case t == nil: fallthrough // should not happen but be cautious case t == Typ[Invalid]: - s = "" + s = "unknown type" case isUntyped(t): if isNumeric(t) { // Do not imply a specific type requirement: diff --git a/src/go/types/assignments.go b/src/go/types/assignments.go index a7a247b99f..a3b28d0e22 100644 --- a/src/go/types/assignments.go +++ b/src/go/types/assignments.go @@ -260,7 +260,7 @@ func (check *Checker) typesSummary(list []Type, variadic bool) string { case t == nil: fallthrough // should not happen but be cautious case t == Typ[Invalid]: - s = "" + s = "unknown type" case isUntyped(t): if isNumeric(t) { // Do not imply a specific type requirement: diff --git a/src/internal/types/testdata/fixedbugs/issue58742.go b/src/internal/types/testdata/fixedbugs/issue58742.go new file mode 100644 index 0000000000..b649a49774 --- /dev/null +++ b/src/internal/types/testdata/fixedbugs/issue58742.go @@ -0,0 +1,18 @@ +// Copyright 2023 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 p + +func _() (int, UndefinedType /* ERROR "undefined: UndefinedType" */ , string) { + return 0 // ERROR "not enough return values\n\thave (number)\n\twant (int, unknown type, string)" +} + +func _() (int, UndefinedType /* ERROR "undefined: UndefinedType" */ ) { + return 0, 1, 2 // ERROR "too many return values\n\thave (number, number, number)\n\twant (int, unknown type)" +} + +// test case from issue +func _() UndefinedType /* ERROR "undefined: UndefinedType" */ { + return // ERROR "not enough return values\n\thave ()\n\twant (unknown type)" +}