]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.unified] cmd/compile/internal/noder: explicit nil handling
authorMatthew Dempsky <mdempsky@google.com>
Mon, 18 Jul 2022 20:13:46 +0000 (13:13 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 19 Jul 2022 23:30:58 +0000 (23:30 +0000)
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>
src/cmd/compile/internal/noder/codes.go
src/cmd/compile/internal/noder/reader.go
src/cmd/compile/internal/noder/writer.go

index f7ad2503c2cdfe9b9c1d53e6c72562e533e24c9e..1a60ea39bb8738847b33ba581503685d21d0d532 100644 (file)
@@ -53,6 +53,7 @@ const (
        exprConvert
        exprNew
        exprMake
+       exprNil
 )
 
 type codeAssign int
index 8cb0df182c0fb2618693e32f35d2053edb4888f9..4c90f9dc54cdfa7529702c241182bffae6f7aa7e 100644 (file)
@@ -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()
 
index 7ad87146fb06e290b6350fc06d2979006e371e5e..47384c6c64065e237f56b04c5b781aa9e24ad9ca 100644 (file)
@@ -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 {