]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/compile: refactor type/value assertions
authorMatthew Dempsky <mdempsky@google.com>
Fri, 20 Nov 2020 21:23:58 +0000 (13:23 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 24 Nov 2020 19:35:46 +0000 (19:35 +0000)
Small refactoring to make subsequent CLs clearer.

Passes toolstash-check.

Change-Id: I1a6ae599f491220d44aaabae0b7bed4aff46ee92
Reviewed-on: https://go-review.googlesource.com/c/go/+/272651
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>

src/cmd/compile/internal/gc/const.go
src/cmd/compile/internal/gc/iexport.go
src/cmd/compile/internal/gc/syntax.go
src/cmd/compile/internal/gc/typecheck.go

index 42ac3a26f8cbb3e4f33c60a2e8862a75b09536f9..4e7318cfc6d28967a627f16acc06be4088eaaf60 100644 (file)
@@ -275,8 +275,8 @@ func convlit1(n *Node, t *types.Type, explicit bool, context func() string) *Nod
                if v.U == nil {
                        break
                }
-               n.SetVal(v)
                n.Type = t
+               n.SetVal(v)
                return n
 
        case OPLUS, ONEG, OBITNOT, ONOT, OREAL, OIMAG:
@@ -979,9 +979,6 @@ func setconst(n *Node, v Val) {
                Xoffset: BADWIDTH,
        }
        n.SetVal(v)
-       if vt := idealType(v.Ctype()); n.Type.IsUntyped() && n.Type != vt {
-               Fatalf("untyped type mismatch, have: %v, want: %v", n.Type, vt)
-       }
 
        // Check range.
        lno := setlineno(n)
@@ -1000,6 +997,22 @@ func setconst(n *Node, v Val) {
        }
 }
 
+func assertRepresents(t *types.Type, v Val) {
+       if !represents(t, v) {
+               Fatalf("%v does not represent %v", t, v)
+       }
+}
+
+func represents(t *types.Type, v Val) bool {
+       if !t.IsUntyped() {
+               // TODO(mdempsky): Stricter handling of typed types.
+               return true
+       }
+
+       vt := idealType(v.Ctype())
+       return t == vt
+}
+
 func setboolconst(n *Node, v bool) {
        setconst(n, Val{U: v})
 }
@@ -1013,8 +1026,8 @@ func setintconst(n *Node, v int64) {
 // nodlit returns a new untyped constant with value v.
 func nodlit(v Val) *Node {
        n := nod(OLITERAL, nil, nil)
-       n.SetVal(v)
        n.Type = idealType(v.Ctype())
+       n.SetVal(v)
        return n
 }
 
index b48a840d00000320f86b7c55fceb67a852f6b442..c3385f785ac1970cad4682dafec5cfc495c3459b 100644 (file)
@@ -777,9 +777,7 @@ func constTypeOf(typ *types.Type) Ctype {
 }
 
 func (w *exportWriter) value(typ *types.Type, v Val) {
-       if vt := idealType(v.Ctype()); typ.IsUntyped() && typ != vt {
-               Fatalf("exporter: untyped type mismatch, have: %v, want: %v", typ, vt)
-       }
+       assertRepresents(typ, v)
        w.typ(typ)
 
        // Each type has only one admissible constant representation,
index f364ed1527fc7525f442fdd8065c5b2b44538103..de516dec69422af8588ea3595f714df71462be27 100644 (file)
@@ -251,6 +251,9 @@ func (n *Node) SetVal(v Val) {
                Dump("have Opt", n)
                Fatalf("have Opt")
        }
+       if n.Op == OLITERAL {
+               assertRepresents(n.Type, v)
+       }
        n.SetHasVal(true)
        n.E = v.U
 }
index 32619b08d1fbe06714b776b0369627c5813ff791..443a3f7827dd3a69069d555c799ca9c144a41d22 100644 (file)
@@ -3624,8 +3624,8 @@ func typecheckdef(n *Node) {
                        e = convlit(e, t)
                }
 
-               n.SetVal(e.Val())
                n.Type = e.Type
+               n.SetVal(e.Val())
 
        case ONAME:
                if n.Name.Param.Ntype != nil {