Skip to content

Commit 13c70b1

Browse files
committed
cmd/compile/internal/types: remove Markdcl/Pushdcl/Popdcl
Sym.Def used to be used for symbol resolution during the old (pre-types2) typechecker. But since moving to types2-based IR construction, we haven't really had a need for Sym.Def to ever refer to anything but the package-scope definition, because types2 handles symbol resolution for us. This CL finally removes the Markdcl/Pushdcl/Popdcl functions that have been a recurring source of issues in the past. Change-Id: I2b012a0f17203efdd724ebd1e9314bd128cc2d61 Reviewed-on: https://go-review.googlesource.com/c/go/+/458625 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent f0b1563 commit 13c70b1

File tree

6 files changed

+3
-109
lines changed

6 files changed

+3
-109
lines changed

src/cmd/compile/internal/base/debug.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ type DebugFlags struct {
1919
Append int `help:"print information about append compilation"`
2020
Checkptr int `help:"instrument unsafe pointer conversions\n0: instrumentation disabled\n1: conversions involving unsafe.Pointer are instrumented\n2: conversions to unsafe.Pointer force heap allocation" concurrent:"ok"`
2121
Closure int `help:"print information about closure compilation"`
22-
DclStack int `help:"run internal dclstack check"`
2322
Defer int `help:"print information about defer compilation"`
2423
DisableNil int `help:"disable nil checks" concurrent:"ok"`
2524
DumpPtrs int `help:"show Node pointers values in dump output"`

src/cmd/compile/internal/reflectdata/alg.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,6 @@ func genhash(t *types.Type) *obj.LSym {
224224
typecheck.Stmts(fn.Body)
225225
ir.CurFunc = nil
226226

227-
if base.Debug.DclStack != 0 {
228-
types.CheckDclstack()
229-
}
230-
231227
fn.SetNilCheckDisabled(true)
232228
typecheck.Target.Decls = append(typecheck.Target.Decls, fn)
233229

@@ -552,10 +548,6 @@ func geneq(t *types.Type) *obj.LSym {
552548
typecheck.Stmts(fn.Body)
553549
ir.CurFunc = nil
554550

555-
if base.Debug.DclStack != 0 {
556-
types.CheckDclstack()
557-
}
558-
559551
// Disable checknils while compiling this code.
560552
// We are comparing a struct or an array,
561553
// neither of which can be nil, and our comparisons

src/cmd/compile/internal/ssagen/abi.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,6 @@ func makeABIWrapper(f *ir.Func, wrapperABI obj.ABI) {
324324
fn.Body.Append(tail)
325325

326326
typecheck.FinishFuncBody()
327-
if base.Debug.DclStack != 0 {
328-
types.CheckDclstack()
329-
}
330327

331328
typecheck.Func(fn)
332329
ir.CurFunc = fn

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func Declare(n *ir.Name, ctxt ir.Class) {
5959
base.ErrorfAt(n.Pos(), "cannot declare main - must be func")
6060
}
6161
Target.Externs = append(Target.Externs, n)
62+
s.Def = n
6263
} else {
6364
if ir.CurFunc == nil && ctxt == ir.PAUTO {
6465
base.Pos = n.Pos()
@@ -67,15 +68,13 @@ func Declare(n *ir.Name, ctxt ir.Class) {
6768
if ir.CurFunc != nil && ctxt != ir.PFUNC && n.Op() == ir.ONAME {
6869
ir.CurFunc.Dcl = append(ir.CurFunc.Dcl, n)
6970
}
70-
types.Pushdcl(s)
7171
n.Curfn = ir.CurFunc
7272
}
7373

7474
if ctxt == ir.PAUTO {
7575
n.SetFrameOffset(0)
7676
}
7777

78-
s.Def = n
7978
n.Class = ctxt
8079
if ctxt == ir.PFUNC {
8180
n.Sym().SetFunc(true)
@@ -107,16 +106,13 @@ func StartFuncBody(fn *ir.Func) {
107106
funcStack = append(funcStack, funcStackEnt{ir.CurFunc, DeclContext})
108107
ir.CurFunc = fn
109108
DeclContext = ir.PAUTO
110-
111-
types.Markdcl()
112109
}
113110

114111
// finish the body.
115112
// called in auto-declaration context.
116113
// returns in extern-declaration context.
117114
func FinishFuncBody() {
118115
// change the declaration context from auto to previous context
119-
types.Popdcl()
120116
var e funcStackEnt
121117
funcStack, e = funcStack[:len(funcStack)-1], funcStack[len(funcStack)-1]
122118
ir.CurFunc, DeclContext = e.curfn, e.dclcontext

src/cmd/compile/internal/types/scope.go

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,96 +4,8 @@
44

55
package types
66

7-
import (
8-
"cmd/compile/internal/base"
9-
)
10-
11-
// Declaration stack & operations
12-
13-
// A dsym stores a symbol's shadowed declaration so that it can be
14-
// restored once the block scope ends.
15-
type dsym struct {
16-
sym *Sym // sym == nil indicates stack mark
17-
def Object
18-
}
19-
20-
// dclstack maintains a stack of shadowed symbol declarations so that
21-
// Popdcl can restore their declarations when a block scope ends.
22-
var dclstack []dsym
23-
24-
// Pushdcl pushes the current declaration for symbol s (if any) so that
25-
// it can be shadowed by a new declaration within a nested block scope.
26-
func Pushdcl(s *Sym) {
27-
dclstack = append(dclstack, dsym{
28-
sym: s,
29-
def: s.Def,
30-
})
31-
}
32-
33-
// Popdcl pops the innermost block scope and restores all symbol declarations
34-
// to their previous state.
35-
func Popdcl() {
36-
for i := len(dclstack); i > 0; i-- {
37-
d := &dclstack[i-1]
38-
s := d.sym
39-
if s == nil {
40-
// pop stack mark
41-
dclstack = dclstack[:i-1]
42-
return
43-
}
44-
45-
s.Def = d.def
46-
47-
// Clear dead pointer fields.
48-
d.sym = nil
49-
d.def = nil
50-
}
51-
base.Fatalf("popdcl: no stack mark")
52-
}
53-
54-
// Markdcl records the start of a new block scope for declarations.
55-
func Markdcl() {
56-
dclstack = append(dclstack, dsym{
57-
sym: nil, // stack mark
58-
})
59-
}
60-
61-
func isDclstackValid() bool {
62-
for _, d := range dclstack {
63-
if d.sym == nil {
64-
return false
65-
}
66-
}
67-
return true
68-
}
69-
707
// PkgDef returns the definition associated with s at package scope.
71-
func (s *Sym) PkgDef() Object {
72-
return *s.pkgDefPtr()
73-
}
8+
func (s *Sym) PkgDef() Object { return s.Def }
749

7510
// SetPkgDef sets the definition associated with s at package scope.
76-
func (s *Sym) SetPkgDef(n Object) {
77-
*s.pkgDefPtr() = n
78-
}
79-
80-
func (s *Sym) pkgDefPtr() *Object {
81-
// Look for outermost saved declaration, which must be the
82-
// package scope definition, if present.
83-
for i := range dclstack {
84-
d := &dclstack[i]
85-
if s == d.sym {
86-
return &d.def
87-
}
88-
}
89-
90-
// Otherwise, the declaration hasn't been shadowed within a
91-
// function scope.
92-
return &s.Def
93-
}
94-
95-
func CheckDclstack() {
96-
if !isDclstackValid() {
97-
base.Fatalf("mark left on the dclstack")
98-
}
99-
}
11+
func (s *Sym) SetPkgDef(n Object) { s.Def = n }

src/cmd/compile/internal/types/sym.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ type Sym struct {
3535
// bound to within the current scope. (Most parts of the compiler should
3636
// prefer passing the Node directly, rather than relying on this field.)
3737
//
38-
// Def is saved and restored by Pushdcl/Popdcl.
39-
//
4038
// Deprecated: New code should avoid depending on Sym.Def. Add
4139
// mdempsky@ as a reviewer for any CLs involving Sym.Def.
4240
Def Object

0 commit comments

Comments
 (0)