1
0
mirror of https://github.com/golang/go synced 2024-11-26 07:47:57 -07:00

[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

@ -28,12 +28,9 @@ func testdclstack() {
// redeclare emits a diagnostic about symbol s being redeclared at pos. // redeclare emits a diagnostic about symbol s being redeclared at pos.
func redeclare(pos src.XPos, s *types.Sym, where string) { func redeclare(pos src.XPos, s *types.Sym, where string) {
if !s.Lastlineno.IsKnown() { if !s.Lastlineno.IsKnown() {
pkg := s.Origpkg pkgName := dotImportRefs[s.Def.(*ir.Ident)]
if pkg == nil {
pkg = s.Pkg
}
base.ErrorfAt(pos, "%v redeclared %s\n"+ base.ErrorfAt(pos, "%v redeclared %s\n"+
"\tprevious declaration during import %q", s, where, pkg.Path) "\t%v: previous declaration during import %q", s, where, base.FmtPos(pkgName.Pos()), pkgName.Pkg.Path)
} else { } else {
prevPos := s.Lastlineno prevPos := s.Lastlineno
@ -46,7 +43,7 @@ func redeclare(pos src.XPos, s *types.Sym, where string) {
} }
base.ErrorfAt(pos, "%v redeclared %s\n"+ base.ErrorfAt(pos, "%v redeclared %s\n"+
"\tprevious declaration at %v", s, where, base.FmtPos(prevPos)) "\t%v: previous declaration", s, where, base.FmtPos(prevPos))
} }
} }
@ -210,6 +207,10 @@ func symfield(s *types.Sym, typ *types.Type) *ir.Field {
// Automatically creates a new closure variable if the referenced symbol was // Automatically creates a new closure variable if the referenced symbol was
// declared in a different (containing) function. // declared in a different (containing) function.
func oldname(s *types.Sym) ir.Node { func oldname(s *types.Sym) ir.Node {
if s.Pkg != types.LocalPkg {
return ir.NewIdent(base.Pos, s)
}
n := ir.AsNode(s.Def) n := ir.AsNode(s.Def)
if n == nil { if n == nil {
// Maybe a top-level declaration will come along later to // Maybe a top-level declaration will come along later to

View File

@ -165,17 +165,9 @@ func iimport(pkg *types.Pkg, in *bio.Reader) (fingerprint goobj.FingerprintType)
s := pkg.Lookup(p.stringAt(ird.uint64())) s := pkg.Lookup(p.stringAt(ird.uint64()))
off := ird.uint64() off := ird.uint64()
if _, ok := declImporter[s]; ok { if _, ok := declImporter[s]; !ok {
continue declImporter[s] = iimporterAndOffset{p, off}
} }
declImporter[s] = iimporterAndOffset{p, off}
// Create stub declaration. If used, this will
// be overwritten by expandDecl.
if s.Def != nil {
base.Fatalf("unexpected definition for %v: %v", s, ir.AsNode(s.Def))
}
s.Def = ir.NewDeclNameAt(src.NoXPos, s)
} }
} }
@ -187,10 +179,9 @@ func iimport(pkg *types.Pkg, in *bio.Reader) (fingerprint goobj.FingerprintType)
s := pkg.Lookup(p.stringAt(ird.uint64())) s := pkg.Lookup(p.stringAt(ird.uint64()))
off := ird.uint64() off := ird.uint64()
if _, ok := inlineImporter[s]; ok { if _, ok := inlineImporter[s]; !ok {
continue inlineImporter[s] = iimporterAndOffset{p, off}
} }
inlineImporter[s] = iimporterAndOffset{p, off}
} }
} }
@ -442,10 +433,16 @@ func (r *importReader) ident() *types.Sym {
return pkg.Lookup(name) return pkg.Lookup(name)
} }
func (r *importReader) qualifiedIdent() *types.Sym { func (r *importReader) qualifiedIdent() *ir.Name {
name := r.string() name := r.string()
pkg := r.pkg() pkg := r.pkg()
return pkg.Lookup(name) sym := pkg.Lookup(name)
n := sym.PkgDef()
if n == nil {
n = ir.NewDeclNameAt(src.NoXPos, sym)
sym.SetPkgDef(n)
}
return n.(*ir.Name)
} }
func (r *importReader) pos() src.XPos { func (r *importReader) pos() src.XPos {
@ -501,9 +498,9 @@ func (r *importReader) typ1() *types.Type {
// support inlining functions with local defined // support inlining functions with local defined
// types. Therefore, this must be a package-scope // types. Therefore, this must be a package-scope
// type. // type.
n := ir.AsNode(r.qualifiedIdent().PkgDef()) n := r.qualifiedIdent()
if n.Op() == ir.ONONAME { if n.Op() == ir.ONONAME {
expandDecl(n.(*ir.Name)) expandDecl(n)
} }
if n.Op() != ir.OTYPE { if n.Op() != ir.OTYPE {
base.Fatalf("expected OTYPE, got %v: %v, %v", n.Op(), n.Sym(), n) base.Fatalf("expected OTYPE, got %v: %v, %v", n.Op(), n.Sym(), n)
@ -821,10 +818,10 @@ func (r *importReader) node() ir.Node {
return n return n
case ir.ONONAME: case ir.ONONAME:
return mkname(r.qualifiedIdent()) return r.qualifiedIdent()
case ir.ONAME: case ir.ONAME:
return mkname(r.ident()) return r.ident().Def.(*ir.Name)
// case OPACK, ONONAME: // case OPACK, ONONAME:
// unreachable - should have been resolved by typechecking // unreachable - should have been resolved by typechecking

View File

@ -44,8 +44,8 @@ func fninit(n []ir.Node) {
// Find imported packages with init tasks. // Find imported packages with init tasks.
for _, pkg := range sourceOrderImports { for _, pkg := range sourceOrderImports {
n := resolve(ir.AsNode(pkg.Lookup(".inittask").Def)) n := resolve(oldname(pkg.Lookup(".inittask")))
if n == nil { if n.Op() == ir.ONONAME {
continue continue
} }
if n.Op() != ir.ONAME || n.Class() != ir.PEXTERN { if n.Op() != ir.ONAME || n.Class() != ir.PEXTERN {

View File

@ -293,8 +293,10 @@ func Main(archInit func(*Arch)) {
} }
} }
// Phase 3.14: With all user code type-checked, it's now safe to verify map keys. // Phase 3.14: With all user code type-checked, it's now safe to verify map keys
// and unused dot imports.
checkMapKeys() checkMapKeys()
checkDotImports()
base.ExitIfErrors() base.ExitIfErrors()
timings.AddEvent(fcount, "funcs") timings.AddEvent(fcount, "funcs")
@ -953,10 +955,7 @@ func clearImports() {
if IsAlias(s) { if IsAlias(s) {
// throw away top-level name left over // throw away top-level name left over
// from previous import . "x" // from previous import . "x"
if name := n.Name(); name != nil && name.PkgName != nil && !name.PkgName.Used && base.SyntaxErrors() == 0 { // We'll report errors after type checking in checkDotImports.
unused = append(unused, importedPkg{name.PkgName.Pos(), name.PkgName.Pkg.Path, ""})
name.PkgName.Used = true
}
s.Def = nil s.Def = nil
continue continue
} }

View File

@ -369,7 +369,7 @@ func (p *noder) importDecl(imp *syntax.ImportDecl) {
switch my.Name { switch my.Name {
case ".": case ".":
importdot(ipkg, pack) importDot(pack)
return return
case "init": case "init":
base.ErrorfAt(pack.Pos(), "cannot import package as init - init must be a func") base.ErrorfAt(pack.Pos(), "cannot import package as init - init must be a func")

View File

@ -100,13 +100,26 @@ func autolabel(prefix string) *types.Sym {
return lookupN(prefix, int(n)) 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 // and make them available in the current package
func importdot(opkg *types.Pkg, pack *ir.PkgName) { func importDot(pack *ir.PkgName) {
n := 0 if dotImportRefs == nil {
dotImportRefs = make(map[*ir.Ident]*ir.PkgName)
}
opkg := pack.Pkg
for _, s := range opkg.Syms { for _, s := range opkg.Syms {
if s.Def == nil { if s.Def == nil {
continue if _, ok := declImporter[s]; !ok {
continue
}
} }
if !types.IsExported(s.Name) || strings.ContainsRune(s.Name, 0xb7) { // 0xb7 = center dot if !types.IsExported(s.Name) || strings.ContainsRune(s.Name, 0xb7) { // 0xb7 = center dot
continue continue
@ -118,21 +131,26 @@ func importdot(opkg *types.Pkg, pack *ir.PkgName) {
continue continue
} }
s1.Def = s.Def id := ir.NewIdent(src.NoXPos, s)
s1.Block = s.Block dotImportRefs[id] = pack
if ir.AsNode(s1.Def).Name() == nil { s1.Def = id
ir.Dump("s1def", ir.AsNode(s1.Def)) s1.Block = 1
base.Fatalf("missing Name")
}
ir.AsNode(s1.Def).Name().PkgName = pack
s1.Origpkg = opkg
n++
} }
if n == 0 { dotImports = append(dotImports, pack)
// can't possibly be used - there were no symbols }
base.ErrorfAt(pack.Pos(), "imported and not used: %q", opkg.Path)
// 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. // nodAddr returns a node representing &n.

View File

@ -8,6 +8,7 @@ import (
"cmd/compile/internal/base" "cmd/compile/internal/base"
"cmd/compile/internal/ir" "cmd/compile/internal/ir"
"cmd/compile/internal/types" "cmd/compile/internal/types"
"cmd/internal/src"
"fmt" "fmt"
"go/constant" "go/constant"
"go/token" "go/token"
@ -90,11 +91,24 @@ func resolve(n ir.Node) (res ir.Node) {
defer tracePrint("resolve", n)(&res) defer tracePrint("resolve", n)(&res)
} }
// Stub ir.Name left for us by iimport. if sym := n.Sym(); sym.Pkg != types.LocalPkg {
if n, ok := n.(*ir.Name); ok { // We might have an ir.Ident from oldname or importDot.
if n.Sym().Pkg == types.LocalPkg { if id, ok := n.(*ir.Ident); ok {
base.Fatalf("unexpected Name: %+v", n) if pkgName := dotImportRefs[id]; pkgName != nil {
pkgName.Used = true
}
if sym.Def == nil {
if _, ok := declImporter[sym]; !ok {
return n // undeclared name
}
sym.Def = ir.NewDeclNameAt(src.NoXPos, sym)
}
n = ir.AsNode(sym.Def)
} }
// Stub ir.Name left for us by iimport.
n := n.(*ir.Name)
if inimport { if inimport {
base.Fatalf("recursive inimport") base.Fatalf("recursive inimport")
} }
@ -2885,31 +2899,25 @@ func typecheckcomplit(n ir.Node) (res ir.Node) {
if l.Op() == ir.OKEY { if l.Op() == ir.OKEY {
key := l.Left() key := l.Left()
sk := ir.NewStructKeyExpr(l.Pos(), nil, l.Right()) // Sym might have resolved to name in other top-level
ls[i] = sk // package, because of import dot. Redirect to correct sym
l = sk // before we do the lookup.
s := key.Sym()
if id, ok := key.(*ir.Ident); ok && dotImportRefs[id] != nil {
s = lookup(s.Name)
}
// An OXDOT uses the Sym field to hold // An OXDOT uses the Sym field to hold
// the field to the right of the dot, // the field to the right of the dot,
// so s will be non-nil, but an OXDOT // so s will be non-nil, but an OXDOT
// is never a valid struct literal key. // is never a valid struct literal key.
if key.Sym() == nil || key.Op() == ir.OXDOT || key.Sym().IsBlank() { if s == nil || s.Pkg != types.LocalPkg || key.Op() == ir.OXDOT || s.IsBlank() {
base.Errorf("invalid field name %v in struct initializer", key) base.Errorf("invalid field name %v in struct initializer", key)
sk.SetLeft(typecheck(sk.Left(), ctxExpr))
continue continue
} }
// Sym might have resolved to name in other top-level l = ir.NewStructKeyExpr(l.Pos(), s, l.Right())
// package, because of import dot. Redirect to correct sym ls[i] = l
// before we do the lookup.
s := key.Sym()
if s.Pkg != types.LocalPkg && types.IsExported(s.Name) {
s1 := lookup(s.Name)
if s1.Origpkg == s.Pkg {
s = s1
}
}
sk.SetSym(s)
} }
if l.Op() != ir.OSTRUCTKEY { if l.Op() != ir.OSTRUCTKEY {

View File

@ -16,8 +16,7 @@ import (
// An Ident is an identifier, possibly qualified. // An Ident is an identifier, possibly qualified.
type Ident struct { type Ident struct {
miniExpr miniExpr
sym *types.Sym sym *types.Sym
Used bool
} }
func NewIdent(pos src.XPos, sym *types.Sym) *Ident { func NewIdent(pos src.XPos, sym *types.Sym) *Ident {

View File

@ -20,7 +20,7 @@ func TestSizeof(t *testing.T) {
_32bit uintptr // size on 32bit platforms _32bit uintptr // size on 32bit platforms
_64bit uintptr // size on 64bit platforms _64bit uintptr // size on 64bit platforms
}{ }{
{Sym{}, 52, 88}, {Sym{}, 48, 80},
{Type{}, 56, 96}, {Type{}, 56, 96},
{Map{}, 20, 40}, {Map{}, 20, 40},
{Forward{}, 20, 32}, {Forward{}, 20, 32},

View File

@ -38,8 +38,7 @@ type Sym struct {
Block int32 // blocknumber to catch redeclaration Block int32 // blocknumber to catch redeclaration
Lastlineno src.XPos // last declaration for diagnostic Lastlineno src.XPos // last declaration for diagnostic
flags bitset8 flags bitset8
Origpkg *Pkg // original package for . import
} }
const ( const (

View File

@ -13,7 +13,7 @@ type T struct {
} }
func main() { func main() {
_ = T { _ = T{
os.File: 1, // ERROR "unknown T? ?field" os.File: 1, // ERROR "invalid field name os.File|unknown field"
} }
} }

View File

@ -11,7 +11,7 @@ package p
// 1 // 1
var f byte var f byte
var f interface{} // ERROR "previous declaration at issue20415.go:12" var f interface{} // ERROR "issue20415.go:12: previous declaration"
func _(f int) { func _(f int) {
} }
@ -22,7 +22,7 @@ var g byte
func _(g int) { func _(g int) {
} }
var g interface{} // ERROR "previous declaration at issue20415.go:20" var g interface{} // ERROR "issue20415.go:20: previous declaration"
// 3 // 3
func _(h int) { func _(h int) {
@ -30,4 +30,4 @@ func _(h int) {
var h byte var h byte
var h interface{} // ERROR "previous declaration at issue20415.go:31" var h interface{} // ERROR "issue20415.go:31: previous declaration"

View File

@ -0,0 +1,13 @@
// Copyright 2020 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 . "strings"
var _ = Index // use strings
type t struct{ Index int }
var _ = t{Index: 0}

View File

@ -0,0 +1,11 @@
// Copyright 2020 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 . "bytes"
var _ = Index // use bytes
var _ = t{Index: 0}

View File

@ -0,0 +1,7 @@
// compiledir
// Copyright 2020 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

View File

@ -0,0 +1,13 @@
// errorcheck
// Copyright 2020 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 . "bytes"
var _ Buffer // use package bytes
var Index byte // ERROR "Index redeclared.*\n\tLINE-4: previous declaration during import .bytes.|already declared|redefinition"

View File

@ -0,0 +1,15 @@
// errorcheck
// Copyright 2020 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 . "testing" // ERROR "imported and not used"
type S struct {
T int
}
var _ = S{T: 0}