]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: improve error for wrong type in switch
authorEmmanuel Odeke <emm.odeke@gmail.com>
Sat, 14 Jan 2017 12:23:23 +0000 (05:23 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 2 Feb 2017 17:36:43 +0000 (17:36 +0000)
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>

src/cmd/compile/internal/gc/swt.go
test/switch6.go

index 5c96361f98066efde0d9f77efda0090969454ba5..8f6ffa269053778b1e1ee31fce40ceacdbe3d9aa 100644 (file)
@@ -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)
+                                                       }
                                                }
                                        }
                                }
index 32392d8f73d439fd1936ba4decc901beaaf46e9f..9d102fef515d1cbfbe60ff8308b219e3ef479677 100644 (file)
@@ -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\)"
+       }
+}