]> Cypherpunks repositories - gostls13.git/commitdiff
gc: helpful message instead of internal error on method call on pointer to pointer.
authorLuuk van Dijk <lvd@golang.org>
Wed, 2 Nov 2011 16:18:53 +0000 (17:18 +0100)
committerLuuk van Dijk <lvd@golang.org>
Wed, 2 Nov 2011 16:18:53 +0000 (17:18 +0100)
Fixes #2343.

R=rsc
CC=golang-dev
https://golang.org/cl/5332048

src/cmd/gc/typecheck.c
test/fixedbugs/bug371.go [new file with mode: 0644]

index d2268e6641f54e41d8ebed50816a8171ab763c9a..6ae4384e0b8e0a2a5c08368bd6a99ce4da69876e 100644 (file)
@@ -1595,6 +1595,14 @@ looktypedot(Node *n, Type *t, int dostrcmp)
        return 1;
 }
 
+static Type*
+derefall(Type* t)
+{
+       while(t && t->etype == tptr)
+               t = t->type;
+       return t;
+}
+
 static int
 lookdot(Node *n, Type *t, int dostrcmp)
 {
@@ -1652,8 +1660,15 @@ lookdot(Node *n, Type *t, int dostrcmp)
                                n->left = nod(OIND, n->left, N);
                                n->left->implicit = 1;
                                typecheck(&n->left, Etype|Erv);
+                       } else if(tt->etype == tptr && tt->type->etype == tptr && eqtype(derefall(tt), rcvr)) {
+                               yyerror("calling method %N with receiver %lN requires explicit dereference", n->right, n->left);
+                               while(tt->etype == tptr) {
+                                       n->left = nod(OIND, n->left, N);
+                                       n->left->implicit = 1;
+                                       typecheck(&n->left, Etype|Erv);
+                                       tt = tt->type;
+                               }
                        } else {
-                               // method is attached to wrong type?
                                fatal("method mismatch: %T for %T", rcvr, tt);
                        }
                }
diff --git a/test/fixedbugs/bug371.go b/test/fixedbugs/bug371.go
new file mode 100644 (file)
index 0000000..bf993df
--- /dev/null
@@ -0,0 +1,24 @@
+// errchk $G $D/$F.go
+
+// Copyright 2011 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.
+
+// issue 2343
+
+package main
+
+type T struct {}
+
+func (t *T) pm() {}
+func (t T) m() {}
+
+func main() {
+       p := &T{}
+       p.pm()
+       p.m()
+
+       q := &p
+       q.m()  // ERROR "requires explicit dereference"
+       q.pm()
+}