# Tests that let expressions within disjunctions are uniqued per arc, not just
# per Environment. Without the fix, incorrect results were cached and shared
# across arcs, causing the `all` list to evaluate incorrectly under unification.
# https://cuelang.org/issue/2146
// Issue #2146
//
// Ensure that let expressions are uniqued not only per Environment, but also
// per arc. This is relevant for disjunctions. Without it, an incorrect
// result may be cached for an arbitrary Environment.
-- in.cue --
p1: {
	#A: {
		x?: int
		y?: int

		let list = [x, y]
		all:  [for v in list if v != _|_ {v}]

		*{
			x?: _|_
			y:  1
		} | _
	}

	a: #A & { x: 3 }
	b: #A & a
	@test(eq, {
		#A: *{
			y: 1, all: [1], x?: _|_ @test(err, code=user, contains="explicit error", pos=[9:8])
		} | {
			x?: int, y?: int, all: []
		}
		a: {x: 3, y?: int, all: [3]}
		b: {x: 3, y?: int, all: [3]}
	})
}
p2: {
	#A: {
		x?: int
		y?: int

		let list = [x, y]
		all:  [for v in list if v != _|_ {v}]

		_ | *{
			x?: _|_
			y:  1
		}
	}

	a: #A & { x: 3, y: 2 }
	b: #A & a
	@test(eq, {
		#A: *{
			y: 1, all: [1], x?: _|_ @test(err, code=user, contains="explicit error")
		} | {
			x?: int, y?: int, all: []
		}
		a: {x: 3, y: 2, all: [3, 2]}
		b: {x: 3, y: 2, all: [3, 2]}
	})
}

-- out/eval/stats --
Leaks:  46
Freed:  93
Reused: 76
Allocs: 63
Retain: 0

Unifications: 79
Conjuncts:    157
Disjuncts:    12

NumCloseIDs: 38
-- out/todo.txt --
p1.b and p2.b: v2 uses structure-shared let (list#1multi/list#2multi), v3 evaluates it per arc.
