# 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)$"
	#SubID: =~"^\(prefix):sub$"

	id:    #ID
	subid: #SubID
}

case1: #schema & {
	id:    "foo"
	subid: "foo:sub"
}

case2: #schema & {
	subid: "foo:sub"
	id:    "foo"
}
-- out/compile --
--- in.cue
{
  #schema: {
    let splits#1 = 〈import;strings〉.Split(〈0;id〉, "_never_")
    let prefix#2 = 〈0;let splits#1〉[0]
    #ID: =~"^\(〈0;let prefix#2〉)$"
    #SubID: =~"^\(〈0;let prefix#2〉):sub$"
    id: 〈0;#ID〉
    subid: 〈0;#SubID〉
  }
  case1: (〈0;#schema〉 & {
    id: "foo"
    subid: "foo:sub"
  })
  case2: (〈0;#schema〉 & {
    subid: "foo:sub"
    id: "foo"
  })
}
