mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
[dev.typeparams] cmd/compile: avoid ir.DeepCopy in noder.constDecl
Instead of using ir.DeepCopy to copy the IR from the previous constant declaration, just call exprList again and then fix up the position information. This is equivalent in practice, but has cleaner semantics for tricky corner cases like constant declarations that contain function literals. In particular, this refactoring is necessary for the next CL that cleans up function literal construction, because it adds extra consistency checks that weren't satisfied by DeepCopy'd OCLOSUREs. Change-Id: I0372bde5d6613695ee572cc8bf8fb4ff9aef4cb4 Reviewed-on: https://go-review.googlesource.com/c/go/+/327449 Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
parent
2954f11ead
commit
8f00eb0099
1 changed files with 11 additions and 6 deletions
|
|
@ -450,7 +450,7 @@ func (p *noder) varDecl(decl *syntax.VarDecl) []ir.Node {
|
||||||
type constState struct {
|
type constState struct {
|
||||||
group *syntax.Group
|
group *syntax.Group
|
||||||
typ ir.Ntype
|
typ ir.Ntype
|
||||||
values []ir.Node
|
values syntax.Expr
|
||||||
iota int64
|
iota int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -468,16 +468,15 @@ func (p *noder) constDecl(decl *syntax.ConstDecl, cs *constState) []ir.Node {
|
||||||
names := p.declNames(ir.OLITERAL, decl.NameList)
|
names := p.declNames(ir.OLITERAL, decl.NameList)
|
||||||
typ := p.typeExprOrNil(decl.Type)
|
typ := p.typeExprOrNil(decl.Type)
|
||||||
|
|
||||||
var values []ir.Node
|
|
||||||
if decl.Values != nil {
|
if decl.Values != nil {
|
||||||
values = p.exprList(decl.Values)
|
cs.typ, cs.values = typ, decl.Values
|
||||||
cs.typ, cs.values = typ, values
|
|
||||||
} else {
|
} else {
|
||||||
if typ != nil {
|
if typ != nil {
|
||||||
base.Errorf("const declaration cannot have type without expression")
|
base.Errorf("const declaration cannot have type without expression")
|
||||||
}
|
}
|
||||||
typ, values = cs.typ, cs.values
|
typ = cs.typ
|
||||||
}
|
}
|
||||||
|
values := p.exprList(cs.values)
|
||||||
|
|
||||||
nn := make([]ir.Node, 0, len(names))
|
nn := make([]ir.Node, 0, len(names))
|
||||||
for i, n := range names {
|
for i, n := range names {
|
||||||
|
|
@ -485,10 +484,16 @@ func (p *noder) constDecl(decl *syntax.ConstDecl, cs *constState) []ir.Node {
|
||||||
base.Errorf("missing value in const declaration")
|
base.Errorf("missing value in const declaration")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
v := values[i]
|
v := values[i]
|
||||||
if decl.Values == nil {
|
if decl.Values == nil {
|
||||||
v = ir.DeepCopy(n.Pos(), v)
|
ir.Visit(v, func(v ir.Node) {
|
||||||
|
if ir.HasUniquePos(v) {
|
||||||
|
v.SetPos(n.Pos())
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
typecheck.Declare(n, typecheck.DeclContext)
|
typecheck.Declare(n, typecheck.DeclContext)
|
||||||
|
|
||||||
n.Ntype = typ
|
n.Ntype = typ
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue