Patchwork Go patch committed: Don't crash if constant refers to itself

login
register
mail settings
Submitter Ian Taylor
Date Dec. 22, 2010, 6:27 a.m.
Message ID <mcrhbe6iaon.fsf@google.com>
Download mbox | patch
Permalink /patch/76364/
State New
Headers show

Comments

Ian Taylor - Dec. 22, 2010, 6:27 a.m.
This patch fixes another case in the Go frontend where a constant refers
to itself.  If we need the type of a constant while it is being lowered,
then we have an invalid recursive reference.  This patch detects that
case rather than crashing by trying to get the type of the initializer
which has not yet been lowered.  Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu.  Committed to mainline.

Ian

Patch

diff -r f22ca4fe9909 go/expressions.cc
--- a/go/expressions.cc	Tue Dec 21 22:17:16 2010 -0800
+++ b/go/expressions.cc	Tue Dec 21 22:24:46 2010 -0800
@@ -2528,7 +2528,9 @@ 
   if (this->type_ != NULL)
     return this->type_;
 
-  if (this->seen_)
+  Named_constant* nc = this->constant_->const_value();
+
+  if (this->seen_ || nc->lowering())
     {
       this->report_error(_("constant refers to itself"));
       this->type_ = Type::make_error_type();
@@ -2537,7 +2539,6 @@ 
 
   this->seen_ = true;
 
-  Named_constant* nc = this->constant_->const_value();
   Type* ret = nc->type();
 
   if (ret != NULL)
diff -r f22ca4fe9909 go/gogo.cc
--- a/go/gogo.cc	Tue Dec 21 22:17:16 2010 -0800
+++ b/go/gogo.cc	Tue Dec 21 22:24:46 2010 -0800
@@ -1163,8 +1163,8 @@ 
 {
   Named_constant* nc = no->const_value();
 
-  // We can recursively a constant if the initializer expression
-  // manages to refer to itself.
+  // Don't get into trouble if the constant's initializer expression
+  // refers to the constant itself.
   if (nc->lowering())
     return TRAVERSE_CONTINUE;
   nc->set_lowering();