diff mbox

Go patch committed: Check for value method on dereferenced value

Message ID mcrvco44ryy.fsf@dhcp-172-18-216-180.mtv.corp.google.com
State New
Headers show

Commit Message

Ian Lance Taylor Jan. 21, 2012, 10:21 p.m. UTC
In Go pointers are automatically dereferenced when calling value
methods.  However, the Go frontend was accidentally permitting a double
dereference when calling a method on a pointer value, which the language
does not permit.  This patch fixes the problem.  Bootstrapped and ran Go
testsuite on x86_64-unknown-linux-gnu.  Committed to mainline.

Ian
diff mbox

Patch

diff -r f2a8b369a473 go/types.cc
--- a/go/types.cc	Sat Jan 21 13:57:44 2012 -0800
+++ b/go/types.cc	Sat Jan 21 14:18:52 2012 -0800
@@ -8605,6 +8605,7 @@ 
 
   // If this is a pointer to a pointer, then it is possible that the
   // pointed-to type has methods.
+  bool dereferenced = false;
   if (nt == NULL
       && st == NULL
       && it == NULL
@@ -8617,6 +8618,7 @@ 
 	return Expression::make_error(location);
       nt = type->points_to()->named_type();
       st = type->points_to()->struct_type();
+      dereferenced = true;
     }
 
   bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
@@ -8656,6 +8658,12 @@ 
 	  else
 	    go_unreachable();
 	  go_assert(m != NULL);
+	  if (dereferenced && m->is_value_method())
+	    {
+	      error_at(location,
+		       "calling value method requires explicit dereference");
+	      return Expression::make_error(location);
+	    }
 	  if (!m->is_value_method() && expr->type()->points_to() == NULL)
 	    expr = Expression::make_unary(OPERATOR_AND, expr, location);
 	  ret = m->bind_method(expr, location);