Skip to content

Commit c007bf5

Browse files
committed
whitelist generic params matching typedesc for templates/macros
fixes #23432
1 parent 1ef4d04 commit c007bf5

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

compiler/sigmatch.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1905,10 +1905,12 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
19051905
# when `f` is an unresolved typedesc, `a` could be any
19061906
# type, so we should not perform this check earlier
19071907
if c.c.inGenericContext > 0 and
1908-
a.skipTypes({tyTypeDesc}).kind == tyGenericParam:
1908+
a.skipTypes({tyTypeDesc}).kind == tyGenericParam and
1909+
not (c.calleeSym != nil and c.calleeSym.kind in {skMacro, skTemplate}):
19091910
# generic type bodies can sometimes compile call expressions
19101911
# prevent unresolved generic parameters from being passed to procs as
19111912
# typedesc parameters
1913+
# macros and templates receive a pass for practicality
19121914
result = isNone
19131915
elif a.kind != tyTypeDesc:
19141916
if a.kind == tyGenericParam and tfWildcard in a.flags:

tests/generics/tmacrotype.nim

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import std/[sequtils, macros]
2+
3+
block: # issue #23432
4+
type
5+
Future[T] = object
6+
InternalRaisesFuture[T, E] = object
7+
8+
macro Raising[T](F: typedesc[Future[T]], E: varargs[typedesc]): untyped =
9+
## Given a Future type instance, return a type storing `{.raises.}`
10+
## information
11+
##
12+
## Note; this type may change in the future
13+
E.expectKind(nnkBracket)
14+
15+
let raises = nnkTupleConstr.newTree(E.mapIt(it))
16+
nnkBracketExpr.newTree(
17+
ident "InternalRaisesFuture",
18+
nnkDotExpr.newTree(F, ident"T"),
19+
raises
20+
)
21+
22+
type X[E] = Future[void].Raising(E)
23+
24+
proc f(x: X) = discard
25+
26+
27+
var v: Future[void].Raising([ValueError])
28+
f(v)

0 commit comments

Comments
 (0)