# Tests that mixing struct fields and embedded scalars in a comprehension body
# correctly reports an error. Issue #2481: nested for/if/let comprehensions
# triggered a spurious "circular dependency" error in the evaluator.
# https://cuelang.org/issue/2481
// Mixing struct fields and embedded scalars should error. Using comprehensions
// this follows a different path which may miss the error.
// TODO: fix
-- in.cue --
// Mixing struct fields and embedded scalars should error. Using comprehensions
// this follows a different path which may miss the error.
// TODO: fix
out: {
	for k in keys {
		if values[k] != _|_  {
			val: k
			if val != _|_ {
				k
			}
		}
	}
	@test(err, code=eval, hint="string with struct")
}

// multiple lets are mapped to the same location and will conflict. This is
// fine, though, as lets do not represent a single data field. This should
// therefore not error.
// TODO: fix
multiLets: {
	for k in keys {
		if values[k] != _|_  {
			let val = k
			if val != _|_ {
				k
			}
		}
	}
	@test(eq, "b")
}

keys: [
	// order matters
	"a",
	"b",
] @test(eq, ["a", "b"])

values: {
	// order does not matter
	"b": "member"
	"c": "foo"
	@test(eq, {b: "member", c: "foo"})
}

full: {
	out: {

		// changing this to a list of the keys "fixes" things, i.e. this comprehension is required
		for k, _ in #github_team_membership {

			// Remove this comprehension to trigger a different type of cycle error
			if ( values[k] != _|_ ) {

				// This let is also required
				let val = values[k]

				if val != null && ((val & string) != _|_) {
					(k): val
				}
			}
		}
	}

	#github_team_membership: {
		team_id: string
		role:    string
	}

	values: {
		"role":     "member"
		"username": "foo"
	}
	@test(eq, {out: {role: "member"}, #github_team_membership: {team_id: string, role: string}, values: {role: "member", username: "foo"}})
}
-- out/errors.txt --
[eval] out: conflicting values {val:k,if (val != _|_(explicit error (_|_ literal) in source)) {k}} and "b" (mismatched types struct and string):
    ./in.cue:4:6
    ./in.cue:5:2
    ./in.cue:6:3
    ./in.cue:6:24
    ./in.cue:8:4
    ./in.cue:9:5
    ./in.cue:35:2
-- out/eval/stats --
Leaks:  3
Freed:  23
Reused: 9
Allocs: 17
Retain: 0

Unifications: 26
Conjuncts:    54
Disjuncts:    0

NumCloseIDs: 22
