-- in.cue --
dynamicTop: {
	#D: {
		for k, v in {foo: 123} {(k): v}
	}
}

// Issue #1404
dynamicDepth1: {
	#D: {
		a: foo: 123
		b: {for k, v in a {(k): v}}
	}
}

allowed: {
	#D: {
		if true {
			d: int
		}
	}

	vErr: #D & {d: 5}
}

disallowed: {
	#D: {
		if false {
			d: int
		}
	}

	vErr: #D & {d: 5}
}

// Interpret comprehensions as embeddings.
// Issue #1956
comprehensionIsEmbed: {
	#A: size: int | *1
	#B: {
		kind: string

		// Should be identical to just #A. That is, #A is treated as open.
		if true {
			#A
		}
	}
	x: #B & {
		kind: "A"
	}
}

// This comprehension is inside a definition and should still disallow b.
noEraseDefinition: {
	#Foo: [if true {a: int}]
	a: {#Foo & [{b: 2}]} // Error
}

// issue1956: Tests that conditional comprehensions correctly embed definitions
// inside a closed struct without spuriously rejecting fields.
// https://cuelang.org/issue/1956
issue1956: {
	#Details: {
		size: int | *1
	}

	#Thing: {
		kind: "KindA" | "KindB"
		if kind == "KindA" {
			#Details
		}
	}

	x: #Thing & {
		kind: "KindA"
	}
}

// This triggers adding the same "closedInfo" twice, potentially causing
// a cyclic linked list in the todo list.
dedupTodo: {
	#sub: {
		c: _
		if c.n == "c" {
			X: test: c
		}
	}

	out: #sub // needs to be definition
	out: {
		c: n: "c" // Do not combine with &, as the original issue relies on this.
		c: n: string
	}
}
-- v3issues.cue --
// issue3483: Tests that a falsy if comprehension inside a definition does not
// incorrectly produce "field not allowed" in evalv3.
// https://cuelang.org/issue/3483
issue3483: {
	#Schema: schemaField: int
	out1: out2: #Schema & {
		if false {
			schemaField: 3
		}
	}
}
// issue3486: Tests that embedding a definition inside an if comprehension does
// not produce spurious "field not allowed" errors.
// https://cuelang.org/issue/3486
issue3486: {
	#schema: {}

	if true {
		out: {
			#schema

			// This field should be allowed as #schema is embedded.
			extra: "foo"
		}
	}
}
-- out/compile --
--- in.cue
{
  dynamicTop: {
    #D: {
      for k, v in {
        foo: 123
      } {
        〈1;k〉: 〈1;v〉
      }
    }
  }
  dynamicDepth1: {
    #D: {
      a: {
        foo: 123
      }
      b: {
        for k, v in 〈1;a〉 {
          〈1;k〉: 〈1;v〉
        }
      }
    }
  }
  allowed: {
    #D: {
      if true {
        d: int
      }
    }
    vErr: (〈0;#D〉 & {
      d: 5
    })
  }
  disallowed: {
    #D: {
      if false {
        d: int
      }
    }
    vErr: (〈0;#D〉 & {
      d: 5
    })
  }
  comprehensionIsEmbed: {
    #A: {
      size: (int|*1)
    }
    #B: {
      kind: string
      if true {
        〈2;#A〉
      }
    }
    x: (〈0;#B〉 & {
      kind: "A"
    })
  }
  noEraseDefinition: {
    #Foo: [
      if true {
        a: int
      },
    ]
    a: {
      (〈1;#Foo〉 & [
        {
          b: 2
        },
      ])
    }
  }
  issue1956: {
    #Details: {
      size: (int|*1)
    }
    #Thing: {
      kind: ("KindA"|"KindB")
      if (〈0;kind〉 == "KindA") {
        〈2;#Details〉
      }
    }
    x: (〈0;#Thing〉 & {
      kind: "KindA"
    })
  }
  dedupTodo: {
    #sub: {
      c: _
      if (〈0;c〉.n == "c") {
        X: {
          test: 〈2;c〉
        }
      }
    }
    out: 〈0;#sub〉
    out: {
      c: {
        n: "c"
      }
      c: {
        n: string
      }
    }
  }
}
--- v3issues.cue
{
  issue3483: {
    #Schema: {
      schemaField: int
    }
    out1: {
      out2: (〈1;#Schema〉 & {
        if false {
          schemaField: 3
        }
      })
    }
  }
  issue3486: {
    #schema: {}
    if true {
      out: {
        〈2;#schema〉
        extra: "foo"
      }
    }
  }
}
