]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: make redeclaration between import and func less confusing
authorRuss Cox <rsc@golang.org>
Wed, 2 Jan 2013 20:34:28 +0000 (15:34 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 2 Jan 2013 20:34:28 +0000 (15:34 -0500)
Fixes #4510.

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

src/cmd/gc/dcl.c
test/fixedbugs/issue4510.dir/f1.go [new file with mode: 0644]
test/fixedbugs/issue4510.dir/f2.go [new file with mode: 0644]
test/fixedbugs/issue4510.go [new file with mode: 0644]

index 7bc9ce988ea887e5c8716c80d7ac40ec6e1a5856..1c15e1eb6ef75e315ad330a6817a88675f8c2e4a 100644 (file)
@@ -151,16 +151,30 @@ void
 redeclare(Sym *s, char *where)
 {
        Strlit *pkgstr;
+       int line1, line2;
 
        if(s->lastlineno == 0) {
                pkgstr = s->origpkg ? s->origpkg->path : s->pkg->path;
                yyerror("%S redeclared %s\n"
                        "\tprevious declaration during import \"%Z\"",
                        s, where, pkgstr);
-       } else
-               yyerror("%S redeclared %s\n"
+       } else {
+               line1 = parserline();
+               line2 = s->lastlineno;
+               
+               // When an import and a declaration collide in separate files,
+               // present the import as the "redeclared", because the declaration
+               // is visible where the import is, but not vice versa.
+               // See issue 4510.
+               if(s->def == N) {
+                       line2 = line1;
+                       line1 = s->lastlineno;
+               }
+
+               yyerrorl(line1, "%S redeclared %s (%#N)\n"
                        "\tprevious declaration at %L",
-                       s, where, s->lastlineno);
+                       s, where, s->def, line2);
+       }
 }
 
 static int vargen;
diff --git a/test/fixedbugs/issue4510.dir/f1.go b/test/fixedbugs/issue4510.dir/f1.go
new file mode 100644 (file)
index 0000000..1e642e4
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2012 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 p
+
+import "fmt" // ERROR "fmt redeclared"
+
+var _ = fmt.Printf
diff --git a/test/fixedbugs/issue4510.dir/f2.go b/test/fixedbugs/issue4510.dir/f2.go
new file mode 100644 (file)
index 0000000..895fc34
--- /dev/null
@@ -0,0 +1,7 @@
+// Copyright 2012 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 p
+
+func fmt() {}
diff --git a/test/fixedbugs/issue4510.go b/test/fixedbugs/issue4510.go
new file mode 100644 (file)
index 0000000..003f9e8
--- /dev/null
@@ -0,0 +1,7 @@
+// errorcheckdir
+
+// Copyright 2012 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 ignored