Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test unlocking paramTypesMatch for templates and macros #23176

Draft
wants to merge 8 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/sempass2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ proc trackProc*(c: PContext; s: PSym, body: PNode) =

track(t, body)

if s.kind != skMacro:
if s.kind notin {skMacro, skTemplate}:
let params = s.typ.n
for i in 1..<params.len:
let param = params[i].sym
Expand Down
27 changes: 13 additions & 14 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ proc procTypeRel(c: var TCandidate, f, a: PType): TTypeRelation =
elif a[0] != nil:
return isNone

result = getProcConvMismatch(c.c.config, f, a, result)[1]
result = getProcConvMismatch(c.c.config, f, a, result, c.calleeSym != nil and c.calleeSym.kind in {skTemplate, skMacro})[1]

when useEffectSystem:
if compatibleEffects(f, a) != efCompat: return isNone
Expand Down Expand Up @@ -2168,19 +2168,17 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
# This special typing rule for macros and templates is not documented
# anywhere and breaks symmetry. It's hard to get rid of though, my
# custom seqs example fails to compile without this:
if r != isNone and m.calleeSym != nil and
m.calleeSym.kind in {skMacro, skTemplate}:
let macroOrTemplate = m.calleeSym != nil and m.calleeSym.kind in {skMacro, skTemplate}
if r != isNone and macroOrTemplate:
# XXX: duplicating this is ugly, but we cannot (!) move this
# directly into typeRel using return-like templates
incMatches(m, r)
if f.kind == tyTyped:
return arg
elif f.kind == tyTypeDesc:
return arg
if f.kind in {tyTyped, tyUntyped, tyTypeDesc,
tyVar, tyLent, tySink, tyOpenArray}:
incMatches(m, r)
return argSemantized
elif f.kind == tyStatic and arg.typ.n != nil:
incMatches(m, r)
return arg.typ.n
else:
return argSemantized # argOrig

# If r == isBothMetaConvertible then we rerun typeRel.
# bothMetaCounter is for safety to avoid any infinite loop,
Expand Down Expand Up @@ -2251,8 +2249,9 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
inc(m.genericMatches)
if arg.typ == nil:
result = arg
elif skipTypes(arg.typ, abstractVar-{tyTypeDesc}).kind == tyTuple or
m.inheritancePenalty > oldInheritancePenalty:
elif m.inheritancePenalty > oldInheritancePenalty or (
skipTypes(arg.typ, abstractVar-{tyTypeDesc}).kind == tyTuple and
not macroOrTemplate):
result = implicitConv(nkHiddenSubConv, f, arg, m, c)
elif arg.typ.isEmptyContainer:
result = arg.copyTree
Expand All @@ -2271,8 +2270,8 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
inc(m.exactMatches)
result = arg
let ff = skipTypes(f, abstractVar-{tyTypeDesc})
if ff.kind == tyTuple or
(arg.typ != nil and skipTypes(arg.typ, abstractVar-{tyTypeDesc}).kind == tyTuple):
if not macroOrTemplate and (ff.kind == tyTuple or
(arg.typ != nil and skipTypes(arg.typ, abstractVar-{tyTypeDesc}).kind == tyTuple)):
result = implicitConv(nkHiddenSubConv, f, arg, m, c)
of isNone:
# do not do this in ``typeRel`` as it then can't infer T in ``ref T``:
Expand Down
15 changes: 9 additions & 6 deletions compiler/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,7 @@ proc skipHiddenSubConv*(n: PNode; g: ModuleGraph; idgen: IdGenerator): PNode =
else:
result = n

proc getProcConvMismatch*(c: ConfigRef, f, a: PType, rel = isNone): (set[ProcConvMismatch], TTypeRelation) =
proc getProcConvMismatch*(c: ConfigRef, f, a: PType, rel = isNone, explicitClosure = false): (set[ProcConvMismatch], TTypeRelation) =
## Returns a set of the reason of mismatch, and the relation for conversion.
result[1] = rel
if tfNoSideEffect in f.flags and tfNoSideEffect notin a.flags:
Expand All @@ -1644,11 +1644,14 @@ proc getProcConvMismatch*(c: ConfigRef, f, a: PType, rel = isNone): (set[ProcCon
if f.callConv != a.callConv:
# valid to pass a 'nimcall' thingie to 'closure':
if f.callConv == ccClosure and a.callConv == ccNimCall:
case result[1]
of isInferred: result[1] = isInferredConvertible
of isBothMetaConvertible: result[1] = isBothMetaConvertible
elif result[1] != isNone: result[1] = isConvertible
else: result[0].incl pcmDifferentCallConv
if explicitClosure and tfExplicitCallConv notin f.flags:
discard
else:
case result[1]
of isInferred: result[1] = isInferredConvertible
of isBothMetaConvertible: result[1] = isBothMetaConvertible
elif result[1] != isNone: result[1] = isConvertible
else: result[0].incl pcmDifferentCallConv
else:
result[1] = isNone
result[0].incl pcmDifferentCallConv
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/net.nim
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ when defined(nimHasStyleChecks):
when defined(posix) and not defined(lwip):
from std/posix import TPollfd, POLLIN, POLLPRI, POLLOUT, POLLWRBAND, Tnfds

template monitorPollEvent(x: var SocketHandle, y: cint, timeout: int): int =
template monitorPollEvent(x: var SocketHandle, y: cshort, timeout: int): int =
var tpollfd: TPollfd
tpollfd.fd = cast[cint](x)
tpollfd.events = y
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/unicode.nim
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ proc runeLenAt*(s: openArray[char], i: Natural): int =

const replRune = Rune(0xFFFD)

template fastRuneAt*(s: openArray[char] or string, i: int, result: untyped, doInc = true) =
template fastRuneAt*(s: openArray[char] or string, i: typed, result: untyped, doInc = true) =
## Returns the rune ``s[i]`` in ``result``.
##
## If ``doInc == true`` (default), ``i`` is incremented by the number
Expand Down
9 changes: 9 additions & 0 deletions tests/template/ttypedarg.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
block: # issue #18667
template fn(b: seq[string]) =
let d = $(b, )
fn(@[])

block: # issue #18667
template fn(b: seq[string]) =
let d = $b
fn(@[])
Loading