diff mbox

Go patch committed: Don't crash on case nil

Message ID mcr8vknh13e.fsf@dhcp-172-18-216-180.mtv.corp.google.com
State New
Headers show

Commit Message

Ian Lance Taylor Feb. 1, 2012, 12:03 a.m. UTC
I discovered that gccgo crashes on a test case like:

package p

type S struct {
	f interface{}
}

func F(p *S) bool {
	v := p.f
	switch a := v.(type) {
	case nil:
		_ = a
		return true
	}
	return true
}


The "case nil" in the type switch causes the find_methods traversal to
try to get the type of v, but that is invalid before lowering.

This patch fixes the problem.  I've added the test case to the master Go
testsuite.  Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu.  Committed to mainline.

Ian
diff mbox

Patch

diff -r a767ab0415d3 go/gogo.cc
--- a/go/gogo.cc	Tue Jan 31 12:47:10 2012 -0800
+++ b/go/gogo.cc	Tue Jan 31 15:59:40 2012 -0800
@@ -3848,6 +3848,24 @@ 
   b->set_end_location(s->location());
 }
 
+// Whether this variable has a type.
+
+bool
+Variable::has_type() const
+{
+  if (this->type_ == NULL)
+    return false;
+
+  // A variable created in a type switch case nil does not actually
+  // have a type yet.  It will be changed to use the initializer's
+  // type in determine_type.
+  if (this->is_type_switch_var_
+      && this->type_->is_nil_constant_as_type())
+    return false;
+
+  return true;
+}
+
 // In an assignment which sets a variable to a tuple of EXPR, return
 // the type of the first element of the tuple.
 
diff -r a767ab0415d3 go/gogo.h
--- a/go/gogo.h	Tue Jan 31 12:47:10 2012 -0800
+++ b/go/gogo.h	Tue Jan 31 15:59:40 2012 -0800
@@ -1154,8 +1154,7 @@ 
 
   // Return whether the type is defined yet.
   bool
-  has_type() const
-  { return this->type_ != NULL; }
+  has_type() const;
 
   // Get the initial value.
   Expression*