cmd/compile/internal/types: remove Sym.Block and Sym.Lastlineno

These fields were used for tracking the last scope/position that an
identifier was declared, so that we could report redeclaration
errors. However, redeclaration errors are now diagnosed by types2 (and
typecheck.Redeclared was removed in CL 388537), so these fields can be
safely pruned.

Updates #51691.

Change-Id: Ifd5ea3f6795fadb420913298d59287c95e4669a1
Reviewed-on: https://go-review.googlesource.com/c/go/+/394276
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Matthew Dempsky 2022-03-21 11:24:24 -07:00
parent d8ce7ae491
commit 79103faf2d
6 changed files with 12 additions and 32 deletions

View File

@ -70,8 +70,6 @@ func Declare(n *ir.Name, ctxt ir.Class) {
n.SetFrameOffset(0)
}
s.Block = types.Block
s.Lastlineno = base.Pos
s.Def = n
n.Class = ctxt
if ctxt == ir.PFUNC {

View File

@ -67,7 +67,6 @@ func Lookup(name string) *types.Sym {
// but does not make them visible to user code.
func InitRuntime() {
base.Timer.Start("fe", "loadsys")
types.Block = 1
typs := runtimeTypes()
for _, d := range &runtimeDecls {

View File

@ -91,14 +91,12 @@ func InitUniverse() {
s = Lookup("_")
types.BlankSym = s
s.Block = -100
s.Def = NewName(s)
ir.AsNode(s.Def).SetType(types.Types[types.TBLANK])
ir.BlankNode = ir.AsNode(s.Def)
ir.BlankNode.SetTypecheck(1)
s = types.BuiltinPkg.Lookup("_")
s.Block = -100
s.Def = NewName(s)
ir.AsNode(s.Def).SetType(types.Types[types.TBLANK])
@ -222,6 +220,5 @@ func DeclareUniverse() {
}
s1.Def = s.Def
s1.Block = s.Block
}
}

View File

@ -6,21 +6,15 @@ package types
import (
"cmd/compile/internal/base"
"cmd/internal/src"
)
// Declaration stack & operations
var blockgen int32 = 1 // max block number
var Block int32 = 1 // current block number
// A dsym stores a symbol's shadowed declaration so that it can be
// restored once the block scope ends.
type dsym struct {
sym *Sym // sym == nil indicates stack mark
def Object
block int32
lastlineno src.XPos // last declaration for diagnostic
sym *Sym // sym == nil indicates stack mark
def Object
}
// dclstack maintains a stack of shadowed symbol declarations so that
@ -31,10 +25,8 @@ var dclstack []dsym
// it can be shadowed by a new declaration within a nested block scope.
func Pushdcl(s *Sym) {
dclstack = append(dclstack, dsym{
sym: s,
def: s.Def,
block: s.Block,
lastlineno: s.Lastlineno,
sym: s,
def: s.Def,
})
}
@ -46,14 +38,11 @@ func Popdcl() {
s := d.sym
if s == nil {
// pop stack mark
Block = d.block
dclstack = dclstack[:i-1]
return
}
s.Def = d.def
s.Block = d.block
s.Lastlineno = d.lastlineno
// Clear dead pointer fields.
d.sym = nil
@ -65,11 +54,8 @@ func Popdcl() {
// Markdcl records the start of a new block scope for declarations.
func Markdcl() {
dclstack = append(dclstack, dsym{
sym: nil, // stack mark
block: Block,
sym: nil, // stack mark
})
blockgen++
Block = blockgen
}
func isDclstackValid() bool {

View File

@ -20,7 +20,7 @@ func TestSizeof(t *testing.T) {
_32bit uintptr // size on 32bit platforms
_64bit uintptr // size on 64bit platforms
}{
{Sym{}, 44, 72},
{Sym{}, 32, 64},
{Type{}, 64, 112},
{Map{}, 20, 40},
{Forward{}, 20, 32},

View File

@ -7,7 +7,6 @@ package types
import (
"cmd/compile/internal/base"
"cmd/internal/obj"
"cmd/internal/src"
"unicode"
"unicode/utf8"
)
@ -32,14 +31,15 @@ type Sym struct {
Pkg *Pkg
Name string // object name
// Def, Block, and Lastlineno are saved and restored by Pushdcl/Popdcl.
// The unique ONAME, OTYPE, OPACK, or OLITERAL node that this symbol is
// bound to within the current scope. (Most parts of the compiler should
// prefer passing the Node directly, rather than relying on this field.)
Def Object
Block int32 // blocknumber to catch redeclaration
Lastlineno src.XPos // last declaration for diagnostic
//
// Def is saved and restored by Pushdcl/Popdcl.
//
// Deprecated: New code should avoid depending on Sym.Def. Add
// mdempsky@ as a reviewer for any CLs involving Sym.Def.
Def Object
flags bitset8
}