1
0
mirror of https://github.com/golang/go synced 2024-11-05 16:26:11 -07:00
go/test/fixedbugs/issue48471.go

57 lines
2.2 KiB
Go
Raw Normal View History

// errorcheck
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>
2021-11-10 09:41:21 -07:00
// 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)
type T5 struct{}
func (T5) m(int) {}
type T6 struct{}
func (T6) m(int) string { return "" }
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>
2021-11-10 09:41:21 -07:00
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\)"
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>
2021-11-10 09:41:21 -07:00
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\)\n\t\thave m\(int\)\n\t\twant M\(int\)"
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 \(type \*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\)\n\t\thave m\(int\)\n\t\twant M\(int\)"
_ = 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\)"
_ = i.(T5) // ERROR ""impossible type assertion: i.\(T5\)\n\tT5 does not implement I \(missing M method\)\n\t\thave m\(int\)\n\t\twant M\(int\)"
_ = i.(T6) // ERROR "impossible type assertion: i.\(T6\)\n\tT6 does not implement I \(missing M method\)\n\t\thave m\(int\) string\n\t\twant M\(int\)"
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>
2021-11-10 09:41:21 -07:00
var t *T4
t = i // ERROR "cannot use i \(variable of type I\) as type \*T4 in assignment:\n\tneed type assertion"
_ = i
}