Skip to content

Commit

Permalink
restrict to unresolved statics, maybe sigmatch should find anything u…
Browse files Browse the repository at this point in the history
…nresolved
  • Loading branch information
metagn committed Aug 19, 2024
1 parent ddbbb6a commit a3ca148
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
6 changes: 3 additions & 3 deletions compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/generics/tuninstantiatedgenericcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a3ca148

Please sign in to comment.