# A `let` binding that factors out a repeated expression changes the evaluation
# result unexpectedly compared to the inline form, indicating a bug in how
# `let`-bound values are resolved relative to their enclosing scope.
# https://cuelang.org/issue/4244
-- in.cue --
import "strings"

#schema: {
	let splits = strings.Split(id, "_never_")
	let prefix = splits[0]

	#ID:    =~"^\(prefix)$" @test(err, code=incomplete, contains="invalid interpolation", pos=[0:12, -2:22])
	#SubID: =~"^\(prefix):sub$" @test(err, code=incomplete, contains="invalid interpolation", pos=[0:12, -3:22])

	id:    #ID @test(err, code=incomplete, contains="invalid interpolation", pos=[-3:12, -5:22])
	subid: #SubID @test(err, code=incomplete, contains="invalid interpolation", pos=[-3:12, -6:22])
}

case1: #schema & {
	id:    "foo"
	subid: "foo:sub"
} @test(eq, {
	id: "foo"
	subid: "foo:sub"
	#ID: =~"^foo$"
	#SubID: =~"^foo:sub$"
})

case2: #schema & {
	subid: "foo:sub"
	id:    "foo"
} @test(eq, {
	subid: "foo:sub"
	id: "foo"
	#ID: =~"^foo$"
	#SubID: =~"^foo:sub$"
})
-- out/errors.txt --
[incomplete] #schema.prefix: invalid interpolation: index out of range [0] with length 1:
    ./in.cue:7:12
    ./in.cue:5:22
[incomplete] #schema.prefix: invalid interpolation: index out of range [0] with length 1:
    ./in.cue:8:12
    ./in.cue:5:22
[incomplete] #schema.prefix: invalid interpolation: index out of range [0] with length 1:
    ./in.cue:7:12
    ./in.cue:5:22
[incomplete] #schema.prefix: invalid interpolation: index out of range [0] with length 1:
    ./in.cue:8:12
    ./in.cue:5:22
-- out/eval/stats --
Leaks:  8
Freed:  24
Reused: 13
Allocs: 19
Retain: 0

Unifications: 32
Conjuncts:    47
Disjuncts:    0

NumCloseIDs: 10
