From 399c7fd6158651c307b010b1fba74e763b56460c Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 16 Aug 2024 16:32:49 +0300 Subject: [PATCH] better original patch + don't constant fold calls in `typeof` --- compiler/semexprs.nim | 5 +++-- tests/vm/tgenericcompiletimeproc.nim | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 583cd99970952..b928a892e8377 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -971,7 +971,7 @@ proc evalAtCompileTime(c: PContext, n: PNode): PNode = if callee.magic notin ctfeWhitelist: return if callee.kind notin {skProc, skFunc, skConverter, skConst} or - hasUnresolvedParams(n[0], {}): + callee.isGenericRoutineStrict: return if n.typ != nil and typeAllowed(n.typ, skConst, c) != nil: return @@ -1119,7 +1119,8 @@ proc afterCallActions(c: PContext; n, orig: PNode, flags: TExprFlags; expectedTy not (result.typ.kind == tySequence and result.elementType.kind == tyEmpty): liftTypeBoundOps(c, result.typ, n.info) #result = patchResolvedTypeBoundOp(c, result) - if c.matchedConcept == nil: + if c.matchedConcept == nil and efInTypeof notin flags: + # don't fold calls in concepts and typeof result = evalAtCompileTime(c, result) proc semIndirectOp(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType = nil): PNode = diff --git a/tests/vm/tgenericcompiletimeproc.nim b/tests/vm/tgenericcompiletimeproc.nim index 5b92337b5ae7e..12d81c96897ed 100644 --- a/tests/vm/tgenericcompiletimeproc.nim +++ b/tests/vm/tgenericcompiletimeproc.nim @@ -17,3 +17,10 @@ block: # issue #19365 proc f[T](x: static T): T {.compileTime.} = x + x doAssert f(123) == 246 doAssert f(1.0) == 2.0 + +block: + # don't fold compile time procs in typeof + proc fail[T](x: T): T {.compileTime.} = + doAssert false + x + doAssert typeof(fail(123)) is typeof(123)