# Tests that nested comprehensions with let bindings across multiple files work.
# Issue #3763: for comprehension with multi-level let bindings and pattern constraints.
-- reduced.cue --
reduced: {
	A: x1: y?: int // needs to be optional
	A: x2: y?: int
	A: x3: y?: int

	if true {
		let Z = [for _, x in A {x}]
		B: [for z in Z if z.y != _|_ { b: 1 }]
	}
	@test(eq, {
		A: {
			x1: {y?: int}
			x2: {y?: int}
			x3: {y?: int}
		}
		B: []
	})
}
-- full.cue --
full: {
	_core_v1: {
		#cp: {
			hostPort?: int32
			cp:        int32
		}
	}

	@test(shareID=L, at=k.n.n1._cs.l)
	k: n: n1: {
		_CL: "l"
	}

	let nc = {
		_ports: [ID=_]: _core_v1.#cp
	}

	_cl: [ID=_]: nc & {
		_ports: {
			"1": {}
			"2": {}
			"3": {}
		}
	}

	k: n: [ID=_]: {
		_CL: string
		_cs: (_CL): _cl[(_CL)]
	}

	for _, n in k.n {
		let cps = [for _, c in n._cs for _, cps in c._ports {cps}]
		ports: [for cp in cps if cp.hostPort != _|_ {
			port: "\(cp.hostPort)"
		}]
	}

	_cl: l: {} @test(shareID=L)
	@test(eq, {
		_core_v1: #cp: {
			hostPort?: >=-2147483648 & <=2147483647 & int
			cp:        >=-2147483648 & <=2147483647 & int
		}
		k: n: n1: {
			_CL: "l"
			_cs: l: _ports: {
				"1": {
					hostPort?: >=-2147483648 & <=2147483647 & int
					cp:        >=-2147483648 & <=2147483647 & int
				}
				"2": {
					hostPort?: >=-2147483648 & <=2147483647 & int
					cp:        >=-2147483648 & <=2147483647 & int
				}
				"3": {
					hostPort?: >=-2147483648 & <=2147483647 & int
					cp:        >=-2147483648 & <=2147483647 & int
				}
			}
		}
		_cl: l: _ports: {
			"1": {
				hostPort?: >=-2147483648 & <=2147483647 & int
				cp:        >=-2147483648 & <=2147483647 & int
			}
			"2": {
				hostPort?: >=-2147483648 & <=2147483647 & int
				cp:        >=-2147483648 & <=2147483647 & int
			}
			"3": {
				hostPort?: >=-2147483648 & <=2147483647 & int
				cp:        >=-2147483648 & <=2147483647 & int
			}
		}
		ports: []
	})
}
-- out/todo.txt --
full.k.n.n1._cs.l: v3 uses structure sharing (~(full._cl.l)) where v2 expands
the full _ports struct inline.
reduced.let Z: v3 shows a multi-comprehension (let Z#3multi) where v2 shows a
concrete list.
-- out/eval/stats --
Leaks:  19
Freed:  53
Reused: 48
Allocs: 24
Retain: 0

Unifications: 68
Conjuncts:    126
Disjuncts:    0

NumCloseIDs: 38
