Skip to content

Commit ac8421f

Browse files
committed
[dev.typealias] cmd/compile: various minor cleanups
Also: Don't allow type pragmas with type alias declarations. For #18130. Change-Id: Ie54ea5fefcd677ad87ced03466bbfd783771e974 Reviewed-on: https://go-review.googlesource.com/35102 Reviewed-by: Matthew Dempsky <[email protected]>
1 parent f011e0c commit ac8421f

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

src/cmd/compile/internal/gc/main.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,9 @@ func Main() {
344344
// Don't use range--typecheck can add closures to xtop.
345345
timings.Start("fe", "typecheck", "top1")
346346
for i := 0; i < len(xtop); i++ {
347-
if xtop[i].Op != ODCL && xtop[i].Op != OAS && xtop[i].Op != OAS2 {
348-
xtop[i] = typecheck(xtop[i], Etop)
347+
n := xtop[i]
348+
if op := n.Op; op != ODCL && op != OAS && op != OAS2 {
349+
xtop[i] = typecheck(n, Etop)
349350
}
350351
}
351352

@@ -355,8 +356,9 @@ func Main() {
355356
// Don't use range--typecheck can add closures to xtop.
356357
timings.Start("fe", "typecheck", "top2")
357358
for i := 0; i < len(xtop); i++ {
358-
if xtop[i].Op == ODCL || xtop[i].Op == OAS || xtop[i].Op == OAS2 {
359-
xtop[i] = typecheck(xtop[i], Etop)
359+
n := xtop[i]
360+
if op := n.Op; op == ODCL || op == OAS || op == OAS2 {
361+
xtop[i] = typecheck(n, Etop)
360362
}
361363
}
362364
resumecheckwidth()
@@ -366,8 +368,9 @@ func Main() {
366368
timings.Start("fe", "typecheck", "func")
367369
var fcount int64
368370
for i := 0; i < len(xtop); i++ {
369-
if xtop[i].Op == ODCLFUNC || xtop[i].Op == OCLOSURE {
370-
Curfn = xtop[i]
371+
n := xtop[i]
372+
if op := n.Op; op == ODCLFUNC || op == OCLOSURE {
373+
Curfn = n
371374
decldepth = 1
372375
saveerrors()
373376
typecheckslice(Curfn.Nbody.Slice(), Etop)
@@ -459,8 +462,9 @@ func Main() {
459462
timings.Start("be", "compilefuncs")
460463
fcount = 0
461464
for i := 0; i < len(xtop); i++ {
462-
if xtop[i].Op == ODCLFUNC {
463-
funccompile(xtop[i])
465+
n := xtop[i]
466+
if n.Op == ODCLFUNC {
467+
funccompile(n)
464468
fcount++
465469
}
466470
}

src/cmd/compile/internal/gc/noder.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,7 @@ func (p *noder) importDecl(imp *syntax.ImportDecl) {
153153

154154
func (p *noder) varDecl(decl *syntax.VarDecl) []*Node {
155155
names := p.declNames(decl.NameList)
156-
157-
var typ *Node
158-
if decl.Type != nil {
159-
typ = p.typeExpr(decl.Type)
160-
}
156+
typ := p.typeExprOrNil(decl.Type)
161157

162158
var exprs []*Node
163159
if decl.Values != nil {
@@ -170,11 +166,7 @@ func (p *noder) varDecl(decl *syntax.VarDecl) []*Node {
170166

171167
func (p *noder) constDecl(decl *syntax.ConstDecl) []*Node {
172168
names := p.declNames(decl.NameList)
173-
174-
var typ *Node
175-
if decl.Type != nil {
176-
typ = p.typeExpr(decl.Type)
177-
}
169+
typ := p.typeExprOrNil(decl.Type)
178170

179171
var exprs []*Node
180172
if decl.Values != nil {
@@ -190,12 +182,14 @@ func (p *noder) typeDecl(decl *syntax.TypeDecl) *Node {
190182
}
191183

192184
name := typedcl0(p.name(decl.Name))
193-
name.Name.Param.Pragma = Pragma(decl.Pragma)
194-
195-
var typ *Node
196-
if decl.Type != nil {
197-
typ = p.typeExpr(decl.Type)
185+
pragma := Pragma(decl.Pragma)
186+
if pragma != 0 && decl.Alias {
187+
yyerror("cannot specify directive with type alias")
188+
pragma = 0
198189
}
190+
name.Name.Param.Pragma = pragma
191+
192+
typ := p.typeExprOrNil(decl.Type)
199193

200194
return typedcl1(name, typ, true)
201195
}
@@ -470,6 +464,13 @@ func (p *noder) typeExpr(typ syntax.Expr) *Node {
470464
return p.expr(typ)
471465
}
472466

467+
func (p *noder) typeExprOrNil(typ syntax.Expr) *Node {
468+
if typ != nil {
469+
return p.expr(typ)
470+
}
471+
return nil
472+
}
473+
473474
func (p *noder) chanDir(dir syntax.ChanDir) ChanDir {
474475
switch dir {
475476
case 0:

0 commit comments

Comments
 (0)