]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile: force untyped constants from types2 to expected kind
authorMatthew Dempsky <mdempsky@google.com>
Tue, 26 Jan 2021 01:26:07 +0000 (17:26 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 26 Jan 2021 01:50:30 +0000 (01:50 +0000)
Currently, types2 sometimes produces constant.Values with a Kind
different than the untyped constant type's Is{Integer,Float,Complex}
info, which irgen expects to always match.

While we mull how best to proceed in #43891, this CL adapts irgen to
types2's current behavior. In particular, fixedbugs/issue11945.go now
passes with -G=3.

Updates #43891.

Change-Id: I24823a32ff49af6045a032d3903dbb55cbec6bef
Reviewed-on: https://go-review.googlesource.com/c/go/+/286652
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Trust: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/noder/decl.go
test/fixedbugs/issue11945.go

index c41b77c1008c138ab66ede48e0f171030addae7a..9862f452fdcfe7e3ae465842f5f4a7ea86fe1aa5 100644 (file)
@@ -5,6 +5,8 @@
 package noder
 
 import (
+       "go/constant"
+
        "cmd/compile/internal/base"
        "cmd/compile/internal/ir"
        "cmd/compile/internal/syntax"
@@ -58,7 +60,22 @@ func (g *irgen) constDecl(out *ir.Nodes, decl *syntax.ConstDecl) {
 
        for _, name := range decl.NameList {
                name, obj := g.def(name)
-               name.SetVal(obj.(*types2.Const).Val())
+
+               // For untyped numeric constants, make sure the value
+               // representation matches what the rest of the
+               // compiler (really just iexport) expects.
+               // TODO(mdempsky): Revisit after #43891 is resolved.
+               val := obj.(*types2.Const).Val()
+               switch name.Type() {
+               case types.UntypedInt, types.UntypedRune:
+                       val = constant.ToInt(val)
+               case types.UntypedFloat:
+                       val = constant.ToFloat(val)
+               case types.UntypedComplex:
+                       val = constant.ToComplex(val)
+               }
+               name.SetVal(val)
+
                out.Append(ir.NewDecl(g.pos(decl), ir.ODCLCONST, name))
        }
 }
index 510b6555c653cce4bd429323caa08c1794c47778..218d07a6939db25867d738c1b7ac5d0c82e9a94f 100644 (file)
@@ -13,6 +13,10 @@ const (
        _ = real(0) // from bug report
        _ = imag(0) // from bug report
 
+       // same as above, but exported for #43891
+       Real0 = real(0)
+       Imag0 = imag(0)
+
        // if the arguments are untyped, the results must be untyped
        // (and compatible with types that can represent the values)
        _ int = real(1)