-- in.cue --
-- comprehensions.cue --
// Panic "vertex not initialized" in v0.14.0+ with recursive comprehensions
// referencing a field being defined, particularly across subdirectories.
// https://cuelang.org/issue/4119
issue4119: reduced: {
	x: [...]
	x: _ | *[{f: 123}]
	x: [
		for i in x {
			f: i.f
		}
	]
	@test(eq, {x: *[{
		f: 123
	}] | []})
}
// This test is added to test the memory management bug isn't triggered
// one level deeper. This test is expected to fail, as it now triggers
// a structural cycle in the right disjunct, leaving an incomplete error
// on the left disjunct.
issue4119: reducedNested: {
	x: {y: [...]}
	x: {y: _} | *{y: [{f: 123}]}
	x: {y: [
		for i in x {
			f: i.f
		}
	]}
	@test(err, code=eval, path=x.y.0.f, contains="undefined field: %s", args=[f], pos=[5:9])
}
issue4119: full: {
	_img: img={
		val: string
		accounts: {
			groups: img.accounts.groups | *[{gid: 65532}]

			// Recursive comprehension - this triggers the bug
			groups: [
				for g in groups
				let _gid = g.gid {
					gid: [
						if _gid != _|_ {_gid},
						65532,
					][0]
				},
			]
		}
	}

	art: {
		images: [string]: #Image
		for k, v in images {
			images: (k): _img & v
		}
	}
	#Image: {
		val: string
		accounts?: {
			groups?: [...]
		}
	}
	art: images: ko: {
		val: "test"
	}
	@test(eq, {
		_img: {
			val: string
			accounts: {groups: [{ gid: 65532 }]}
		}
		art: images: ko: {
			val: "test"
			accounts: {groups: *[{ gid: 65532 }] | []}
		}
		#Image: {val: string, accounts?: groups?: []}
	})
}
-- out/errors.txt --
[eval] issue4119.reducedNested.x.y.0.f: undefined field: f:
    ./comprehensions.cue:25:9
-- out/eval/stats --
Leaks:  7
Freed:  48
Reused: 28
Allocs: 27
Retain: 0

Unifications: 38
Conjuncts:    88
Disjuncts:    8

NumCloseIDs: 9
