-- in.cue --
// cmd/cue: misleading "field not allowed" error when a type clash occurs inside
// a disjunction; the actual conflict should be reported instead.
// https://cuelang.org/issue/570
issue570: {
	results: #DecodeOutput
	results: result: "hello"

	#Output: {
		result: _
	} | {
		error: string
	}

	#DecodeOutput: #Output & {
		result?: [... string]
		...
	}
	@test(err, code=eval, at=results.result, contains="conflicting values", args=["hello", [...string]], pos=[1:11, 2:19, 11:12])
}

// Clarify when an error resulted from a failed disjunction; error messages
// should indicate which disjunct failed rather than showing a generic error.
// https://cuelang.org/issue/516
issue516: {
	#Def: {
		match: metrics: string: {}
	} | {}

	x: #Def
	x: match: metrics: "foo": {}
	@test(err, code=eval, at=x, contains="2 errors in empty disjunction", pos=[])
}

// issue #465
explicitDefaultError: {
	a: string | *_|_

	if a != "" {
	}
	@test(err, code=incomplete, contains="non-concrete value string in operand to !=", pos=[3:5, 1:5])
}

// evaluator: export as YAML vs JSON emits different templated data when
// pattern constraints with defaults produce different disjunction results.
// https://cuelang.org/issue/2916
issue2916: {
	b: _
	[_]: c:      uint | *1
	[string]: c: >=3 | *3
	@test(eq, {b: {c: *3 | >=3 & int}})
}
// evaluator: inconsistent export output for different formats based on which
// default is selected when multiple defaults are unified.
// https://cuelang.org/issue/3157
issue3157: {
	foo: *42 | _
	foo: *43 | bool
	@test(eq, {foo: *43 | bool})
}

// cmd/cue: every error in an empty disjunction should be reported, not just
// the first. Both disjuncts of `1 | 2` fail against the bound `>10`, and both
// "invalid value" errors are now reported even though they share the same
// primary position (the bound's location).
// https://cuelang.org/issue/4043
issue4043: {
	B: >10
	B: 1 | 2 @test(err, code=eval, contains="2 errors in empty disjunction",
		suberr=(code=eval, contains="invalid value 1 (out of bound >10)", pos=[-1:5, 0:5]),
		suberr=(code=eval, contains="invalid value 2 (out of bound >10)", pos=[-1:5, 0:9]),
	)
}
-- issue3581.cue --
// evalv3: tools/flow unexpected error when a flow task result field is used as
// a list item; string interpolation in a list element caused a spurious error.
// https://cuelang.org/issue/3581
issue3581: reduced: {
	list: [...] | *[]
	list: ["\(c)" ] @test(err, code=incomplete, contains="2 errors in empty disjunction",
		suberr=(code=incomplete, contains="incompatible list lengths", args=[0, 1], pos=[-1:17]),
		suberr=(code=incomplete, contains="invalid interpolation: non-concrete value", pos=[0:9]),
	)
	c: _
	@test(eq, {list: _|_, c: _})
}
-- issue3576.cue --
// evalv3: disjunct of definitions with identical optional fields results in
// incomplete evaluation; the resolved disjunct should be fully evaluated.
// https://cuelang.org/issue/3576
issue3576: reduced: {
	#A: a!: string
	#B: {
		if false {
			dummy: {}
		}
	}
	foo: #B | #A
	foo: a: "1"
	@test(eq, {
		#A: {a!: string}
		#B: {}
		foo: {a: "1"}
	})
}
issue3576: full: {
	#Run: {
		run!: string
		options?: #Option | [#Option, ...]
		if options != _|_ {
			optionsValue: options
		}
	}

	#Copy: {
		copy!: string
		options?: #Option | [#Option, ...]
		if options != _|_ {
			optionsValue: options
		}
	}
	
	#Option: {}
	
	foo: #Run | #Copy
	foo: run: "make"
	@test(eq, {
		#Run: {run!: string, options?: {} | [{}]}
		#Copy: {copy!: string, options?: {} | [{}]}
		#Option: {}
		foo: {run: "make", options?: {} | [{}]}
	})
}
-- issue3599.cue --
// evalv3: "explicit error in source" regression when using bottom (_|_) in
// repeating disjunctions; errors in optional/let fields should be ignored.
// https://cuelang.org/issue/3599
// Ignore errors for irrelevant fields like let and optional fields.
issue3599: full: {
	#Action: {
		action: {
			read!:  string
			write?: _|_
		} | {
			write!: string
			read?:  _|_
		}
	}
	
	#Minio: {
		container: #Action & {
			action: {
				read!:  string
				write?: _|_
			} | {
				write!: string
				read?:  _|_
			}
		}
		doWrite: bool | *true
		container: action: defaultAction
		defaultAction: {
			if doWrite { write: "somefile.txt" }
			if !doWrite { read: "somefile.txt" }
		}
	}
	@test(skip, hint="somehow this test doesn't work. Fix it.")
	@test(eq:todo, {
		#Action: {
			action: {read!: string, write?: _|_ @test(err, code=user, contains="explicit error (_|_ literal) in source", pos=[])} | {write!: string, read?: _|_ @test(err, code=user, contains="explicit error (_|_ literal) in source", pos=[])}
		}
		#Minio: {
			container: {
				action: {
					write: "somefile.txt"
					read?: _|_ @test(err, code=user, contains="explicit error (_|_ literal) in source (and 1 more errors)", pos=[23:13])
				}
			}
			doWrite: *true | bool
			defaultAction: {write: "somefile.txt"}
		}
	})
}
issue3599: reduced: p1: {
	{ a?:  1&2 } | { a:  string, b?: 1&2 }
	{  } | {  }
	@test(eq, {a?: _|_ @test(err, code=eval, contains="conflicting values", args=[2, 1], pos=[49:25, 50:2, 50:9, 50:11])} | {a: string, b?: _|_ @test(err, code=eval, contains="conflicting values", args=[2, 1], pos=[49:25, 50:2, 50:35, 50:37])})
}
issue3599: reduced: p2: {
	{  } | {  }
	{ a?:  1&2 } | { a:  string, b?: 1&2 }
	@test(eq, {a?: _|_ @test(err, code=eval, contains="conflicting values", args=[2, 1], pos=[54:25, 56:2, 56:9, 56:11])} | {a: string, b?: _|_ @test(err, code=eval, contains="conflicting values", args=[2, 1], pos=[54:25, 56:2, 56:35, 56:37])})
}
issue3599: let: {
	{ } | { a:  B | 1, let B = 1&2 }
	{  } | {  }
	@test(eq, {} | {a: 1})
}
-- optionalerror.cue --
// evalv3 closedness regression with disjunctions and ellipses; a definition
// using _|_ in an optional field inside a disjunction caused spurious errors.
// https://cuelang.org/issue/4022
issue4022: reduced: {
	#NonNamespaced: metadata: namespace?: _|_
	#NullOrNonNamespaced: int | #NonNamespaced
	out: #NullOrNonNamespaced
	out: #NonNamespaced
	out: metadata: {}
	@test(eq, {
		#NonNamespaced: {
			metadata: {
				namespace?: _|_ @test(err, code=user, contains="explicit error (_|_ literal) in source", pos=[1:40])
			}
		}
		#NullOrNonNamespaced: int | {metadata: {
		namespace?: _|_ @test(err, code=user, contains="explicit error (_|_ literal) in source", pos=[1:40])
	}}
		out: {
			metadata: {
				namespace?: _|_ @test(err, code=user, contains="explicit error (_|_ literal) in source", pos=[1:40])
			}
		}
	})
}
issue4022: full: {
	#NonNamespaced: {
		metadata: {
			name!:      string
			namespace?: _|_
			...
		}
		...
	}

	#NullOrNonNamespaced: null | #NonNamespaced

	out: #NullOrNonNamespaced
	out: #NonNamespaced
	out: {
		metadata: name: "foo"
		apiVersion: "v1"
	}
	@test(eq, {
		#NonNamespaced: {
			metadata: {
				name!: string
				namespace?: _|_ @test(err, code=user, contains="explicit error (_|_ literal) in source", pos=[4:16])
			}
		}
		#NullOrNonNamespaced: null | {metadata: {
		name!: string
		namespace?: _|_ @test(err, code=user, contains="explicit error (_|_ literal) in source", pos=[4:16])
	}}
		out: {
			metadata: {
				name: "foo"
				namespace?: _|_ @test(err, code=user, contains="explicit error (_|_ literal) in source", pos=[4:16])
			}
			apiVersion: "v1"
		}
	})
}
-- out/errors.txt --
[eval] issue570.results.result: conflicting values "hello" and [...string] (mismatched types string and list):
    ./in.cue:5:11
    ./in.cue:6:19
    ./in.cue:15:12
[eval] issue516.x: 2 errors in empty disjunction:
issue516.x.match: field not allowed:
    ./in.cue:30:5
issue516.x.match.metrics.foo: field not allowed:
    ./in.cue:30:21
[incomplete] explicitDefaultError: non-concrete value string in operand to !=:
    ./in.cue:38:5
    ./in.cue:36:5
[eval] issue4043.B: 2 errors in empty disjunction:
issue4043.B: invalid value 1 (out of bound >10):
    ./in.cue:67:5
    ./in.cue:68:5
issue4043.B: invalid value 2 (out of bound >10):
    ./in.cue:67:5
    ./in.cue:68:9
[incomplete] issue3581.reduced.list: 2 errors in empty disjunction:
issue3581.reduced.list: incompatible list lengths (0 and 1):
    ./issue3581.cue:5:17
issue3581.reduced.list.0: invalid interpolation: non-concrete value _ (type _):
    ./issue3581.cue:6:9
[user] explicit error (_|_ literal) in source:
    ./optionalerror.cue:5:40
[user] explicit error (_|_ literal) in source:
    ./optionalerror.cue:30:16
-- out/eval/stats --
Leaks:  31
Freed:  205
Reused: 184
Allocs: 52
Retain: 0

Unifications: 118
Conjuncts:    271
Disjuncts:    74
Notifications: 2

NumCloseIDs: 51
-- out/todo.txt --
issue4022: only evaluated by v3 evaluator.
