mirror of
https://github.com/golang/go
synced 2024-11-02 09:28:34 +00:00
[dev.unified] cmd/compile/internal/noder: explicit nil handling
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>
This commit is contained in:
parent
e971b6a9be
commit
318027044a
3 changed files with 13 additions and 0 deletions
|
@ -53,6 +53,7 @@ const (
|
|||
exprConvert
|
||||
exprNew
|
||||
exprMake
|
||||
exprNil
|
||||
)
|
||||
|
||||
type codeAssign int
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue