From 3fd21cdf46ae5da4b1aa88e995faa19d0e2a625e Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 17 Aug 2024 18:15:41 +0300 Subject: [PATCH] allow early template macro instantiation? weird territory --- compiler/sem.nim | 2 +- compiler/semcall.nim | 2 +- compiler/semexprs.nim | 2 +- tests/generics/tuninstantiatedgenericcalls.nim | 12 ++++++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/compiler/sem.nim b/compiler/sem.nim index f5d9790dc0cb5..e33c5bb0e8b5d 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -501,7 +501,7 @@ const proc semMacroExpr(c: PContext, n, nOrig: PNode, sym: PSym, flags: TExprFlags = {}; expectedType: PType = nil): PNode = - if c.inGenericContext > 0 and sfAllUntyped notin sym.flags: + if false and c.inGenericContext > 0 and sfAllUntyped notin sym.flags: # in generic type body, typed macros can only be instantiated # when the generic type is instantiated result = semGenericStmt(c, n) diff --git a/compiler/semcall.nim b/compiler/semcall.nim index d2642be1a3b33..b0444fff484cd 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -701,7 +701,7 @@ proc semResolvedCall(c: PContext, x: var TCandidate, else: c.inheritBindings(x, expectedType) finalCallee = generateInstance(c, x.calleeSym, x.bindings, n.info) - elif c.inGenericContext == 0: + elif true or c.inGenericContext == 0: # For macros and templates, the resolved generic params # are added as normal params. # This is not done in a generic type body context, as typed macros diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 7299d65d45147..b9f24a74b3a7c 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -27,7 +27,7 @@ const proc semTemplateExpr(c: PContext, n: PNode, s: PSym, flags: TExprFlags = {}; expectedType: PType = nil): PNode = - if c.inGenericContext > 0 and sfAllUntyped notin s.flags: + if false and c.inGenericContext > 0 and sfAllUntyped notin s.flags: # in generic type body, typed templates can only be instantiated # when the generic type is instantiated result = semGenericStmt(c, n) diff --git a/tests/generics/tuninstantiatedgenericcalls.nim b/tests/generics/tuninstantiatedgenericcalls.nim index 7af6e202f238d..54f9ce1d45190 100644 --- a/tests/generics/tuninstantiatedgenericcalls.nim +++ b/tests/generics/tuninstantiatedgenericcalls.nim @@ -181,3 +181,15 @@ block: # deref syntax doAssert foo.x == 123 inc foo.x doAssert foo.x == 124 + +block: + type Generic[T] = object + field: T + macro foo(x: typed): untyped = x + type Foo[T] = object + field: Generic[int].foo() + var x: Foo[int] + macro bar[T](x: typedesc[Generic[T]]): untyped = x + type Bar[T] = object + field: Generic[int].bar() + var y: Bar[int]