generate tyFromExpr for when in generics (#24066)

fixes #22342, fixes #22607

Another followup of #22029, `when` expressions in general in generic
type bodies now behave like `nkRecWhen` does since #24042, leaving them
as `tyFromExpr` if a condition is uncertain. The tests for the issues
were originally added but left disabled in #24005.
This commit is contained in:
metagn
2024-09-06 12:46:17 +03:00
committed by GitHub
parent a93c5d79b9
commit 7cd1777218
4 changed files with 65 additions and 4 deletions

View File

@@ -333,10 +333,47 @@ block: # issue #24044
type MyBuf[I] = ArrayBuf[maxLen(I)]
var v: MyBuf[int]
when false: # issue #22342, type section version of #22607
block: # issue #22342, type section version of #22607
type GenAlias[isInt: static bool] = (
when isInt:
int
else:
float
)
doAssert GenAlias[true] is int
doAssert GenAlias[false] is float
proc foo(T: static bool): GenAlias[T] = discard
doAssert foo(true) is int
doAssert foo(false) is float
proc foo[T: static bool](v: var GenAlias[T]) =
v += 1
var x: int
foo[true](x)
doAssert not compiles(foo[false](x))
foo[true](x)
doAssert x == 2
var y: float
foo[false](y)
doAssert not compiles(foo[true](y))
foo[false](y)
doAssert y == 2
block: # `when`, test no constant semchecks
type Foo[T] = (
when false:
{.error: "bad".}
elif defined(neverDefined):
{.error: "bad 2".}
else:
T
)
var x: Foo[int]
type Bar[T] = (
when true:
T
elif defined(js):
{.error: "bad".}
else:
{.error: "bad 2".}
)
var y: Bar[int]

View File

@@ -223,7 +223,7 @@ block: # issue #7547
let z = initContainsFoo(5) # Error: undeclared identifier: 'N'
doAssert z.Ffoo is int
when false: # issue #22607, needs nkWhenStmt to be handled like nkRecWhen
block: # issue #22607, needs nkWhenStmt to be handled like nkRecWhen
proc test[x: static bool](
t: (
when x: