Skip to content

Commit

Permalink
[dev.unified] cmd/compile/internal/noder: explicit nil handling
Browse files Browse the repository at this point in the history
Currently, uses of "nil" are handling as references to cmd/compile's
own untyped "nil" object, and then we rely on implicitly converting
that to its appropriate type. But there are cases where this can
subtly go wrong (e.g., the switch test case added in the previous CL).

Instead, explicitly handling "nil" expressions so that we can
construct them directly with the appropriate type, as computed already
by types2.

Change-Id: I587f044f60f24e87525dde6d7dad6c58f14478de
Reviewed-on: https://go-review.googlesource.com/c/go/+/418100
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
  • Loading branch information
mdempsky committed Jul 19, 2022
1 parent e971b6a commit 3180270
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cmd/compile/internal/noder/codes.go
Expand Up @@ -53,6 +53,7 @@ const (
exprConvert
exprNew
exprMake
exprNil
)

type codeAssign int
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/compile/internal/noder/reader.go
Expand Up @@ -1684,6 +1684,11 @@ func (r *reader) expr() (res ir.Node) {
orig := r.String()
return typecheck.Expr(OrigConst(pos, typ, val, op, orig))

case exprNil:
pos := r.pos()
typ := r.typ()
return Nil(pos, typ)

case exprCompLit:
return r.compLit()

Expand Down
7 changes: 7 additions & 0 deletions src/cmd/compile/internal/noder/writer.go
Expand Up @@ -1413,6 +1413,13 @@ func (w *writer) expr(expr syntax.Expr) {
w.String(syntax.String(expr))
return
}

if _, isNil := obj.(*types2.Nil); isNil {
w.Code(exprNil)
w.pos(expr)
w.typ(tv.Type)
return
}
}

if obj != nil {
Expand Down

0 comments on commit 3180270

Please sign in to comment.