Commit 04098d88 authored by Russ Cox's avatar Russ Cox

cmd/gc: make forward declaration in pure Go package an error

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
parent 1b3244e0
......@@ -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")
......
......@@ -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;
......
......@@ -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)
......
......@@ -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);
......
......@@ -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))
......
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