[dev.regabi] cmd/compile: use ir.Ident for imported identifiers

This CL substantially reworks how imported declarations are handled,
and fixes a number of issues with dot imports. In particular:

1. It eliminates the stub ir.Name declarations that are created
upfront during import-declaration processing, allowing this to be
deferred to when the declarations are actually needed. (Eventually,
this can be deferred even further so we never have to create ir.Names
w/ ONONAME, but this CL is already invasive/subtle enough.)

2. During noding, we now use ir.Idents to represent uses of imported
declarations, including of dot-imported declarations.

3. Unused dot imports are now reported after type checking, so that we
can correctly distinguish whether composite literal keys are a simple
identifier (struct literals) or expressions (array/slice/map literals)
and whether it might be a use of a dot-imported declaration.

4. It changes the "redeclared" error messages to report the previous
position information in the same style as other compiler error
messages that reference other source lines.

Passes buildall w/ toolstash -cmp.

Fixes #6428.
Fixes #43164.
Fixes #43167.
Updates #42990.

Change-Id: I40a0a780ec40daf5700fbc3cfeeb7300e1055981
Reviewed-on: https://go-review.googlesource.com/c/go/+/277713
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Matthew Dempsky 2020-12-13 10:35:20 -08:00
parent 305d93ef84
commit 4c2d66f642
17 changed files with 159 additions and 79 deletions

View file

@ -100,13 +100,26 @@ func autolabel(prefix string) *types.Sym {
return lookupN(prefix, int(n))
}
// find all the exported symbols in package opkg
// dotImports tracks all PkgNames that have been dot-imported.
var dotImports []*ir.PkgName
// dotImportRefs maps idents introduced by importDot back to the
// ir.PkgName they were dot-imported through.
var dotImportRefs map[*ir.Ident]*ir.PkgName
// find all the exported symbols in package referenced by PkgName,
// and make them available in the current package
func importdot(opkg *types.Pkg, pack *ir.PkgName) {
n := 0
func importDot(pack *ir.PkgName) {
if dotImportRefs == nil {
dotImportRefs = make(map[*ir.Ident]*ir.PkgName)
}
opkg := pack.Pkg
for _, s := range opkg.Syms {
if s.Def == nil {
continue
if _, ok := declImporter[s]; !ok {
continue
}
}
if !types.IsExported(s.Name) || strings.ContainsRune(s.Name, 0xb7) { // 0xb7 = center dot
continue
@ -118,21 +131,26 @@ func importdot(opkg *types.Pkg, pack *ir.PkgName) {
continue
}
s1.Def = s.Def
s1.Block = s.Block
if ir.AsNode(s1.Def).Name() == nil {
ir.Dump("s1def", ir.AsNode(s1.Def))
base.Fatalf("missing Name")
}
ir.AsNode(s1.Def).Name().PkgName = pack
s1.Origpkg = opkg
n++
id := ir.NewIdent(src.NoXPos, s)
dotImportRefs[id] = pack
s1.Def = id
s1.Block = 1
}
if n == 0 {
// can't possibly be used - there were no symbols
base.ErrorfAt(pack.Pos(), "imported and not used: %q", opkg.Path)
dotImports = append(dotImports, pack)
}
// checkDotImports reports errors for any unused dot imports.
func checkDotImports() {
for _, pack := range dotImports {
if !pack.Used {
base.ErrorfAt(pack.Pos(), "imported and not used: %q", pack.Pkg.Path)
}
}
// No longer needed; release memory.
dotImports = nil
dotImportRefs = nil
}
// nodAddr returns a node representing &n.