Skip to content

Commit 8e2ad07

Browse files
committed
tools/fix: add test for selector, call, and default embedding flags
The explicitopen fix classifies embedded expressions in two places: openEmbedExpr for whole embeddings and collectEmbedFlags for operands of conjunctions and disjunctions as well as comprehension field values. The latter misses selectors, index expressions, and()/or() calls, and defaulted operands (*X), all of which may resolve to closed structs. As a result, embedding a conjunction with a selector operand or a disjunction with a defaulted definition operand adds no wrapper to the enclosing struct, and comprehension field values of those shapes are not opened, changing behavior in both cases. The tests document the current, broken output; TODOs mark the intended rewrites. Signed-off-by: Daniel Martí <mvdan@mvdan.cc> Change-Id: Ib85689a441740cf4b61fe301e3df312e10a33192 Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1242611 Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org> Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
1 parent 486f8a3 commit 8e2ad07

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

tools/fix/fix_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,129 @@ package foo
206206
`,
207207
},
208208

209+
{
210+
// Selectors may resolve to closed values just like plain
211+
// references, so a conjunction with a selector operand needs
212+
// a runtime __reclose check on the enclosing struct.
213+
// TODO: the enclosing struct is left unwrapped; it should be
214+
// wrapped in __reclose to preserve the old behavior.
215+
name: "reclose embeddings of selector conjunctions (fixExplicitOpen)",
216+
exps: []string{"explicitopen"},
217+
in: `package foo
218+
219+
#A: a: int
220+
h: inner: #A
221+
222+
v: {
223+
h.inner & {a: 1}
224+
extra: 2
225+
}
226+
`,
227+
out: `@experiment(explicitopen)
228+
229+
package foo
230+
231+
#A: a: int
232+
h: inner: #A
233+
234+
v: {
235+
(h.inner & {a: 1})...
236+
extra: 2
237+
}
238+
`,
239+
},
240+
241+
{
242+
// Comprehension field values that may resolve to closed
243+
// structs via a selector conjunction or an and() call must
244+
// be opened like plain references.
245+
// TODO: the field values are left untouched; they should
246+
// become "(lib.v & {hc: port: 1})..." and "and([lib.v])..."
247+
// to preserve the old behavior.
248+
name: "open selector and call field values in comprehensions (fixExplicitOpen)",
249+
exps: []string{"explicitopen"},
250+
in: `package foo
251+
252+
#HC: hc: {port: 1}
253+
lib: v: #HC
254+
255+
#Service: {
256+
enable: bool
257+
egress?: [string]: {...}
258+
if enable {
259+
egress: lib.v & {hc: port: 1}
260+
}
261+
if enable {
262+
egress2: and([lib.v])
263+
}
264+
}
265+
`,
266+
out: `package foo
267+
268+
#HC: hc: {port: 1}
269+
lib: v: #HC
270+
271+
#Service: {
272+
enable: bool
273+
egress?: [string]: {...}
274+
if enable {
275+
egress: lib.v & {hc: port: 1}
276+
}
277+
if enable {
278+
egress2: and([lib.v])
279+
}
280+
}
281+
`,
282+
},
283+
284+
{
285+
// The default marker *X takes on X's closedness: a disjunction
286+
// with a defaulted definition operand needs a runtime __reclose
287+
// check when embedded, and must be opened as a comprehension
288+
// field value.
289+
// TODO: the enclosing struct is left unwrapped and the field
290+
// value untouched; they should get __reclose and "..." to
291+
// preserve the old behavior.
292+
name: "default marker embedding flags (fixExplicitOpen)",
293+
exps: []string{"explicitopen"},
294+
in: `package foo
295+
296+
#A: {a: int}
297+
298+
v: {
299+
*#A | {}
300+
extra: 1
301+
}
302+
303+
#S: {
304+
enable: bool
305+
x?: {...}
306+
if enable {
307+
x: *#A | {}
308+
}
309+
}
310+
`,
311+
out: `@experiment(explicitopen)
312+
313+
package foo
314+
315+
#A: {a: int}
316+
317+
v: {
318+
(*#A | {})...
319+
extra: 1
320+
}
321+
322+
#S: {
323+
enable: bool
324+
x?: {...}
325+
if enable {
326+
x: *#A | {}
327+
}
328+
}
329+
`,
330+
},
331+
209332
{
210333
// The old comprehension opening also overrides an explicit
211334
// close() in a field value: sibling entries added elsewhere

0 commit comments

Comments
 (0)