Skip to content

Commit e97c624

Browse files
committed
internal/core/adt: ensure pattern is evaluated
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 <[email protected]> Change-Id: I13e89de6a1fe25ad4161c61a0e450360981f2f0e Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200588 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Matthew Sackman <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent aeb3bf3 commit e97c624

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

Diff for: cue/testdata/builtins/all.txtar

+45-7
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@ okIncompleteChild: {
1616
b: {}
1717
x: and([{a: b.c}, {b: 1}])
1818
}
19+
-- issue3412.cue --
20+
andInPattern: {
21+
out: {
22+
[and(#constraints)]: _
23+
someKey: {}
24+
}
25+
#constraints: [=~"^.*$"]
26+
}
1927
-- out/eval/stats --
2028
Leaks: 2
21-
Freed: 12
22-
Reused: 8
23-
Allocs: 6
24-
Retain: 2
29+
Freed: 18
30+
Reused: 12
31+
Allocs: 8
32+
Retain: 3
2533

26-
Unifications: 14
27-
Conjuncts: 17
28-
Disjuncts: 14
34+
Unifications: 20
35+
Conjuncts: 27
36+
Disjuncts: 21
2937
-- out/evalalpha --
3038
Errors:
3139
fatalArg.x: invalid operands "eee" and 'eee' to '+' (type string and bytes):
@@ -65,6 +73,15 @@ Result:
6573
b: (int){ 1 }
6674
}
6775
}
76+
andInPattern: (struct){
77+
out: (struct){
78+
someKey: (struct){
79+
}
80+
}
81+
#constraints: (#list){
82+
0: (string){ =~"^.*$" }
83+
}
84+
}
6885
}
6986
-- diff/-out/evalalpha<==>+out/eval --
7087
diff old new
@@ -137,6 +154,15 @@ Result:
137154
b: (int){ 1 }
138155
}
139156
}
157+
andInPattern: (struct){
158+
out: (struct){
159+
someKey: (struct){
160+
}
161+
}
162+
#constraints: (#list){
163+
0: (string){ =~"^.*$" }
164+
}
165+
}
140166
}
141167
-- out/compile --
142168
--- in.cue
@@ -164,3 +190,15 @@ Result:
164190
])
165191
}
166192
}
193+
--- issue3412.cue
194+
{
195+
andInPattern: {
196+
out: {
197+
[and(〈1;#constraints〉)]: _
198+
someKey: {}
199+
}
200+
#constraints: [
201+
=~"^.*$",
202+
]
203+
}
204+
}

Diff for: internal/core/adt/composite.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,12 @@ func (v *Vertex) updateArcType(t ArcType) {
328328
if (s != nil || v.isFinal()) && s.ctx.isDevVersion() {
329329
c := s.ctx
330330
if s.scheduler.frozen.meets(arcTypeKnown) {
331+
p := token.NoPos
332+
if src := c.Source(); src != nil {
333+
p = src.Pos()
334+
}
331335
parent := v.Parent
332-
parent.reportFieldCycleError(c, c.Source().Pos(), v.Label)
336+
parent.reportFieldCycleError(c, p, v.Label)
333337
return
334338
}
335339
}

Diff for: internal/core/adt/constraints.go

+3
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func matchPattern(ctx *OpContext, pattern Value, f Feature) bool {
134134
// This is an optimization an intended to be faster than regular CUE evaluation
135135
// for the majority of cases where pattern constraints are used.
136136
func matchPatternValue(ctx *OpContext, pattern Value, f Feature, label Value) (result bool) {
137+
if v, ok := pattern.(*Vertex); ok {
138+
v.unify(ctx, scalarKnown, finalize)
139+
}
137140
pattern = Unwrap(pattern)
138141
label = Unwrap(label)
139142

0 commit comments

Comments
 (0)