From 9ef5ee0c80f344dc58632f60d51d1c835ee568bf Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 6 Sep 2024 23:49:04 +0300 Subject: [PATCH] sem whole `when` stmts as generic stmt fixes CI, refs #24066, refs #24065 --- compiler/semexprs.nim | 5 ++--- tests/proc/tstaticsignature.nim | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 737a846c06659..5413b25f9059b 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -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: @@ -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) @@ -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: diff --git a/tests/proc/tstaticsignature.nim b/tests/proc/tstaticsignature.nim index 3c6d66b2bd92e..518c88ba5571a 100644 --- a/tests/proc/tstaticsignature.nim +++ b/tests/proc/tstaticsignature.nim @@ -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