-
-
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.
check constant conditions in generic
when
in objects (#24042)
fixes #24041 `when` statements in generic object types normally just leave their conditions as expressions and still typecheck their branch bodies. Instead of this, when the condition can be evaluated as a constant as well as the ones before it and it resolves to `true`, it now uses the body of that branch without typechecking the remaining ones.
- Loading branch information
Showing
2 changed files
with
58 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
discard """ | ||
targets: "c js" | ||
""" | ||
|
||
block: # issue #24041 | ||
type ArrayBuf[N: static int, T = byte] = object | ||
when sizeof(int) > sizeof(uint8): | ||
when N <= int(uint8.high): | ||
n: uint8 | ||
else: | ||
when sizeof(int) > sizeof(uint16): | ||
when N <= int(uint16.high): | ||
n: uint16 | ||
else: | ||
when sizeof(int) > sizeof(uint32): | ||
when N <= int(uint32.high): | ||
n: uint32 | ||
else: | ||
n: int | ||
else: | ||
n: int | ||
else: | ||
n: int | ||
else: | ||
n: int | ||
|
||
var x: ArrayBuf[8] | ||
doAssert x.n is uint8 | ||
when sizeof(int) > sizeof(uint32): | ||
var y: ArrayBuf[int(uint32.high) * 8] | ||
doAssert y.n is int | ||
|
||
block: # constant condition after dynamic one | ||
type Foo[T] = object | ||
when T is int: | ||
a: int | ||
elif true: | ||
a: string | ||
else: | ||
a: bool | ||
var x: Foo[string] | ||
doAssert x.a is string | ||
var y: Foo[int] | ||
doAssert y.a is int | ||
var z: Foo[float] | ||
doAssert z.a is string |