[dev.unified] cmd/compile: extract nil handling from exprType

Type switches are the only context where exprType was used and `nilOK`
was true. It'll simplify subsequent dictionary work somewhat if
exprType doesn't need to worry about `nil`, so extract this logic and
move it into switchStmt instead.

Change-Id: I3d810f465173f5bb2e2dee7bbc7843fff6a62ee5
Reviewed-on: https://go-review.googlesource.com/c/go/+/419474
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Matthew Dempsky 2022-07-25 12:21:21 -07:00
parent 92798176e7
commit 831fdf1dff
2 changed files with 26 additions and 21 deletions

View file

@ -1547,7 +1547,11 @@ func (r *reader) switchStmt(label *types.Sym) ir.Node {
cases = nil // TODO(mdempsky): Unclear if this matters.
}
for i := range cases {
cases[i] = r.exprType(true)
if r.Bool() { // case nil
cases[i] = typecheck.Expr(types.BuiltinPkg.Lookup("nil").Def.(*ir.NilExpr))
} else {
cases[i] = r.exprType()
}
}
} else {
cases = r.exprList()
@ -1734,7 +1738,7 @@ func (r *reader) expr() (res ir.Node) {
case exprAssert:
x := r.expr()
pos := r.pos()
typ := r.exprType(false)
typ := r.exprType()
srcRType := r.rtype(pos)
// TODO(mdempsky): Always emit ODYNAMICDOTTYPE for uniformity?
@ -1800,7 +1804,7 @@ func (r *reader) expr() (res ir.Node) {
case exprMake:
pos := r.pos()
typ := r.exprType(false)
typ := r.exprType()
extra := r.exprs()
n := typecheck.Expr(ir.NewCallExpr(pos, ir.OMAKE, nil, append([]ir.Node{typ}, extra...))).(*ir.MakeExpr)
n.RType = r.rtype(pos)
@ -1808,7 +1812,7 @@ func (r *reader) expr() (res ir.Node) {
case exprNew:
pos := r.pos()
typ := r.exprType(false)
typ := r.exprType()
return typecheck.Expr(ir.NewUnaryExpr(pos, ir.ONEW, typ))
case exprConvert:
@ -2043,13 +2047,9 @@ func (r *reader) convRTTI(pos src.XPos) (typeWord, srcRType ir.Node) {
return
}
func (r *reader) exprType(nilOK bool) ir.Node {
func (r *reader) exprType() ir.Node {
r.Sync(pkgbits.SyncExprType)
if nilOK && r.Bool() {
return typecheck.Expr(types.BuiltinPkg.Lookup("nil").Def.(*ir.NilExpr))
}
pos := r.pos()
setBasePos(pos)