Skip to content

Commit

Permalink
internal/core/adt: ensure pattern is evaluated
Browse files Browse the repository at this point in the history
The old evaluator handled patterns differently. Ensure
patterns are evaluated before used.

This also fixes a case where a nil pointer was referenced
when computing an error position.

Fixes #3412

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I13e89de6a1fe25ad4161c61a0e450360981f2f0e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200588
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Matthew Sackman <matthew@cue.works>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
mpvl committed Sep 5, 2024
1 parent aeb3bf3 commit e97c624
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
52 changes: 45 additions & 7 deletions cue/testdata/builtins/all.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ okIncompleteChild: {
b: {}
x: and([{a: b.c}, {b: 1}])
}
-- issue3412.cue --
andInPattern: {
out: {
[and(#constraints)]: _
someKey: {}
}
#constraints: [=~"^.*$"]
}
-- out/eval/stats --
Leaks: 2
Freed: 12
Reused: 8
Allocs: 6
Retain: 2
Freed: 18
Reused: 12
Allocs: 8
Retain: 3

Unifications: 14
Conjuncts: 17
Disjuncts: 14
Unifications: 20
Conjuncts: 27
Disjuncts: 21
-- out/evalalpha --
Errors:
fatalArg.x: invalid operands "eee" and 'eee' to '+' (type string and bytes):
Expand Down Expand Up @@ -65,6 +73,15 @@ Result:
b: (int){ 1 }
}
}
andInPattern: (struct){
out: (struct){
someKey: (struct){
}
}
#constraints: (#list){
0: (string){ =~"^.*$" }
}
}
}
-- diff/-out/evalalpha<==>+out/eval --
diff old new
Expand Down Expand Up @@ -137,6 +154,15 @@ Result:
b: (int){ 1 }
}
}
andInPattern: (struct){
out: (struct){
someKey: (struct){
}
}
#constraints: (#list){
0: (string){ =~"^.*$" }
}
}
}
-- out/compile --
--- in.cue
Expand Down Expand Up @@ -164,3 +190,15 @@ Result:
])
}
}
--- issue3412.cue
{
andInPattern: {
out: {
[and(〈1;#constraints〉)]: _
someKey: {}
}
#constraints: [
=~"^.*$",
]
}
}
6 changes: 5 additions & 1 deletion internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,12 @@ func (v *Vertex) updateArcType(t ArcType) {
if (s != nil || v.isFinal()) && s.ctx.isDevVersion() {
c := s.ctx
if s.scheduler.frozen.meets(arcTypeKnown) {
p := token.NoPos
if src := c.Source(); src != nil {
p = src.Pos()
}
parent := v.Parent
parent.reportFieldCycleError(c, c.Source().Pos(), v.Label)
parent.reportFieldCycleError(c, p, v.Label)
return
}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/core/adt/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func matchPattern(ctx *OpContext, pattern Value, f Feature) bool {
// This is an optimization an intended to be faster than regular CUE evaluation
// for the majority of cases where pattern constraints are used.
func matchPatternValue(ctx *OpContext, pattern Value, f Feature, label Value) (result bool) {
if v, ok := pattern.(*Vertex); ok {
v.unify(ctx, scalarKnown, finalize)
}
pattern = Unwrap(pattern)
label = Unwrap(label)

Expand Down

0 comments on commit e97c624

Please sign in to comment.