0
1
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 .

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:
Emmanuel Odeke
2017-01-14 05:23:23 -07:00
committed by Matthew Dempsky
parent ba1a65fc51
commit 34b563f447
2 changed files with 21 additions and 2 deletions
src/cmd/compile/internal/gc
test

@ -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\)"
}
}