From daffc2d2ef1b68eae0a7e79d015fe33339f19534 Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Wed, 2 Mar 2011 16:18:17 -0500 Subject: [PATCH] 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 --- src/cmd/gc/export.c | 12 +++++++++--- src/cmd/gc/init.c | 27 +++++++++++---------------- src/cmd/gc/subr.c | 2 +- test/init.go | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 test/init.go diff --git a/src/cmd/gc/export.c b/src/cmd/gc/export.c index 594509915e..09b963f271 100644 --- a/src/cmd/gc/export.c +++ b/src/cmd/gc/export.c @@ -51,6 +51,12 @@ exportname(char *s) return isupperrune(r); } +static int +initname(char *s) +{ + return strcmp(s, "init") == 0; +} + void autoexport(Node *n, int ctxt) { @@ -60,7 +66,7 @@ autoexport(Node *n, int ctxt) return; if(n->ntype && n->ntype->op == OTFUNC && n->ntype->left) // method return; - if(exportname(n->sym->name) || strcmp(n->sym->name, "init") == 0) + if(exportname(n->sym->name) || initname(n->sym->name)) exportsym(n); else packagesym(n); @@ -304,7 +310,7 @@ importsym(Sym *s, int op) // mark the symbol so it is not reexported if(s->def == N) { - if(exportname(s->name)) + if(exportname(s->name) || initname(s->name)) s->flags |= SymExport; else s->flags |= SymPackage; // package scope @@ -374,7 +380,7 @@ importvar(Sym *s, Type *t, int ctxt) { Node *n; - if(!exportname(s->name) && !mypackage(s)) + if(!exportname(s->name) && !initname(s->name) && !mypackage(s)) return; importsym(s, ONAME); diff --git a/src/cmd/gc/init.c b/src/cmd/gc/init.c index dc073443e0..af4eb03360 100644 --- a/src/cmd/gc/init.c +++ b/src/cmd/gc/init.c @@ -30,19 +30,19 @@ renameinit(Node *n) /* * hand-craft the following initialization code - * var initdone· uint8 (1) - * func Init·() (2) - * if initdone· != 0 { (3) - * if initdone· == 2 (4) + * var initdone· uint8 (1) + * func init() (2) + * if initdone· != 0 { (3) + * if initdone· == 2 (4) * return * throw(); (5) * } - * initdone. = 1; (6) + * initdone· = 1; (6) * // over all matching imported symbols - * .init·() (7) + * .init() (7) * { } (8) - * init·() // if any (9) - * initdone. = 2; (10) + * init·() // if any (9) + * initdone· = 2; (10) * return (11) * } */ @@ -79,7 +79,7 @@ anyinit(NodeList *n) // are there any imported init functions for(h=0; hlink) { - if(s->name[0] != 'I' || strncmp(s->name, "Init·", 6) != 0) + if(s->name[0] != 'i' || strcmp(s->name, "init") != 0) continue; if(s->def == N) continue; @@ -118,12 +118,7 @@ fninit(NodeList *n) // (2) maxarg = 0; - 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"); + snprint(namebuf, sizeof(namebuf), "init"); fn = nod(ODCLFUNC, N, N); initsym = lookup(namebuf); @@ -154,7 +149,7 @@ fninit(NodeList *n) // (7) for(h=0; hlink) { - if(s->name[0] != 'I' || strncmp(s->name, "Init·", 6) != 0) + if(s->name[0] != 'i' || strcmp(s->name, "init") != 0) continue; if(s->def == N) continue; diff --git a/src/cmd/gc/subr.c b/src/cmd/gc/subr.c index 94e6dc5d52..142e5ba41e 100644 --- a/src/cmd/gc/subr.c +++ b/src/cmd/gc/subr.c @@ -2268,7 +2268,7 @@ syslook(char *name, int copy) s = pkglookup(name, runtimepkg); if(s == S || s->def == N) - fatal("looksys: cant find runtime.%s", name); + fatal("syslook: can't find runtime.%s", name); if(!copy) return s->def; diff --git a/test/init.go b/test/init.go new file mode 100644 index 0000000000..b6c8c97063 --- /dev/null +++ b/test/init.go @@ -0,0 +1,18 @@ +// 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" +} -- 2.50.0