Patchwork Go patch committed: Don't crash on self-referential initializer

login
register
mail settings
Submitter Ian Taylor
Date Feb. 15, 2011, 7:34 p.m.
Message ID <mcrmxlx5bxv.fsf@google.com>
Download mbox | patch
Permalink /patch/83285/
State New
Headers show

Comments

Ian Taylor - Feb. 15, 2011, 7:34 p.m.
This patch to the Go frontend avoids a crash when a variable initializer
erroneously refers to the variable itself.  We already avoided many such
crashes, but not in cases where lowering the initializer changes the
initializer itself.  This patch fixes cases like that.  Bootstrapped and
ran Go testsuite on x86_64-unknown-linux-gnu.  Committed to mainline.

Ian

Patch

diff -r e33f8e5aa641 go/gogo.cc
--- a/go/gogo.cc	Tue Feb 15 11:05:24 2011 -0800
+++ b/go/gogo.cc	Tue Feb 15 11:30:28 2011 -0800
@@ -1139,7 +1139,8 @@ 
 {
  public:
   Lower_parse_tree(Gogo* gogo, Named_object* function)
-    : Traverse(traverse_constants
+    : Traverse(traverse_variables
+	       | traverse_constants
 	       | traverse_functions
 	       | traverse_statements
 	       | traverse_expressions),
@@ -1147,6 +1148,9 @@ 
   { }
 
   int
+  variable(Named_object*);
+
+  int
   constant(Named_object*, bool);
 
   int
@@ -1167,6 +1171,18 @@ 
   int iota_value_;
 };
 
+// Lower variables.  We handle variables specially to break loops in
+// which a variable initialization expression refers to itself.  The
+// loop breaking is in lower_init_expression.
+
+int
+Lower_parse_tree::variable(Named_object* no)
+{
+  if (no->is_variable())
+    no->var_value()->lower_init_expression(this->gogo_, this->function_);
+  return TRAVERSE_CONTINUE;
+}
+
 // Lower constants.  We handle constants specially so that we can set
 // the right value for the predeclared constant iota.  This works in
 // conjunction with the way we lower Const_expression objects.