diff mbox

Go patch committed: Don't deref unknown type when importing anon field

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

Commit Message

Ian Lance Taylor Oct. 17, 2013, 3:51 p.m. UTC
This patch fixes the Go frontend to not dereference an unknown type when
importing an anonymous field.  This fixes a bug in a recent patch I
committed.  I have a test case ready to commit to the master repository
after the Go 1.2 release is made.  This patch bootstrapped and ran Go
testsuite on x86_64-unknown-linux-gnu.  Committed to mainline and 4.8
branch.

Ian
diff mbox

Patch

diff -r 742104f0d4c7 go/types.cc
--- a/go/types.cc	Wed Oct 16 06:37:07 2013 -0700
+++ b/go/types.cc	Thu Oct 17 08:39:50 2013 -0700
@@ -5263,11 +5263,25 @@ 
 	  // that an embedded builtin type is accessible from another
 	  // package (we know that all the builtin types are not
 	  // exported).
-	  if (name.empty() && ftype->deref()->named_type() != NULL)
+	  // This is called during parsing, before anything is
+	  // lowered, so we have to be careful to avoid dereferencing
+	  // an unknown type name.
+	  if (name.empty())
 	    {
-	      const std::string fn(ftype->deref()->named_type()->name());
-	      if (fn[0] >= 'a' && fn[0] <= 'z')
-		name = '.' + imp->package()->pkgpath() + '.' + fn;
+	      Type *t = ftype;
+	      if (t->classification() == Type::TYPE_POINTER)
+		{
+		  // Very ugly.
+		  Pointer_type* ptype = static_cast<Pointer_type*>(t);
+		  t = ptype->points_to();
+		}
+	      std::string tname;
+	      if (t->forward_declaration_type() != NULL)
+		tname = t->forward_declaration_type()->name();
+	      else if (t->named_type() != NULL)
+		tname = t->named_type()->name();
+	      if (!tname.empty() && tname[0] >= 'a' && tname[0] <= 'z')
+		name = '.' + imp->package()->pkgpath() + '.' + tname;
 	    }
 
 	  Struct_field sf(Typed_identifier(name, ftype, imp->location()));