Skip to content

Commit

Permalink
internal/core/convert: fix conversion from float
Browse files Browse the repository at this point in the history
Conversion from Go types big.Float, float32 and
float64 always added an error to the OpContext even
when no error had occured.

Fixes #749

Closes #761
cuelang/cue#761

GitOrigin-RevId: 3476e9fc96bde56801c1cadda0004c529e70af39
Change-Id: I253846087ddaf5dbfd9bd2ade4cf917101e482e5
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8721
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
antong authored and mpvl committed Feb 13, 2021
1 parent 89cada6 commit b20ac5a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
13 changes: 13 additions & 0 deletions cmd/cue/cmd/testdata/script/issue749.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cue eval ./float.cue
cmp stderr expect-stderr
cmp stdout expect-stdout
-- float.cue --
import "strconv"

x : strconv.ParseFloat("3.14", 32)
y : strconv.ParseFloat("3.14", 64)

-- expect-stderr --
-- expect-stdout --
x: 3.140000104904175
y: 3.14
32 changes: 32 additions & 0 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,38 @@ func TestFill2(t *testing.T) {
}
}

func TestFillFloat(t *testing.T) {
// This tests panics for issue #749

want := `{
x: 3.14
}`

filltest := func(x interface{}) {
r := &Runtime{}
i, err := r.Compile("test", `
x: number
`)
if err != nil {
t.Fatal(err)
}

i, err = i.Fill(x, "x")
if err != nil {
t.Fatal(err)
}

got := fmt.Sprint(i.Value())
if got != want {
t.Errorf("got: %s\nwant: %s", got, want)
}
}

filltest(float32(3.14))
filltest(float64(3.14))
filltest(big.NewFloat(3.14))
}

func TestValue_LookupDef(t *testing.T) {
r := &Runtime{}

Expand Down
16 changes: 11 additions & 5 deletions internal/core/convert/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
case *big.Float:
n := &adt.Num{Src: src, K: adt.FloatKind}
_, _, err := n.X.SetString(v.String())
ctx.AddErr(errors.Promote(err, "invalid float"))
if err != nil {
return ctx.AddErr(errors.Promote(err, "invalid float"))
}
return n

case *apd.Decimal:
Expand Down Expand Up @@ -355,13 +357,17 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
return toUint(ctx, uint64(v))
case float64:
n := &adt.Num{Src: src, K: adt.FloatKind}
_, _, err := n.X.SetString(fmt.Sprintf("%g", v))
ctx.AddErr(errors.Promote(err, "invalid float"))
_, _, err := n.X.SetString(fmt.Sprint(v))
if err != nil {
return ctx.AddErr(errors.Promote(err, "invalid float"))
}
return n
case float32:
n := &adt.Num{Src: src, K: adt.FloatKind}
_, _, err := n.X.SetString(fmt.Sprintf("%g", v))
ctx.AddErr(errors.Promote(err, "invalid float"))
_, _, err := n.X.SetString(fmt.Sprint(v))
if err != nil {
return ctx.AddErr(errors.Promote(err, "invalid float"))
}
return n

case reflect.Value:
Expand Down

0 comments on commit b20ac5a

Please sign in to comment.