diff --git a/compiler/sem.nim b/compiler/sem.nim index e33c5bb0e8b5d..1672602ad433d 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -501,9 +501,9 @@ const proc semMacroExpr(c: PContext, n, nOrig: PNode, sym: PSym, flags: TExprFlags = {}; expectedType: PType = nil): PNode = - 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 + if c.inGenericContext > 0 and n.findUnresolvedStatic != nil: + # in generic type body, typed macros using unresolved statics + # can only be instantiated when the generic type is instantiated result = semGenericStmt(c, n) result.typ = makeTypeFromExpr(c, result.copyTree) return diff --git a/compiler/semcall.nim b/compiler/semcall.nim index b0444fff484cd..d8f3e362b9787 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -701,10 +701,10 @@ proc semResolvedCall(c: PContext, x: var TCandidate, else: c.inheritBindings(x, expectedType) finalCallee = generateInstance(c, x.calleeSym, x.bindings, n.info) - elif true or c.inGenericContext == 0: + elif c.inGenericContext == 0 or n.findUnresolvedStatic == nil: # 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 + # This is not done with unresolved static arguments, as typed macros # cannot be instantiated yet and semMacroExpr/semTemplateExpr will # reject them and delay their instantiation, when fully resolved types # will be added instead. diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index b9f24a74b3a7c..3f7f0e111e773 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -27,9 +27,9 @@ const proc semTemplateExpr(c: PContext, n: PNode, s: PSym, flags: TExprFlags = {}; expectedType: PType = nil): PNode = - 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 + if c.inGenericContext > 0 and n.findUnresolvedStatic != nil: + # in generic type body, typed templates using unresolved statics + # can only be instantiated when the generic type is instantiated result = semGenericStmt(c, n) result.typ = makeTypeFromExpr(c, result.copyTree) return diff --git a/tests/generics/tuninstantiatedgenericcalls.nim b/tests/generics/tuninstantiatedgenericcalls.nim index 6c594325387c8..ea6b379517cb9 100644 --- a/tests/generics/tuninstantiatedgenericcalls.nim +++ b/tests/generics/tuninstantiatedgenericcalls.nim @@ -200,3 +200,24 @@ block: var x2: Foo2[int] var y: Bar[int] var y2: Bar2[int] + +block: + macro pick(x: static int): untyped = + if x < 100: + result = bindSym"int" + else: + result = bindSym"float" + + type Foo[T: static int] = object + fixed1: pick(25) + fixed2: pick(125) + unknown: pick(T) + + var a: Foo[123] + doAssert a.fixed1 is int + doAssert a.fixed2 is float + doAssert a.unknown is float + var b: Foo[23] + doAssert b.fixed1 is int + doAssert b.fixed2 is float + doAssert b.unknown is int