aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDan Scales <danscales@google.com>2021-11-10 08:41:21 -0800
committerDan Scales <danscales@google.com>2021-11-12 23:07:01 +0000
commitc8d6ee12d5eb1c64f2aff8a4d511f677a64e3aed (patch)
tree226a36c5b123a6a745aec8578b3f421028e54ff8 /test
parent1cd600301ea2a0b13d5e158282200114dc9de3fd (diff)
downloadgo-c8d6ee12d5eb1c64f2aff8a4d511f677a64e3aed.tar.xz
cmd/compile: match Go 1.17 compiler error messages more closely
When being used by the compiler, fix up types2 error messages to be more like Go 1.17 compiler errors. In particular: - add information about which method is missing when a type is not assignable/convertible/etc. to an interface. - add information about any existing method which has the same name, but wrong type. - add extra hint in the case that the source or destination type is a pointer to an interface, rather than an interface. - add extra hint "need type assertion" in the case that the source is an interface that is implemented by the destination. - the following change in the CL stack also adds information about any existing method with a different name that only differs in case. Include much of the new logic in a new common function (*Checker).missingMethodReason(). types2 still adds a little more information in some cases then the Go 1.17 compiler. For example, it typically says "(value of type T)", rather than "(type T)", where "value" could also be "constant", "variable", etc. I kept the types2 error messages almost all the same when types2 is not used by the compiler. The only change (to reduce amount of compatibility code) was to change "M method" phrasing in one case to "method M" phrasing in one error message (which is the phrasing it uses in all other cases). That is the reason that there are a few small changes in types2/testdata/check/*.src. Added new test test/fixedbugs/issue48471.go to test that the added information is appearing correctly. Also adjusted the pattern matching in a bunch of other test/fixedbugs/*.go, now that types2 is producing error messages closer to Go 1.17. Was able to remove a couple test files from the types2 exception list in run.go. Updated #48471 Change-Id: I8af1eae6eb8a5541d8ea20b66f494e2e795e1956 Reviewed-on: https://go-review.googlesource.com/c/go/+/363436 Trust: Dan Scales <danscales@google.com> Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'test')
-rw-r--r--test/alias2.go10
-rw-r--r--test/append1.go2
-rw-r--r--test/ddd1.go2
-rw-r--r--test/fixedbugs/bug389.go2
-rw-r--r--test/fixedbugs/issue41247.go2
-rw-r--r--test/fixedbugs/issue48471.go41
-rw-r--r--test/fixedbugs/issue6572.go2
-rw-r--r--test/fixedbugs/issue9521.go4
-rw-r--r--test/interface/explicit.go2
-rw-r--r--test/run.go3
10 files changed, 54 insertions, 16 deletions
diff --git a/test/alias2.go b/test/alias2.go
index d7b5dccb68..61c7551f79 100644
--- a/test/alias2.go
+++ b/test/alias2.go
@@ -46,8 +46,8 @@ var _ A0 = T0{}
var _ T0 = A0{}
// But aliases and original types cannot be used with new types based on them.
-var _ N0 = T0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|incompatible type"
-var _ N0 = A0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|incompatible type"
+var _ N0 = T0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use T0{} \(value of type T0\) as type N0 in variable declaration"
+var _ N0 = A0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use A0{} \(value of type T0\) as type N0 in variable declaration"
var _ A5 = Value{}
@@ -82,10 +82,10 @@ func _() {
var _ A0 = T0{}
var _ T0 = A0{}
- var _ N0 = T0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|incompatible type"
- var _ N0 = A0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|incompatible type"
+ var _ N0 = T0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use T0{} \(value of type T0\) as type N0 in variable declaration"
+ var _ N0 = A0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use A0{} \(value of type T0\) as type N0 in variable declaration"
- var _ A5 = Value{} // ERROR "cannot use reflect\.Value{} \(type reflect.Value\) as type A5 in assignment|incompatible type"
+ var _ A5 = Value{} // ERROR "cannot use reflect\.Value{} \(type reflect.Value\) as type A5 in assignment|cannot use Value{} \(value of type reflect.Value\) as type A5 in variable declaration"
}
// Invalid type alias declarations.
diff --git a/test/append1.go b/test/append1.go
index 9dab120b25..397be570d9 100644
--- a/test/append1.go
+++ b/test/append1.go
@@ -17,6 +17,6 @@ func main() {
_ = append(s...) // ERROR "cannot use ... on first argument|not enough arguments in call to append"
_ = append(s, 2, s...) // ERROR "too many arguments to append|too many arguments in call to append"
- _ = append(s, make([]int, 0)) // ERROR "cannot use make.* as type int in append|cannot use make.* as int value"
+ _ = append(s, make([]int, 0)) // ERROR "cannot use make.* as type int in append|cannot use make.* \(value of type \[\]int\) as type int in argument to append"
_ = append(s, make([]int, -1)...) // ERROR "negative len argument in make|index -1.* must not be negative"
}
diff --git a/test/ddd1.go b/test/ddd1.go
index f7381b7c94..639b0bfdbd 100644
--- a/test/ddd1.go
+++ b/test/ddd1.go
@@ -19,7 +19,7 @@ var (
_ = sum(1.0, 2.0)
_ = sum(1.5) // ERROR "1\.5 .untyped float constant. as int|integer"
_ = sum("hello") // ERROR ".hello. (.untyped string constant. as int|.type untyped string. as type int)|incompatible"
- _ = sum([]int{1}) // ERROR "\[\]int{...}.*as type int|incompatible"
+ _ = sum([]int{1}) // ERROR "\[\]int{.*}.*as type int"
)
func sum3(int, int, int) int { return 0 }
diff --git a/test/fixedbugs/bug389.go b/test/fixedbugs/bug389.go
index 167e64e72c..209be8e6f7 100644
--- a/test/fixedbugs/bug389.go
+++ b/test/fixedbugs/bug389.go
@@ -9,4 +9,4 @@ package foo
func fn(a float32) {}
-var f func(arg int) = fn // ERROR "cannot use fn .type func.float32.. as type func.int. in assignment|different parameter types|incompatible type"
+var f func(arg int) = fn // ERROR "cannot use fn .type func.float32.. as type func.int. in assignment|different parameter types|cannot use fn .*type func.*float32.. as type func.*int. in variable declaration"
diff --git a/test/fixedbugs/issue41247.go b/test/fixedbugs/issue41247.go
index c5e495ba93..05889a9ce8 100644
--- a/test/fixedbugs/issue41247.go
+++ b/test/fixedbugs/issue41247.go
@@ -7,5 +7,5 @@
package p
func f() [2]int {
- return [...]int{2: 0} // ERROR "cannot use \[\.\.\.\]int{...} \(type \[3\]int\)|incompatible type"
+ return [...]int{2: 0} // ERROR "cannot use \[\.\.\.\]int{.*} \(.*type \[3\]int\)"
}
diff --git a/test/fixedbugs/issue48471.go b/test/fixedbugs/issue48471.go
new file mode 100644
index 0000000000..0412d23b99
--- /dev/null
+++ b/test/fixedbugs/issue48471.go
@@ -0,0 +1,41 @@
+// errorcheck
+
+// Copyright 2021 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
+
+type I interface{ M(int) }
+
+type T struct{}
+
+type T2 struct{}
+
+func (*T2) m(int)
+
+type T3 struct{}
+
+func (*T3) M(string) {}
+
+type T4 struct{}
+
+func (*T4) M(int)
+
+func f(I)
+
+func g() {
+ f(new(T)) // ERROR "cannot use new\(T\) \(.*type \*T\) as type I in argument to f:\n\t\*T does not implement I \(missing M method\)"
+ var i I
+ i = new(T) // ERROR "cannot use new\(T\) \(.*type \*T\) as type I in assignment:\n\t\*T does not implement I \(missing M method\)"
+ i = I(new(T)) // ERROR "cannot convert new\(T\) \(.*type \*T\) to type I:\n\t\*T does not implement I \(missing M method\)"
+ i = new(T2) // ERROR "cannot use new\(T2\) \(.*type \*T2\) as type I in assignment:\n\t\*T2 does not implement I \(missing M method\)"
+ i = new(T3) // ERROR "cannot use new\(T3\) \(.*type \*T3\) as type I in assignment:\n\t\*T3 does not implement I \(wrong type for M method\)\n\t\thave M\(string\)\n\t\twant M\(int\)"
+ i = T4{} // ERROR "cannot use T4\{\} \(.*type T4\) as type I in assignment:\n\tT4 does not implement I \(M method has pointer receiver\)"
+ i = new(I) // ERROR "cannot use new\(I\) \(.*type \*I\) as type I in assignment:\n\t\*I does not implement I \(\*I is pointer to interface, not interface\)"
+ _ = i.(*T2) // ERROR "impossible type assertion: i.\(\*T2\)\n\t\*T2 does not implement I \(missing M method\)"
+ _ = i.(*T3) // ERROR "impossible type assertion: i.\(\*T3\)\n\t\*T3 does not implement I \(wrong type for M method\)\n\t\thave M\(string\)\n\t\twant M\(int\)"
+ var t *T4
+ t = i // ERROR "cannot use i \(variable of type I\) as type \*T4 in assignment:\n\tneed type assertion"
+ _ = i
+}
diff --git a/test/fixedbugs/issue6572.go b/test/fixedbugs/issue6572.go
index 9f4d2de0e3..d69bf5aee2 100644
--- a/test/fixedbugs/issue6572.go
+++ b/test/fixedbugs/issue6572.go
@@ -17,6 +17,6 @@ func bar() (T, string, T) { // ERROR "undefined"
func main() {
var x, y, z int
x, y = foo()
- x, y, z = bar() // ERROR "cannot (use type|assign) string|incompatible type"
+ x, y, z = bar() // ERROR "cannot (use type|assign|use.*type) string|"
_, _, _ = x, y, z
}
diff --git a/test/fixedbugs/issue9521.go b/test/fixedbugs/issue9521.go
index 1ad40bdfda..a029ec145e 100644
--- a/test/fixedbugs/issue9521.go
+++ b/test/fixedbugs/issue9521.go
@@ -13,6 +13,6 @@ func f() (_, _ []int) { return }
func g() (x []int, y float64) { return }
func main() {
- _ = append(f()) // ERROR "cannot use \[\]int value as type int in append|incompatible type"
- _ = append(g()) // ERROR "cannot use float64 value as type int in append|incompatible type"
+ _ = append(f()) // ERROR "cannot use \[\]int value as type int in append|cannot use.*type \[\]int.*to append"
+ _ = append(g()) // ERROR "cannot use float64 value as type int in append|cannot use.*type float64.*to append"
}
diff --git a/test/interface/explicit.go b/test/interface/explicit.go
index f769f5878c..e18d6843ec 100644
--- a/test/interface/explicit.go
+++ b/test/interface/explicit.go
@@ -38,7 +38,7 @@ var e E
func main() {
e = t // ok
- t = e // ERROR "need explicit|need type assertion|incompatible type"
+ t = e // ERROR "need explicit|need type assertion"
// neither of these can work,
// because i has an extra method
diff --git a/test/run.go b/test/run.go
index 942fd032f2..ad64304ec8 100644
--- a/test/run.go
+++ b/test/run.go
@@ -2125,14 +2125,11 @@ var types2Failures = setOf(
"shift1.go", // issue #42989
"typecheck.go", // invalid function is not causing errors when called
- "interface/private.go", // types2 phrases errors differently (doesn't use non-spec "private" term)
-
"fixedbugs/bug176.go", // types2 reports all errors (pref: types2)
"fixedbugs/bug195.go", // types2 reports slightly different (but correct) bugs
"fixedbugs/bug228.go", // types2 doesn't run when there are syntax errors
"fixedbugs/bug231.go", // types2 bug? (same error reported twice)
"fixedbugs/bug255.go", // types2 reports extra errors
- "fixedbugs/bug374.go", // types2 reports extra errors
"fixedbugs/bug388.go", // types2 not run due to syntax errors
"fixedbugs/bug412.go", // types2 produces a follow-on error