aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCuong Manh Le <cuong.manhle.vn@gmail.com>2021-07-15 00:56:44 +0700
committerCuong Manh Le <cuong.manhle.vn@gmail.com>2021-07-15 17:35:20 +0000
commitc1cc9f9c3d5ed789a080ef9f8dd9c11eca7e2026 (patch)
tree63966f1e0757fbbc640ed14b26bf683e508b7f42
parent21a04e33353316635b5f3351e807916f3bb1e844 (diff)
downloadgo-c1cc9f9c3d5ed789a080ef9f8dd9c11eca7e2026.tar.gz
go-c1cc9f9c3d5ed789a080ef9f8dd9c11eca7e2026.zip
cmd/compile: fix lookup package of redeclared dot import symbol
The compiler is relying on Sym.Def field to lookup symbol package in DotImportRefs map. But the Sym.Def field is clear whenever the compiler finish processing a file. If the dot import happen in file A, then the redeclaration happen in file B, then the symbol lookup in file B will see a nil Sym.Def, that cause the compiler crashes. To fix this, we can interate over DotImportRefs and check for matching symbol name and return the corresponding package. Though this operation can be slow, but it only happens in invalid program, when printing error message, so it's not worth to optimize it further. Fixes #47201 Change-Id: I4ca1cb0a8e7432b19cf71434592a4cbb58d54adf Reviewed-on: https://go-review.googlesource.com/c/go/+/334589 Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
-rw-r--r--src/cmd/compile/internal/typecheck/dcl.go12
-rw-r--r--test/fixedbugs/issue47201.dir/a.go13
-rw-r--r--test/fixedbugs/issue47201.dir/b.go9
-rw-r--r--test/fixedbugs/issue47201.go7
4 files changed, 40 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/typecheck/dcl.go b/src/cmd/compile/internal/typecheck/dcl.go
index f3058d8811..5b771e3c0b 100644
--- a/src/cmd/compile/internal/typecheck/dcl.go
+++ b/src/cmd/compile/internal/typecheck/dcl.go
@@ -106,7 +106,17 @@ func Export(n *ir.Name) {
// Redeclared emits a diagnostic about symbol s being redeclared at pos.
func Redeclared(pos src.XPos, s *types.Sym, where string) {
if !s.Lastlineno.IsKnown() {
- pkgName := DotImportRefs[s.Def.(*ir.Ident)]
+ var pkgName *ir.PkgName
+ if s.Def == nil {
+ for id, pkg := range DotImportRefs {
+ if id.Sym().Name == s.Name {
+ pkgName = pkg
+ break
+ }
+ }
+ } else {
+ pkgName = DotImportRefs[s.Def.(*ir.Ident)]
+ }
base.ErrorfAt(pos, "%v redeclared %s\n"+
"\t%v: previous declaration during import %q", s, where, base.FmtPos(pkgName.Pos()), pkgName.Pkg.Path)
} else {
diff --git a/test/fixedbugs/issue47201.dir/a.go b/test/fixedbugs/issue47201.dir/a.go
new file mode 100644
index 0000000000..54b7079092
--- /dev/null
+++ b/test/fixedbugs/issue47201.dir/a.go
@@ -0,0 +1,13 @@
+// Copyright 2021 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 (
+ . "fmt"
+)
+
+func test() {
+ Println("foo")
+}
diff --git a/test/fixedbugs/issue47201.dir/b.go b/test/fixedbugs/issue47201.dir/b.go
new file mode 100644
index 0000000000..5fd0635af2
--- /dev/null
+++ b/test/fixedbugs/issue47201.dir/b.go
@@ -0,0 +1,9 @@
+// Copyright 2021 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
+
+func Println() {} // ERROR "Println redeclared in this block"
+
+func main() {}
diff --git a/test/fixedbugs/issue47201.go b/test/fixedbugs/issue47201.go
new file mode 100644
index 0000000000..e3a470b419
--- /dev/null
+++ b/test/fixedbugs/issue47201.go
@@ -0,0 +1,7 @@
+// errorcheckdir
+
+// Copyright 2021 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