diff mbox

Go patch committed: Only finalize embedded fields before finalizing methods

Message ID CAOyqgcW+cRmkphLeDzTuKjEo9_uxC9YBBiB3tEB=K52LPxOAYw@mail.gmail.com
State New
Headers show

Commit Message

Ian Lance Taylor Aug. 2, 2017, 4:27 p.m. UTC
In the Go frontend, when finalizing the methods of a named struct
type, we used to finalize all the field types first.  That can fail if
the field types refer indirectly to the named type.  Change it to just
finalize the embedded field types first, and the rest of the fields
later.  This fixes https://golang.org/issue/21253.  Bootstrapped and
ran Go testsuite on x86_64-pc-linux-gnu.  Committed to mainline and
GCC 7 branch.

Ian
diff mbox

Patch

Index: gcc/go/gofrontend/MERGE
===================================================================
--- gcc/go/gofrontend/MERGE	(revision 250686)
+++ gcc/go/gofrontend/MERGE	(working copy)
@@ -1,4 +1,4 @@ 
-f7c36b27a49131f60eedde260896d310d735d408
+c1ac6bc99f988633c6bc68a5ca9ffad3487750ef
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
Index: gcc/go/gofrontend/gogo.cc
===================================================================
--- gcc/go/gofrontend/gogo.cc	(revision 250548)
+++ gcc/go/gofrontend/gogo.cc	(working copy)
@@ -3058,26 +3058,53 @@  Finalize_methods::type(Type* t)
 
     case Type::TYPE_NAMED:
       {
-	// We have to finalize the methods of the real type first.
-	// But if the real type is a struct type, then we only want to
-	// finalize the methods of the field types, not of the struct
-	// type itself.  We don't want to add methods to the struct,
-	// since it has a name.
 	Named_type* nt = t->named_type();
 	Type* rt = nt->real_type();
 	if (rt->classification() != Type::TYPE_STRUCT)
 	  {
+	    // Finalize the methods of the real type first.
 	    if (Type::traverse(rt, this) == TRAVERSE_EXIT)
 	      return TRAVERSE_EXIT;
+
+	    // Finalize the methods of this type.
+	    nt->finalize_methods(this->gogo_);
 	  }
 	else
 	  {
+	    // We don't want to finalize the methods of a named struct
+	    // type, as the methods should be attached to the named
+	    // type, not the struct type.  We just want to finalize
+	    // the field types.
+	    //
+	    // It is possible that a field type refers indirectly to
+	    // this type, such as via a field with function type with
+	    // an argument or result whose type is this type.  To
+	    // avoid the cycle, first finalize the methods of any
+	    // embedded types, which are the only types we need to
+	    // know to finalize the methods of this type.
+	    const Struct_field_list* fields = rt->struct_type()->fields();
+	    if (fields != NULL)
+	      {
+		for (Struct_field_list::const_iterator pf = fields->begin();
+		     pf != fields->end();
+		     ++pf)
+		  {
+		    if (pf->is_anonymous())
+		      {
+			if (Type::traverse(pf->type(), this) == TRAVERSE_EXIT)
+			  return TRAVERSE_EXIT;
+		      }
+		  }
+	      }
+
+	    // Finalize the methods of this type.
+	    nt->finalize_methods(this->gogo_);
+
+	    // Finalize all the struct fields.
 	    if (rt->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT)
 	      return TRAVERSE_EXIT;
 	  }
 
-	nt->finalize_methods(this->gogo_);
-
 	// If this type is defined in a different package, then finalize the
 	// types of all the methods, since we won't see them otherwise.
 	if (nt->named_object()->package() != NULL && nt->has_any_methods())