mirror of
https://github.com/golang/go
synced 2025-05-31 15:40:52 +00:00
cmd/compile: improve error for wrong type in switch
Fixes #10561. Provides a better diagnostic message for failed type switch satisfaction in the case that a value receiver is being used in place of the pointer receiver that implements and satisfies the interface. Change-Id: If8c13ba13f2a8d81bf44bac7c3a66c12921ba921 Reviewed-on: https://go-review.googlesource.com/35235 Reviewed-by: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
committed by
Matthew Dempsky
parent
ba1a65fc51
commit
34b563f447
@ -162,8 +162,13 @@ func typecheckswitch(n *Node) {
|
||||
yyerror("impossible type switch case: %L cannot have dynamic type %v"+
|
||||
" (wrong type for %v method)\n\thave %v%S\n\twant %v%S", n.Left.Right, n1.Type, missing.Sym, have.Sym, have.Type, missing.Sym, missing.Type)
|
||||
} else if !missing.Broke {
|
||||
yyerror("impossible type switch case: %L cannot have dynamic type %v"+
|
||||
" (missing %v method)", n.Left.Right, n1.Type, missing.Sym)
|
||||
if ptr != 0 {
|
||||
yyerror("impossible type switch case: %L cannot have dynamic type %v"+
|
||||
" (%v method has pointer receiver)", n.Left.Right, n1.Type, missing.Sym)
|
||||
} else {
|
||||
yyerror("impossible type switch case: %L cannot have dynamic type %v"+
|
||||
" (missing %v method)", n.Left.Right, n1.Type, missing.Sym)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,3 +30,17 @@ func f1(e interface{}) {
|
||||
default: // ERROR "multiple defaults in switch"
|
||||
}
|
||||
}
|
||||
|
||||
type I interface {
|
||||
Foo()
|
||||
}
|
||||
|
||||
type X int
|
||||
|
||||
func (*X) Foo() {}
|
||||
func f2() {
|
||||
var i I
|
||||
switch i.(type) {
|
||||
case X: // ERROR "impossible type switch case: i \(type I\) cannot have dynamic type X \(Foo method has pointer receiver\)"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user