this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/pkg: make CallCtxt.Decimal support *adt.Vertex

The cue.Value.Decimal method currently makes the assumption that
its function argument is an *adt.Num. This means that the "Validator"
form of the builtin math.MultipleOf(n) will not work, because in that
case the first argument will be an *adt.Vertex.

This changes the explicit interface convertion to instead use the
cue.value.Decimal method, which will work in all cases.

Fixes #2927

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: I35f780efde92a6523548ed04e1e7a6a960b191ce
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1184631
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

authored by

Noam Dolovich and committed by
Daniel Martí
243df639 a1dec7d1

+12 -5
+5 -3
internal/pkg/context.go
··· 18 18 "io" 19 19 "math/big" 20 20 21 + "github.com/cockroachdb/apd/v3" 22 + 21 23 "cuelang.org/go/cue" 22 24 "cuelang.org/go/cue/token" 23 25 "cuelang.org/go/internal/core/adt" 24 26 "cuelang.org/go/internal/value" 25 - "github.com/cockroachdb/apd/v3" 26 27 ) 27 28 28 29 // CallCtxt is passed to builtin implementations that need to use a cue.Value. This is an internal type. Its interface may change. ··· 129 130 130 131 func (c *CallCtxt) Decimal(i int) *apd.Decimal { 131 132 x := value.Make(c.ctx, c.args[i]) 132 - if _, err := x.MantExp(nil); err != nil { 133 + res, err := x.Decimal() 134 + if err != nil { 133 135 c.invalidArgType(c.args[i], i, "Decimal", err) 134 136 return nil 135 137 } 136 - return &c.args[i].(*adt.Num).X 138 + return res 137 139 } 138 140 139 141 func (c *CallCtxt) Float64(i int) float64 {
+7 -2
pkg/math/testdata/round.txtar
··· 11 11 mul4: math.MultipleOf(1, 1) 12 12 mul5: math.MultipleOf(5, 2.5) 13 13 mul6: math.MultipleOf(100e100, 10) 14 + mul7: 10 15 + mul7: math.MultipleOf(2) 16 + mul8: 9 & math.MultipleOf(3) 14 17 15 18 r0: math.Round(2.5) 16 19 r1: math.Round(-2.5) ··· 36 39 mul2: error in call to math.MultipleOf: division by zero: 37 40 ./in.cue:6:7 38 41 floorE1: too many arguments in call to math.Floor (have 2, want 1): 39 - ./in.cue:17:10 42 + ./in.cue:20:10 40 43 floorE2: cannot use "foo" (type string) as number in argument 1 to math.Floor: 41 - ./in.cue:18:21 44 + ./in.cue:21:21 42 45 43 46 Result: 44 47 mul0: true ··· 49 52 mul4: true 50 53 mul5: true 51 54 mul6: true 55 + mul7: 10 56 + mul8: 9 52 57 r0: 3 53 58 r1: -3 54 59 r2: 2