aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cmd/compile/internal/noder/decl.go19
-rw-r--r--test/fixedbugs/issue11945.go4
2 files changed, 22 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/noder/decl.go b/src/cmd/compile/internal/noder/decl.go
index c41b77c100..9862f452fd 100644
--- a/src/cmd/compile/internal/noder/decl.go
+++ b/src/cmd/compile/internal/noder/decl.go
@@ -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))
}
}
diff --git a/test/fixedbugs/issue11945.go b/test/fixedbugs/issue11945.go
index 510b6555c6..218d07a693 100644
--- a/test/fixedbugs/issue11945.go
+++ b/test/fixedbugs/issue11945.go
@@ -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)