Skip to content

Commit

Permalink
require untyped macros/templates to match before early expansion in g…
Browse files Browse the repository at this point in the history
…enerics

fixes #22740
  • Loading branch information
metagn committed Aug 20, 2024
1 parent 43274bf commit d54f671
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
21 changes: 14 additions & 7 deletions compiler/semgnrc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,20 @@ proc semGenericStmt(c: PContext, n: PNode,
# unambiguous macros/templates are expanded if all params are untyped
if sfAllUntyped in s.flags and sc.safeLen <= 1:
onUse(fn.info, s)
case s.kind
of skMacro: result = semMacroExpr(c, n, n, s, {efNoSemCheck})
of skTemplate: result = semTemplateExpr(c, n, s, {efNoSemCheck})
else: discard # unreachable
c.friendModules.add(s.owner.getModule)
result = semGenericStmt(c, result, flags, ctx)
discard c.friendModules.pop()
var errors: CandidateErrors
var r = resolveOverloads(c, n, n, {skTemplate, skMacro}, {efNoDiagnostics},
errors, false)
if r.state == csMatch:
case s.kind
of skMacro: result = semMacroExpr(c, n, n, s, {efNoSemCheck})
of skTemplate: result = semTemplateExpr(c, n, s, {efNoSemCheck})
else: discard # unreachable
c.friendModules.add(s.owner.getModule)
result = semGenericStmt(c, result, flags, ctx)
discard c.friendModules.pop()
else:
n[0] = sc
result = n
else:
n[0] = sc
result = n
Expand Down
21 changes: 21 additions & 0 deletions tests/generics/tuntypedmacro.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
block: # issue #22740
macro foo(n: untyped): untyped = discard
# Remove this macro to fix the problem
macro foo(n, n2: untyped): untyped = discard

# This one is fine
proc test1(v: string) =
foo >"test1"

# This one fails to compile
proc test2[T](v: T) =
foo >"test2"

test1("hello")
test2("hello")

block: # above simplified (yes this errored)
proc foo[T]() =
when false:
>1
foo[int]()

0 comments on commit d54f671

Please sign in to comment.