Skip to content

Commit

Permalink
internal/core/adt: fix matchPattern
Browse files Browse the repository at this point in the history
matchPattern did not handle the case where Feature f
was `AnyString`. It now does. This also means that
more places in the code should check for label != nil,
as it is now possible for it to be nil even if kind is string.

Issue #3060

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I3a814b92b0d28dd8408a95beff329042938f7192
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194051
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
mpvl committed Apr 29, 2024
1 parent 9cf30f1 commit 72ba528
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,7 @@ func TestElem(t *testing.T) {
value string
path []string
want string
skip bool
}{{
value: `
a: [...int]
Expand Down Expand Up @@ -1941,10 +1942,13 @@ func TestElem(t *testing.T) {
`,
path: []string{"a", "foo", "b", ""},
want: "{\n\tc: \"foo\" + string\n\td: string\n}",
skip: true, // TODO(p3): Skip because this is just a reordering.
}}
for _, tc := range testCases {
runMatrix(t, "", func(t *testing.T, cfg *evalConfig) {
TODO_V3(t, cfg)
if tc.skip {
TODO_V3(t, cfg)
}

v := cfg.getValue(t, tc.value)
v.v.Finalize(v.ctx()) // TODO: do in instance.
Expand Down
12 changes: 6 additions & 6 deletions internal/core/adt/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ func matchPattern(ctx *OpContext, pattern Value, f Feature) bool {
// avoid many bound checks), which we probably should. Especially when we
// allow list constraints, like [<10]: T.
var label Value
if int64(f.Index()) == MaxIndex {
f = 0
} else if f.IsString() {
if f.IsString() && int64(f.Index()) != MaxIndex {
label = f.ToValue(ctx)
}

Expand All @@ -144,8 +142,8 @@ func matchPatternValue(ctx *OpContext, pattern Value, f Feature, label Value) (r
}

k := IntKind
if label != nil {
k = label.Kind()
if f.IsString() {
k = StringKind
}
if !k.IsAnyOf(pattern.Kind()) {
return false
Expand Down Expand Up @@ -173,7 +171,6 @@ func matchPatternValue(ctx *OpContext, pattern Value, f Feature, label Value) (r
return true

case *BasicType:
k := label.Kind()
return x.K&k == k

case *BoundValue:
Expand All @@ -198,6 +195,9 @@ func matchPatternValue(ctx *OpContext, pattern Value, f Feature, label Value) (r
return err == nil && xi == yi

case *String:
if label == nil {
return false
}
y, ok := label.(*String)
return ok && x.Str == y.Str

Expand Down

0 comments on commit 72ba528

Please sign in to comment.