Skip to content

Commit

Permalink
sem whole when stmts as generic stmt
Browse files Browse the repository at this point in the history
fixes CI, refs nim-lang#24066, refs nim-lang#24065
  • Loading branch information
metagn committed Sep 6, 2024
1 parent 7cd1777 commit 9ef5ee0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 2 additions & 3 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2709,7 +2709,6 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
elif not cannotResolve and val.intVal != 0 and result == nil:
setResult(it[1])
return # we're not in nimvm and we already have a result
it[1] = semGenericStmt(c, it[1])
else:
let e = forceBool(c, semConstExpr(c, it[0]))
if e.kind != nkIntLit:
Expand All @@ -2722,7 +2721,7 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
of nkElse, nkElseExpr:
checkSonsLen(it, 1, c.config)
if cannotResolve:
it[0] = semGenericStmt(c, it[0])
discard
elif result == nil or whenNimvm:
if semCheck:
it[0] = semExpr(c, it[0], flags)
Expand All @@ -2733,7 +2732,7 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
result = it[0]
else: illFormedAst(n, c.config)
if cannotResolve:
result = n
result = semGenericStmt(c, n)
result.typ = makeTypeFromExpr(c, result.copyTree)
return
if result == nil:
Expand Down
15 changes: 15 additions & 0 deletions tests/proc/tstaticsignature.nim
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,18 @@ block: # issue #22607, needs nkWhenStmt to be handled like nkRecWhen
test[true](1.int)
test[false](1.0)
doAssert not compiles(test[])

block: # `when` in static signature
template ctAnd(a, b): bool =
when a:
when b: true
else: false
else: false
template test(): untyped =
when ctAnd(declared(SharedTable), typeof(result) is SharedTable):
result = SharedTable()
else:
result = 123
proc foo[T](): T = test()
proc bar[T](x = foo[T]()): T = x
doAssert bar[int]() == 123

0 comments on commit 9ef5ee0

Please sign in to comment.