1
0
mirror of https://github.com/golang/go synced 2024-09-23 19:10:13 -06:00
go/test/interface/explicit.go
Dan Scales c8d6ee12d5 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-12 23:07:01 +00:00

107 lines
2.0 KiB
Go

// errorcheck
// Copyright 2009 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.
// Verify compiler messages about erroneous static interface conversions.
// Does not compile.
package main
type T struct {
a int
}
var t *T
type X int
func (x *X) M() {}
type I interface {
M()
}
var i I
type I2 interface {
M()
N()
}
var i2 I2
type E interface{}
var e E
func main() {
e = t // ok
t = e // ERROR "need explicit|need type assertion"
// neither of these can work,
// because i has an extra method
// that t does not, so i cannot contain a t.
i = t // ERROR "incompatible|missing M method"
t = i // ERROR "incompatible|assignment$"
i = i2 // ok
i2 = i // ERROR "incompatible|missing N method"
i = I(i2) // ok
i2 = I2(i) // ERROR "invalid|missing N method|cannot convert"
e = E(t) // ok
t = T(e) // ERROR "need explicit|need type assertion|incompatible|cannot convert"
// cannot type-assert non-interfaces
f := 2.0
_ = f.(int) // ERROR "non-interface type|only valid for interface types|not an interface"
}
type M interface {
M()
}
var m M
var _ = m.(int) // ERROR "impossible type assertion"
type Int int
func (Int) M(float64) {}
var _ = m.(Int) // ERROR "impossible type assertion"
var _ = m.(X) // ERROR "pointer receiver"
var ii int
var jj Int
var m1 M = ii // ERROR "incompatible|missing"
var m2 M = jj // ERROR "incompatible|wrong type for M method"
var m3 = M(ii) // ERROR "invalid|missing|cannot convert"
var m4 = M(jj) // ERROR "invalid|wrong type for M method|cannot convert"
type B1 interface {
_() // ERROR "methods must have a unique non-blank name"
}
type B2 interface {
M()
_() // ERROR "methods must have a unique non-blank name"
}
type T2 struct{}
func (t *T2) M() {}
func (t *T2) _() {}
// Already reported about the invalid blank interface method above;
// no need to report about not implementing it.
var b1 B1 = &T2{}
var b2 B2 = &T2{}