Skip to content

Commit c1cc9f9

Browse files
committed
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 <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 21a04e3 commit c1cc9f9

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/cmd/compile/internal/typecheck/dcl.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,17 @@ func Export(n *ir.Name) {
106106
// Redeclared emits a diagnostic about symbol s being redeclared at pos.
107107
func Redeclared(pos src.XPos, s *types.Sym, where string) {
108108
if !s.Lastlineno.IsKnown() {
109-
pkgName := DotImportRefs[s.Def.(*ir.Ident)]
109+
var pkgName *ir.PkgName
110+
if s.Def == nil {
111+
for id, pkg := range DotImportRefs {
112+
if id.Sym().Name == s.Name {
113+
pkgName = pkg
114+
break
115+
}
116+
}
117+
} else {
118+
pkgName = DotImportRefs[s.Def.(*ir.Ident)]
119+
}
110120
base.ErrorfAt(pos, "%v redeclared %s\n"+
111121
"\t%v: previous declaration during import %q", s, where, base.FmtPos(pkgName.Pos()), pkgName.Pkg.Path)
112122
} else {

test/fixedbugs/issue47201.dir/a.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
. "fmt"
9+
)
10+
11+
func test() {
12+
Println("foo")
13+
}

test/fixedbugs/issue47201.dir/b.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
func Println() {} // ERROR "Println redeclared in this block"
8+
9+
func main() {}

test/fixedbugs/issue47201.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// errorcheckdir
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package ignored

0 commit comments

Comments
 (0)