consider calling convention and iterator in getType for procs (#24181)

fixes #19010

`getType` for proc types generated an `nkProcTy` for iterator types
instead of `nkIteratorTy`, and didn't generate a calling convention
pragma unless it was in the proc AST. Iterator types now generate
`nkIteratorTy`, and a calling convention pragma is added if the calling
convention isn't `closure` or was explicitly provided.
This commit is contained in:
metagn
2024-09-27 12:11:21 +03:00
committed by GitHub
parent 56a3dd57fb
commit dc3ffb6a71
3 changed files with 41 additions and 6 deletions

View File

@@ -237,7 +237,7 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo;
of tySequence: result = mapTypeToBracket("seq", mSeq, t, info)
of tyProc:
if inst:
result = newNodeX(nkProcTy)
result = newNodeX(if tfIterator in t.flags: nkIteratorTy else: nkProcTy)
var fp = newNodeX(nkFormalParams)
if t.returnType == nil:
fp.add newNodeI(nkEmpty, info)
@@ -246,8 +246,15 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo;
for i in FirstParamAt..<t.kidsLen:
fp.add newIdentDefs(t.n[i], t[i])
result.add fp
result.add if t.n[0].len > 0: t.n[0][pragmasEffects].copyTree
else: newNodeI(nkEmpty, info)
var prag =
if t.n[0].len > 0:
t.n[0][pragmasEffects].copyTree
else:
newNodeI(nkEmpty, info)
if t.callConv != ccClosure or tfExplicitCallConv in t.flags:
if prag.kind == nkEmpty: prag = newNodeI(nkPragma, info)
prag.add newIdentNode(getIdent(cache, $t.callConv), info)
result.add prag
else:
result = mapTypeToBracket("proc", mNone, t, info)
of tyOpenArray: result = mapTypeToBracket("openArray", mOpenArray, t, info)

View File

@@ -1,9 +1,9 @@
discard """
nimout: '''intProc; ntyProc; proc[int, int, float]; proc (a: int; b: float): int
nimout: '''intProc; ntyProc; proc[int, int, float]; proc (a: int; b: float): int {.nimcall.}
void; ntyVoid; void; void
int; ntyInt; int; int
proc (); ntyProc; proc[void]; proc ()
voidProc; ntyProc; proc[void]; proc ()
proc () {.nimcall.}; ntyProc; proc[void]; proc () {.nimcall.}
voidProc; ntyProc; proc[void]; proc () {.nimcall.}
listing fields for ObjType
a: string
b: int

View File

@@ -0,0 +1,28 @@
discard """
nimout: '''
var x: proc () {.cdecl.} = foo
var x: iterator (): int {.closure.} = bar
'''
"""
# issue #19010
import macros
macro createVar(x: typed): untyped =
result = nnkVarSection.newTree:
newIdentDefs(ident"x", getTypeInst(x), copy(x))
echo repr result
block:
proc foo() {.cdecl.} = discard
createVar(foo)
x()
block:
iterator bar(): int {.closure.} = discard
createVar(bar)
for a in x(): discard