-- in.cue --
dynamicTop: {
	#D: {
		for k, v in {foo: 123} {(k): v}
		@test(eq, {foo: 123})
	}
}

// Issue #1404
dynamicDepth1: {
	#D: {
		a: foo: 123
		b: {for k, v in a {(k): v}}
		@test(eq, {a: {foo: 123}, b: {foo: 123}})
	}
}

allowed: {
	#D: {
		if true {
			d: int
		}
	}

	vErr: #D & {d: 5}
	@test(eq, {#D: {d: int}, vErr: {d: 5}})
}

disallowed: {
	#D: {
		if false {
			d: int
		}
	}

	vErr: #D & {d: 5}
	@test(err, code=eval, at=vErr.d, contains="field not allowed", pos=[7:14])
}

// Interpret comprehensions as embeddings.
// Issue #1956
comprehensionIsEmbed: {
	#A: size: int | *1
	#B: {
		kind: string

		// Should be identical to just #A. That is, #A is treated as open.
		if true {
			#A
		}
	}
	x: #B & {
		kind: "A"
	}
	@test(eq, {
		#A: {size: *1 | int}
		#B: {kind: string, size: *1 | int}
		x: {kind: "A", size: *1 | int}
	})
}

// This comprehension is inside a definition and should still disallow b.
noEraseDefinition: {
	#Foo: [if true {a: int}]
	a: { #Foo & [{ b: 2 }] } // Error
	@test(err, code=eval, at=a.0.b, contains="field not allowed", pos=[2:17])
}

// issue1956: Tests that conditional comprehensions correctly embed definitions
// inside a closed struct without spuriously rejecting fields.
// https://cuelang.org/issue/1956
issue1956: {
	#Details: {
		size: int | *1
	}

	#Thing: {
		kind: "KindA" | "KindB"
		if kind == "KindA" {
			#Details
		}
	}

	x: #Thing & {
		kind: "KindA"
	}
	@test(eq, {
		#Details: {size: *1 | int}
		#Thing: {kind: "KindA" | "KindB"}
		x: {kind: "KindA", size: *1 | int}
	})
}

// This triggers adding the same "closedInfo" twice, potentially causing
// a cyclic linked list in the todo list.
dedupTodo: {
	#sub: {
		c: _
		if c.n == "c" {
			X: test: c
		}
	}

	out: #sub // needs to be definition
	out: {
		c: n: "c" // Do not combine with &, as the original issue relies on this.
		c: n: string
	}
	@test(eq, {
		#sub: {c: _}
		out: {c: {n: "c"}, X: {test: {n: "c"}}}
	})
}

-- v3issues.cue --
// issue3483: Tests that a falsy if comprehension inside a definition does not
// incorrectly produce "field not allowed" in evalv3.
// https://cuelang.org/issue/3483
issue3483: {
	#Schema: schemaField: int
	out1: out2: #Schema & {
		if false {
			schemaField: 3
		}
	}
	@test(eq, {
		#Schema: {schemaField: int}
		out1: {out2: {schemaField: int}}
	})
}
// issue3486: Tests that embedding a definition inside an if comprehension does
// not produce spurious "field not allowed" errors.
// https://cuelang.org/issue/3486
issue3486: {
	#schema: {}

	if true {
		out: {
			#schema
			// This field should be allowed as #schema is embedded.
			extra: "foo"
		}
	}
	@test(eq, {#schema: {}, out: {extra: "foo"}})
}
-- out/errors.txt --
[eval] disallowed.vErr.d: field not allowed:
    ./in.cue:35:14
[eval] noEraseDefinition.a.0.b: field not allowed:
    ./in.cue:64:17
[incomplete] issue1956.#Thing: unresolved disjunction "KindA" | "KindB" (type string):
    ./in.cue:78:6
[incomplete] dedupTodo.#sub: undefined field: n:
    ./in.cue:98:8
-- out/eval/stats --
Leaks:  4
Freed:  77
Reused: 61
Allocs: 20
Retain: 0

Unifications: 67
Conjuncts:    125
Disjuncts:    14

NumCloseIDs: 46
