-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
81 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# related to issue #24032 | ||
|
||
proc `++`(n: var int) = | ||
n += 1 | ||
|
||
type | ||
r = range[ 0..15 ] | ||
|
||
var a: r = 14 | ||
|
||
++int(a) # this should be mutable | ||
|
||
doAssert a == 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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)) |