Files
Nim/tests/range/toutofrangevarconv.nim
metagn 538603e01d allow conversions between var types of range types and base types (#24037)
refs #24032, split from #24036

Conversion from variables of range types or base types of range types to
the other are now considered mutable for `var` params, similar to how
distinct types are mutable when converted to their base type or vice
versa. There are 2 main differences:

1. Conversions from base types to range types need to emit
`nkChckRange`, which is not generated for things like tuple/object
fields.
2. Range types can still correspond to different types in the backend
when nested in other types, such as `set[range[3..5]]` vs
`set[range[0..5]]`.

Since the convertibility check for `var` params and a check whether to
emit a no-op for `nkConv` (and now also `nkChckRange`) so that the
output is still addressable both use `sameType`, we accomplish this by
adding a new flag to `sameType` that ignores range types, but only when
they're not nested in other types. The implementation for this might be
flawed, I didn't include children of some metatypes as "nested in other
types", but stuff like `tyGenericInst` params are respected.
2024-09-03 09:18:38 +02:00

15 lines
260 B
Nim

discard """
outputsub: "value out of range: 5 notin 0 .. 3 [RangeDefect]"
exitcode: "1"
"""
# make sure out of bounds range conversion is detected for `var` conversions
type R = range[0..3]
proc foo(x: var R) =
doAssert x in 0..3
var x = 5
foo(R(x))