Commit daffc2d2 authored by Gustavo Niemeyer's avatar Gustavo Niemeyer Committed by Russ Cox

gc: fix init of packages named main

This change removes the special case which existed
for handling the initalization of the main package,
so that other modules named 'main' get properly
initialized when imported.

Note that gotest of main packages will break in most
cases without this.

R=rsc
CC=golang-dev
https://golang.org/cl/4190050
parent ff1d89d6
...@@ -51,6 +51,12 @@ exportname(char *s) ...@@ -51,6 +51,12 @@ exportname(char *s)
return isupperrune(r); return isupperrune(r);
} }
static int
initname(char *s)
{
return strcmp(s, "init") == 0;
}
void void
autoexport(Node *n, int ctxt) autoexport(Node *n, int ctxt)
{ {
...@@ -60,7 +66,7 @@ autoexport(Node *n, int ctxt) ...@@ -60,7 +66,7 @@ autoexport(Node *n, int ctxt)
return; return;
if(n->ntype && n->ntype->op == OTFUNC && n->ntype->left) // method if(n->ntype && n->ntype->op == OTFUNC && n->ntype->left) // method
return; return;
if(exportname(n->sym->name) || strcmp(n->sym->name, "init") == 0) if(exportname(n->sym->name) || initname(n->sym->name))
exportsym(n); exportsym(n);
else else
packagesym(n); packagesym(n);
...@@ -304,7 +310,7 @@ importsym(Sym *s, int op) ...@@ -304,7 +310,7 @@ importsym(Sym *s, int op)
// mark the symbol so it is not reexported // mark the symbol so it is not reexported
if(s->def == N) { if(s->def == N) {
if(exportname(s->name)) if(exportname(s->name) || initname(s->name))
s->flags |= SymExport; s->flags |= SymExport;
else else
s->flags |= SymPackage; // package scope s->flags |= SymPackage; // package scope
...@@ -374,7 +380,7 @@ importvar(Sym *s, Type *t, int ctxt) ...@@ -374,7 +380,7 @@ importvar(Sym *s, Type *t, int ctxt)
{ {
Node *n; Node *n;
if(!exportname(s->name) && !mypackage(s)) if(!exportname(s->name) && !initname(s->name) && !mypackage(s))
return; return;
importsym(s, ONAME); importsym(s, ONAME);
......
...@@ -30,19 +30,19 @@ renameinit(Node *n) ...@@ -30,19 +30,19 @@ renameinit(Node *n)
/* /*
* hand-craft the following initialization code * hand-craft the following initialization code
* var initdone·<file> uint8 (1) * var initdone· uint8 (1)
* func Init·<file>() (2) * func init() (2)
* if initdone·<file> != 0 { (3) * if initdone· != 0 { (3)
* if initdone·<file> == 2 (4) * if initdone· == 2 (4)
* return * return
* throw(); (5) * throw(); (5)
* } * }
* initdone.<file> = 1; (6) * initdone· = 1; (6)
* // over all matching imported symbols * // over all matching imported symbols
* <pkg>.init·<file>() (7) * <pkg>.init() (7)
* { <init stmts> } (8) * { <init stmts> } (8)
* init·<file>() // if any (9) * init·<n>() // if any (9)
* initdone.<file> = 2; (10) * initdone· = 2; (10)
* return (11) * return (11)
* } * }
*/ */
...@@ -79,7 +79,7 @@ anyinit(NodeList *n) ...@@ -79,7 +79,7 @@ anyinit(NodeList *n)
// are there any imported init functions // are there any imported init functions
for(h=0; h<NHASH; h++) for(h=0; h<NHASH; h++)
for(s = hash[h]; s != S; s = s->link) { for(s = hash[h]; s != S; s = s->link) {
if(s->name[0] != 'I' || strncmp(s->name, "Init·", 6) != 0) if(s->name[0] != 'i' || strcmp(s->name, "init") != 0)
continue; continue;
if(s->def == N) if(s->def == N)
continue; continue;
...@@ -118,12 +118,7 @@ fninit(NodeList *n) ...@@ -118,12 +118,7 @@ fninit(NodeList *n)
// (2) // (2)
maxarg = 0; maxarg = 0;
snprint(namebuf, sizeof(namebuf), "Init·"); snprint(namebuf, sizeof(namebuf), "init");
// this is a botch since we need a known name to
// call the top level init function out of rt0
if(strcmp(localpkg->name, "main") == 0)
snprint(namebuf, sizeof(namebuf), "init");
fn = nod(ODCLFUNC, N, N); fn = nod(ODCLFUNC, N, N);
initsym = lookup(namebuf); initsym = lookup(namebuf);
...@@ -154,7 +149,7 @@ fninit(NodeList *n) ...@@ -154,7 +149,7 @@ fninit(NodeList *n)
// (7) // (7)
for(h=0; h<NHASH; h++) for(h=0; h<NHASH; h++)
for(s = hash[h]; s != S; s = s->link) { for(s = hash[h]; s != S; s = s->link) {
if(s->name[0] != 'I' || strncmp(s->name, "Init·", 6) != 0) if(s->name[0] != 'i' || strcmp(s->name, "init") != 0)
continue; continue;
if(s->def == N) if(s->def == N)
continue; continue;
......
...@@ -2268,7 +2268,7 @@ syslook(char *name, int copy) ...@@ -2268,7 +2268,7 @@ syslook(char *name, int copy)
s = pkglookup(name, runtimepkg); s = pkglookup(name, runtimepkg);
if(s == S || s->def == N) if(s == S || s->def == N)
fatal("looksys: cant find runtime.%s", name); fatal("syslook: can't find runtime.%s", name);
if(!copy) if(!copy)
return s->def; return s->def;
......
// errchk $G -e $D/$F.go
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "runtime"
func init() {
}
func main() {
init() // ERROR "undefined: init"
runtime.init() // ERROR "unexported.*runtime\.init"
var _ = init // ERROR "undefined: init"
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment