diff mbox

Go patch committed: Better error for offsetof(method value)

Message ID mcrsiw7brwi.fsf@iant-glaptop.roam.corp.google.com
State New
Headers show

Commit Message

Ian Lance Taylor Oct. 11, 2013, 6:10 p.m. UTC
This patch to the Go frontend gives a more useful error message for an
invalid call to unsafe.Offsetof on a method value (method values look
like field references, which are valid for unsafe.offsetof).
Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu.
Committed to mainline.  Will commit to 4.8 branch when it reopens.

Ian
diff mbox

Patch

diff -r 07ab2f26915d go/expressions.cc
--- a/go/expressions.cc	Fri Oct 11 10:44:51 2013 -0700
+++ b/go/expressions.cc	Fri Oct 11 11:05:45 2013 -0700
@@ -7253,6 +7253,15 @@ 
   if (this->code_ == BUILTIN_OFFSETOF)
     {
       Expression* arg = this->one_arg();
+
+      if (arg->bound_method_expression() != NULL
+	  || arg->interface_field_reference_expression() != NULL)
+	{
+	  this->report_error(_("invalid use of method value as argument "
+			       "of Offsetof"));
+	  return this;
+	}
+
       Field_reference_expression* farg = arg->field_reference_expression();
       while (farg != NULL)
 	{
@@ -7262,7 +7271,8 @@ 
 	  // it must not be reached through pointer indirections.
 	  if (farg->expr()->deref() != farg->expr())
 	    {
-	      this->report_error(_("argument of Offsetof implies indirection of an embedded field"));
+	      this->report_error(_("argument of Offsetof implies "
+				   "indirection of an embedded field"));
 	      return this;
 	    }
 	  // Go up until we reach the original base.
@@ -7672,6 +7682,8 @@ 
 bool
 Builtin_call_expression::do_is_constant() const
 {
+  if (this->is_error_expression())
+    return true;
   switch (this->code_)
     {
     case BUILTIN_LEN: