# Tests cycles and incomplete errors. The cycle struct covers resolvable cycles
# (x=200, x=y+100, y=x-100 resolves because x is grounded at 200) and
# unresolvable ones (a=b+100, b=a-100 — cycle error). The incompleteOne and
# incompleteTwo cases verify that string interpolation with an abstract type
# parameter reports "incomplete" errors correctly regardless of whether the
# hidden template field _t appears before or after its usage site.
-- in.cue --
cycle: {
	x: 200             @test(eq, 200)
	x: y + 100
	y: x - 100         @test(eq, 100)

	a: b + 100  @test(err, code=cycle,
		suberr=(pos=[0:5], args=[b]),
		suberr=(pos=[4:5], args=[a]),
	)
	b: a - 100  @test(err, code=cycle,
		suberr=(pos=[-4:5], args=[b]),
		suberr=(pos=[0:5], args=[a]),
	)
}

incompleteOne: {
	@test(err, code=incomplete, pos=[9:5, 3:7, 4:3], at=r)
	_t: {
		#p: string
		"""
			hello \(#p)
			"""
	}

	r: """
		\(_t)
		"""
	// TODO: we cannot check for errors on hidden fields yet.
	@test(debugCheck, """
		(struct){
		  _t: (_|_){
		    // [incomplete] incompleteOne._t: invalid interpolation: non-concrete value string (type string):
		    //     in.cue:20:3
		    //     in.cue:19:7
		    #p: (string){ string }
		  }
		  r: (_|_){
		    // [incomplete] incompleteOne._t: invalid interpolation: invalid interpolation: non-concrete value string (type string):
		    //     in.cue:25:5
		    //     in.cue:19:7
		    //     in.cue:20:3
		  }
		}
		""")
}

incompleteTwo: {
	r: """
		\(_t)
		""" @test(err, code=incomplete)

	_t: {
		#p: string
		"""
			hello \(#p)
			"""
	}
}
-- out/errors.txt --
[cycle] cycle.a: cycle with field: b:
    ./in.cue:6:5
cycle.b: cycle with field: a:
    ./in.cue:10:5
[cycle] cycle.a: cycle with field: b:
    ./in.cue:6:5
cycle.b: cycle with field: a:
    ./in.cue:10:5
[incomplete] incompleteOne._t: invalid interpolation: non-concrete value string (type string):
    ./in.cue:20:3
    ./in.cue:19:7
[incomplete] incompleteOne._t: invalid interpolation: invalid interpolation: non-concrete value string (type string):
    ./in.cue:25:5
    ./in.cue:19:7
    ./in.cue:20:3
[incomplete] incompleteTwo._t: invalid interpolation: invalid interpolation: non-concrete value string (type string):
    ./in.cue:48:5
    ./in.cue:53:7
    ./in.cue:54:3
[incomplete] incompleteTwo._t: invalid interpolation: non-concrete value string (type string):
    ./in.cue:54:3
    ./in.cue:53:7
-- out/eval/stats --
Leaks:  0
Freed:  14
Reused: 6
Allocs: 8
Retain: 0

Unifications: 14
Conjuncts:    17
Disjuncts:    0

NumCloseIDs: 4
