]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: make forward declaration in pure Go package an error
authorRuss Cox <rsc@golang.org>
Sat, 22 Dec 2012 21:46:46 +0000 (16:46 -0500)
committerRuss Cox <rsc@golang.org>
Sat, 22 Dec 2012 21:46:46 +0000 (16:46 -0500)
An error during the compilation can be more precise
than an error at link time.

For 'func init', the error happens always: you can't forward
declare an init func because the name gets mangled.

For other funcs, the error happens only with the special
(and never used by hand) -= flag, which tells 6g the
package is pure go.

The go command now passes -= for pure Go packages.

Fixes #3705.

R=ken2
CC=golang-dev
https://golang.org/cl/6996054

doc/progs/error.go
src/cmd/gc/go.h
src/cmd/gc/lex.c
src/cmd/gc/pgen.c
src/cmd/go/build.go

index f5073266525cafd1899232d02d7048a9fbe44e2a..57854c5fe5a805758df9ce8b9a7542642b40debe 100644 (file)
@@ -20,7 +20,11 @@ import (
 
 type File struct{}
 
-func Open(name string) (file *File, err error)
+func Open(name string) (file *File, err error) {
+       // OMIT
+       panic(1)
+       // STOP OMIT
+}
 
 func openFile() { // OMIT
        f, err := os.Open("filename.ext")
index 36bc4b2954744c7397ebc9b1798eca59a977ac40..071422367c5cdc3ba0b9e5d603d107dc5b938f95 100644 (file)
@@ -937,6 +937,7 @@ EXTERN      int     funcdepth;
 EXTERN int     typecheckok;
 EXTERN int     compiling_runtime;
 EXTERN int     compiling_wrappers;
+EXTERN int     pure_go;
 
 EXTERN int     nointerface;
 EXTERN int     fieldtrack_enabled;
index eabeaeb646971e7c7d2c2be2af83d65f3d934e76..6fd61d1e34a26810eba5caf233c9f3b1779f83c1 100644 (file)
@@ -293,8 +293,9 @@ main(int argc, char *argv[])
        if(argc < 1)
                usage();
 
-       // special flag to detect compilation of package runtime
-       compiling_runtime = debug['+'];
+       // special flags used during build.
+       compiling_runtime = debug['+']; // detect compilation of package runtime
+       pure_go = debug['=']; // package is completely go (no C or assembly)
 
        pathname = mal(1000);
        if(getwd(pathname, 999) == 0)
index 7be254fff173e2cab585e14164cf591afb706620..e388fe6a5eb4e2d27b6acaee4bf7466d40749bd6 100644 (file)
@@ -29,16 +29,19 @@ compile(Node *fn)
                throwreturn = sysfunc("throwreturn");
        }
 
-       if(fn->nbody == nil)
-               return;
+       lno = setlineno(fn);
+
+       if(fn->nbody == nil) {
+               if(pure_go || memcmp(fn->nname->sym->name, "init·", 6) == 0)
+                       yyerror("missing function body", fn);
+               goto ret;
+       }
 
        saveerrors();
 
        // set up domain for labels
        clearlabels();
 
-       lno = setlineno(fn);
-
        curfn = fn;
        dowidth(curfn->type);
 
index f2d81c02d1e81c71e75a425ec7a9dc5fff7d298d..73dd93fd250c80ce42f129bbe4b8678f087c403c 100644 (file)
@@ -1311,6 +1311,8 @@ func (gcToolchain) linker() string {
        return tool(archChar + "l")
 }
 
+var rsc = flag.Bool("rsc", false, "rsc")
+
 func (gcToolchain) gc(b *builder, p *Package, obj string, importArgs []string, gofiles []string) (ofile string, err error) {
        out := "_go_." + archChar
        ofile = obj + out
@@ -1321,6 +1323,21 @@ func (gcToolchain) gc(b *builder, p *Package, obj string, importArgs []string, g
                gcargs = append(gcargs, "-+")
        }
 
+       // If we're giving the compiler the entire package (no C etc files), tell it that,
+       // so that it can give good error messages about forward declarations.
+       // Exceptions: a few standard packages have forward declarations for
+       // pieces supplied behind-the-scenes by package runtime.
+       extFiles := len(p.CgoFiles) + len(p.CFiles) + len(p.SFiles) + len(p.SysoFiles) + len(p.SwigFiles) + len(p.SwigCXXFiles)
+       if p.Standard {
+               switch p.ImportPath {
+               case "os", "runtime/pprof", "sync", "time":
+                       extFiles++
+               }
+       }
+       if extFiles == 0 {
+               gcargs = append(gcargs, "-=")
+       }
+
        args := stringList(tool(archChar+"g"), "-o", ofile, buildGcflags, gcargs, "-D", p.localPrefix, importArgs)
        for _, f := range gofiles {
                args = append(args, mkAbs(p.Dir, f))