this repo has no description
0
fork

Configure Feed

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

internal/core/adt: unify builtin-range matching under MatchBuiltinRange

The previous commit moved the range tables into adt but left two
matchers: adt.MatchBuiltinRange and export.boundSimplifier.matchRange.
Drop the latter so the codebase has a single "match builtin" API.

Now, export calls adt.MatchBuiltinRange on the whole Conjunction;
when it returns a name we emit it directly, and otherwise
boundSimplifier only handles the remaining int/uint + bounds collapse.

This also fixes export to render `int & >=0` as uint,
matching how the debug printer already formats predeclared types.

MatchBuiltinRange is simplified to align with matchRange,
and we drop the now-unnecessary exported tables in the adt package.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ib2ee99a8ebe474993f4f2d3ac0ebee7245e808e7
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1236172
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+23 -75
+2 -2
cmd/cue/cmd/testdata/script/def_jsonschema.txtar
··· 50 50 lastName?: strings.MinRunes(1) 51 51 52 52 // Age in years which must be equal to or greater than zero. 53 - age?: int & >=0 53 + age?: uint 54 54 ... 55 55 } 56 56 -- expect-openonlywhenexplicit-stdout -- ··· 71 71 lastName?: strings.MinRunes(1) 72 72 73 73 // Age in years which must be equal to or greater than zero. 74 - age?: int & >=0 74 + age?: uint 75 75 } 76 76 -- schema.json -- 77 77 {
+11 -20
internal/core/adt/builtinrange.go
··· 16 16 17 17 import "github.com/cockroachdb/apd/v3" 18 18 19 - // BuiltinRange describes a predeclared ranged numeric type such as int16 or 20 - // float32. 21 - type BuiltinRange struct { 22 - Name string 23 - Lo *apd.Decimal 24 - Hi *apd.Decimal 19 + type builtinRange struct { 20 + name string 21 + lo, hi *apd.Decimal 25 22 } 26 23 27 24 func mustDec(s string) *apd.Decimal { ··· 32 29 return d 33 30 } 34 31 35 - // IntBuiltinRanges lists the predeclared sized integer types. 36 - var IntBuiltinRanges = []BuiltinRange{ 32 + var intBuiltinRanges = []builtinRange{ 37 33 {"int8", mustDec("-128"), mustDec("127")}, 38 34 {"int16", mustDec("-32768"), mustDec("32767")}, 39 35 {"int32", mustDec("-2147483648"), mustDec("2147483647")}, ··· 49 45 {"uint128", mustDec("0"), mustDec("340282366920938463463374607431768211455")}, 50 46 } 51 47 52 - // FloatBuiltinRanges lists the predeclared sized float types. 53 - var FloatBuiltinRanges = []BuiltinRange{ 48 + var floatBuiltinRanges = []builtinRange{ 54 49 // 2**127 * (2**24 - 1) / 2**23 55 50 {"float32", 56 51 mustDec("-3.40282346638528859811704183484516925440e+38"), ··· 109 104 } 110 105 } 111 106 112 - if lo != nil && hi == nil && hasInt && lo.X.Sign() == 0 { 107 + if lo != nil && hi == nil && hasInt && lo.X.IsZero() { 113 108 return "uint" 114 109 } 115 110 if lo == nil || hi == nil { 116 111 return "" 117 112 } 118 - 119 - var ranges []BuiltinRange 120 - switch { 121 - case hasInt: 122 - ranges = IntBuiltinRanges 123 - default: 124 - ranges = FloatBuiltinRanges 113 + ranges := floatBuiltinRanges 114 + if hasInt { 115 + ranges = intBuiltinRanges 125 116 } 126 117 for _, r := range ranges { 127 - if lo.X.Cmp(r.Lo) == 0 && hi.X.Cmp(r.Hi) == 0 { 128 - return r.Name 118 + if lo.X.Cmp(r.lo) == 0 && hi.X.Cmp(r.hi) == 0 { 119 + return r.name 129 120 } 130 121 } 131 122 return ""
+3 -52
internal/core/export/bounds.go
··· 19 19 "cuelang.org/go/internal/core/adt" 20 20 ) 21 21 22 - // boundSimplifier simplifies bound values into predeclared identifiers, if 23 - // possible. 22 + // boundSimplifier collapses BasicType and BoundValue conjuncts into a compact 23 + // int/uint prefix plus the remaining bounds. 24 24 type boundSimplifier struct { 25 25 e *exporter 26 26 ··· 90 90 if s.min == nil || s.max == nil { 91 91 return nil 92 92 } 93 - switch { 94 - case s.isInt: 95 - t := s.matchRange(adt.IntBuiltinRanges) 96 - if t != "" { 97 - e = ast.NewIdent(t) 98 - break 99 - } 93 + if s.isInt { 100 94 if sign := s.minNum.X.Sign(); sign == -1 { 101 95 e = ast.NewIdent("int") 102 - 103 96 } else { 104 97 e = ast.NewIdent("uint") 105 98 if sign == 0 && s.min.Op == adt.GreaterEqualOp { 106 99 s.min = nil 107 - break 108 100 } 109 101 } 110 - fallthrough 111 - default: 112 - t := s.matchRange(adt.FloatBuiltinRanges) 113 - if t != "" { 114 - e = wrapBin(e, ast.NewIdent(t), adt.AndOp) 115 - } 116 102 } 117 - 118 103 if s.min != nil { 119 104 e = wrapBin(e, s.e.expr(nil, s.min), adt.AndOp) 120 105 } ··· 122 107 e = wrapBin(e, s.e.expr(nil, s.max), adt.AndOp) 123 108 } 124 109 return e 125 - } 126 - 127 - func (s *boundSimplifier) matchRange(ranges []adt.BuiltinRange) (t string) { 128 - for _, r := range ranges { 129 - if !s.minNum.X.IsZero() && s.min.Op == adt.GreaterEqualOp && s.minNum.X.Cmp(r.Lo) == 0 { 130 - switch s.maxNum.X.Cmp(r.Hi) { 131 - case 0: 132 - if s.max.Op == adt.LessEqualOp { 133 - s.max = nil 134 - } 135 - s.min = nil 136 - return r.Name 137 - case -1: 138 - if !s.minNum.X.IsZero() { 139 - s.min = nil 140 - return r.Name 141 - } 142 - case 1: 143 - } 144 - } else if s.max.Op == adt.LessEqualOp && s.maxNum.X.Cmp(r.Hi) == 0 { 145 - switch s.minNum.X.Cmp(r.Lo) { 146 - case -1: 147 - case 0: 148 - if s.min.Op == adt.GreaterEqualOp { 149 - s.min = nil 150 - } 151 - fallthrough 152 - case 1: 153 - s.max = nil 154 - return r.Name 155 - } 156 - } 157 - } 158 - return "" 159 110 } 160 111 161 112 func wrapBin(a, b ast.Expr, op adt.Op) ast.Expr {
+1 -1
internal/core/export/testdata/main/adt.txtar
··· 198 198 e7: !t || !false 199 199 e8?: !false 200 200 m1: { 201 - [string]: int & >=0 201 + [string]: uint 202 202 } & { 203 203 { 204 204 [string]: int
+6
internal/core/export/value.go
··· 192 192 return e.bareValue(x.Values[0]) 193 193 } 194 194 195 + if e.cfg.Simplify { 196 + if name := adt.MatchBuiltinRange(x); name != "" { 197 + return ast.NewIdent(name) 198 + } 199 + } 200 + 195 201 a := []adt.Value{} 196 202 b := boundSimplifier{e: e} 197 203 for _, v := range x.Values {