Patchwork Go patch committed: Don't permit global variables named init

login
register
mail settings
Submitter Ian Taylor
Date June 26, 2013, 3:48 p.m.
Message ID <mcrli5wlvpl.fsf@iant-glaptop.roam.corp.google.com>
Download mbox | patch
Permalink /patch/254795/
State New
Headers show

Comments

Ian Taylor - June 26, 2013, 3:48 p.m.
The Go language does not permit global variables named init.  That name
is reserved for functions that run when the program starts.  This patch,
from Rémy Oudompheng, implements that restriction in gccgo.
Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu.
Committed to mainline and 4.8 branch.

Ian

Patch

diff -r f250c0138323 go/gogo.cc
--- a/go/gogo.cc	Tue Jun 25 09:27:11 2013 -0700
+++ b/go/gogo.cc	Wed Jun 26 08:46:11 2013 -0700
@@ -1278,6 +1278,14 @@ 
 		   n.c_str());
 	  inform(pf->second, "%qs imported here", n.c_str());
 	}
+
+      // No package scope identifier may be named "init".
+      if (!p->second->is_function()
+	  && Gogo::unpack_hidden_name(p->second->name()) == "init")
+	{
+	  error_at(p->second->location(),
+	           "cannot declare init - must be func");
+	}
     }
 }