-- in.cue --
-- comprehensions.cue --
// Panic "vertex not initialized" in v0.14.0+ with recursive comprehensions
// referencing a field being defined, particularly across subdirectories.
// https://cuelang.org/issue/4119
issue4119: reduced: {
	x: [...]
	x: _ | *[{f: 123}]
	x: [
		for i in x {
			f: i.f
		}
	]
}
// This test is added to test the memory management bug isn't triggered
// one level deeper. This test is expected to fail, as it now triggers
// a structural cycle in the right disjunct, leaving an incomplete error
// on the left disjunct.
issue4119: reducedNested: {
	x: {y: [...]}
	x: {y: _} | *{y: [{f: 123}]}
	x: {y: [
		for i in x {
			f: i.f
		}
	]}
}
issue4119: full: {
	_img: img={
		val: string
		accounts: {
			groups: img.accounts.groups | *[{gid: 65532}]

			// Recursive comprehension - this triggers the bug
			groups: [
				for g in groups
				let _gid = g.gid {
					gid: [
						if _gid != _|_ {_gid},
						65532,
					][0]
				},
			]
		}
	}

	art: {
		images: [string]: #Image
		for k, v in images {
			images: (k): _img & v
		}
	}
	#Image: {
		val: string
		accounts?: {
			groups?: [...]
		}
	}
	art: images: ko: {
		val: "test"
	}
}
-- out/compile --
--- comprehensions.cue
{
  issue4119: {
    reduced: {
      x: [
        ...,
      ]
      x: (_|*[
        {
          f: 123
        },
      ])
      x: [
        for _, i in 〈1;x〉 {
          f: 〈1;i〉.f
        },
      ]
    }
  }
  issue4119: {
    reducedNested: {
      x: {
        y: [
          ...,
        ]
      }
      x: ({
        y: _
      }|*{
        y: [
          {
            f: 123
          },
        ]
      })
      x: {
        y: [
          for _, i in 〈2;x〉 {
            f: 〈1;i〉.f
          },
        ]
      }
    }
  }
  issue4119: {
    full: {
      _img: {
        val: string
        accounts: {
          groups: (〈2〉.accounts.groups|*[
            {
              gid: 65532
            },
          ])
          groups: [
            for _, g in 〈1;groups〉 let _gid = 〈0;g〉.gid {
              gid: [
                if (〈2;_gid〉 != _|_(explicit error (_|_ literal) in source)) {
                  〈3;_gid〉
                },
                65532,
              ][0]
            },
          ]
        }
      }
      art: {
        images: {
          [string]: 〈2;#Image〉
        }
        for k, v in 〈0;images〉 {
          images: {
            〈2;k〉: (〈4;_img〉 & 〈2;v〉)
          }
        }
      }
      #Image: {
        val: string
        accounts?: {
          groups?: [
            ...,
          ]
        }
      }
      art: {
        images: {
          ko: {
            val: "test"
          }
        }
      }
    }
  }
}
--- in.cue
{}
