diff mbox

Go patch committed: Don't permit assigning nil to _

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

Commit Message

Ian Lance Taylor Sept. 25, 2013, 3:28 a.m. UTC
This patch from Chris Manghane corrects the Go frontend to not permit
assigning nil to the sink variable _.  Bootstrapped and ran Go testsuite
on x86_64-unknown-linux-gnu.  Committed to mainline and 4.8 branch.

Ian
diff mbox

Patch

diff -r 869985e4ef63 go/parse.cc
--- a/go/parse.cc	Thu Sep 19 10:30:42 2013 -0700
+++ b/go/parse.cc	Tue Sep 24 20:17:51 2013 -0700
@@ -1940,12 +1940,9 @@ 
 	{
 	  if (this->gogo_->in_global_scope())
 	    return this->create_dummy_global(type, init, location);
-	  else if (type == NULL)
-	    this->gogo_->add_statement(Statement::make_statement(init, true));
 	  else
 	    {
-	      // With both a type and an initializer, create a dummy
-	      // variable so that we will check whether the
+	      // Create a dummy variable so that we will check whether the
 	      // initializer can be assigned to the type.
 	      Variable* var = new Variable(type, init, false, false, false,
 					   location);
diff -r 869985e4ef63 go/statements.cc
--- a/go/statements.cc	Thu Sep 19 10:30:42 2013 -0700
+++ b/go/statements.cc	Tue Sep 24 20:17:51 2013 -0700
@@ -594,6 +594,15 @@ 
 
   Type* lhs_type = this->lhs_->type();
   Type* rhs_type = this->rhs_->type();
+
+  // Invalid assignment of nil to the blank identifier.
+  if (lhs_type->is_sink_type()
+      && rhs_type->is_nil_type())
+    {
+      this->report_error(_("use of untyped nil"));
+      return;
+    }
+
   std::string reason;
   bool ok;
   if (this->are_hidden_fields_ok_)
@@ -975,7 +984,10 @@ 
 
       if ((*plhs)->is_sink_expression())
 	{
-	  b->add_statement(Statement::make_statement(*prhs, true));
+          if ((*prhs)->type()->is_nil_type())
+            this->report_error(_("use of untyped nil"));
+          else
+            b->add_statement(Statement::make_statement(*prhs, true));
 	  continue;
 	}