]> Cypherpunks repositories - gostls13.git/commitdiff
go/constant: make constant.Make produce "smallest" const representation
authorRobert Griesemer <gri@golang.org>
Wed, 25 Nov 2020 01:26:22 +0000 (17:26 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 25 Nov 2020 02:18:15 +0000 (02:18 +0000)
Fixes #42640.

Change-Id: I22b8142b0a47a0f957d1bda28cdfdbb8388cffc4
Reviewed-on: https://go-review.googlesource.com/c/go/+/273086
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/go/constant/value.go
src/go/constant/value_test.go

index 116c7575d9eeb1dd595805901b4434f7baef5969..4baae2eb327fdba9e5b6ce2bff1b95ae4011a4f6 100644 (file)
@@ -594,11 +594,11 @@ func Make(x interface{}) Value {
        case int64:
                return int64Val(x)
        case *big.Int:
-               return intVal{x}
+               return makeInt(x)
        case *big.Rat:
-               return ratVal{x}
+               return makeRat(x)
        case *big.Float:
-               return floatVal{x}
+               return makeFloat(x)
        default:
                return unknownVal{}
        }
index 1a5025cbbd4920b5115dfbeec1ec3b5f0417074b..5edc766fde85df201bf4b8c7f3894ed144f647a2 100644 (file)
@@ -620,18 +620,32 @@ func TestUnknown(t *testing.T) {
        }
 }
 
+type makeTestCase struct {
+       kind      Kind
+       arg, want interface{}
+}
+
+func dup(k Kind, x interface{}) makeTestCase { return makeTestCase{k, x, x} }
+
 func TestMake(t *testing.T) {
-       for _, want := range []interface{}{
-               false,
-               "hello",
-               int64(1),
-               big.NewInt(10),
-               big.NewFloat(2.0),
-               big.NewRat(1, 3),
+       for _, test := range []makeTestCase{
+               {Bool, false, false},
+               {String, "hello", "hello"},
+
+               {Int, int64(1), int64(1)},
+               {Int, big.NewInt(10), int64(10)},
+               {Int, new(big.Int).Lsh(big.NewInt(1), 62), int64(1 << 62)},
+               dup(Int, new(big.Int).Lsh(big.NewInt(1), 63)),
+
+               {Float, big.NewFloat(0), floatVal0.val},
+               dup(Float, big.NewFloat(2.0)),
+               dup(Float, big.NewRat(1, 3)),
        } {
-               got := Val(Make(want))
-               if got != want {
-                       t.Errorf("got %v; want %v", got, want)
+               val := Make(test.arg)
+               got := Val(val)
+               if val.Kind() != test.kind || got != test.want {
+                       t.Errorf("got %v (%T, kind = %d); want %v (%T, kind = %d)",
+                               got, got, val.Kind(), test.want, test.want, test.kind)
                }
        }
 }