Skip to content

Commit

Permalink
whitelist generic params matching typedesc for templates/macros
Browse files Browse the repository at this point in the history
fixes #23432
  • Loading branch information
metagn committed May 4, 2024
1 parent 1ef4d04 commit c007bf5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1905,10 +1905,12 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
# when `f` is an unresolved typedesc, `a` could be any
# type, so we should not perform this check earlier
if c.c.inGenericContext > 0 and
a.skipTypes({tyTypeDesc}).kind == tyGenericParam:
a.skipTypes({tyTypeDesc}).kind == tyGenericParam and
not (c.calleeSym != nil and c.calleeSym.kind in {skMacro, skTemplate}):
# generic type bodies can sometimes compile call expressions
# prevent unresolved generic parameters from being passed to procs as
# typedesc parameters
# macros and templates receive a pass for practicality
result = isNone
elif a.kind != tyTypeDesc:
if a.kind == tyGenericParam and tfWildcard in a.flags:
Expand Down
28 changes: 28 additions & 0 deletions tests/generics/tmacrotype.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import std/[sequtils, macros]

block: # issue #23432
type
Future[T] = object
InternalRaisesFuture[T, E] = object

macro Raising[T](F: typedesc[Future[T]], E: varargs[typedesc]): untyped =
## Given a Future type instance, return a type storing `{.raises.}`
## information
##
## Note; this type may change in the future
E.expectKind(nnkBracket)

let raises = nnkTupleConstr.newTree(E.mapIt(it))
nnkBracketExpr.newTree(
ident "InternalRaisesFuture",
nnkDotExpr.newTree(F, ident"T"),
raises
)

type X[E] = Future[void].Raising(E)

proc f(x: X) = discard


var v: Future[void].Raising([ValueError])
f(v)

0 comments on commit c007bf5

Please sign in to comment.