Skip to content

Commit 34ec7db

Browse files
committed
fix(py),feat(sugar): auto-reorder non-auto restype methods in class
1 parent 4283524 commit 34ec7db

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/pylib/pysugar/stmt/class.nim

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import std/macros
33
import ./frame, ./funcSignature, ./decorator, ./types
44
import ./pydef
5-
5+
import ../../noneType
6+
proc getNoneTypeNode: NimNode = bindSym"NoneType"
67
template emptyn: NimNode = newEmptyNode()
78

89
proc parseDeclWithType(def: NimNode): tuple[name, typ, val: NimNode] =
@@ -377,7 +378,6 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
377378
var defs = newStmtList()
378379
var decls = newStmtList()
379380
template addMeth(def: NimNode) = defs.add def
380-
#[ return type 'auto' cannot be used in forward declarations
381381
template addMethWithDecl(def: NimNode) =
382382
defs.add def
383383
var decl = def.copyNimNode
@@ -386,7 +386,6 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
386386
decl.add def[i].copyNimTree
387387
decl.add newEmptyNode()
388388
decls.add decl
389-
]#
390389
var noNew = true
391390
var
392391
initArgs: seq[NimNode]
@@ -441,7 +440,7 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
441440
args[1]
442441
args[1][1] = classId
443442
if isConstructor:
444-
if args[0].strVal in ["None", "auto"]:
443+
if args[0].eqIdent"auto":
445444
args[0] = newEmptyNode()
446445
expectIdent args[1][0], "self"
447446
markSelfType
@@ -455,6 +454,9 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
455454
else:
456455
if args.len > 1 and args[1][0].eqIdent "self":
457456
markSelfType
457+
if args[0].eqIdent"None":
458+
args[0] = getNoneTypeNode()
459+
pragmas.add ident"discardable"
458460
# Function body
459461
var docNode: NimNode
460462
var parsedbody = recReplaceSuperCall(
@@ -479,7 +481,11 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
479481
newMethProc(topLevel, procName, generics_cpy, args, body,
480482
procType, pragmas=pragmas)
481483
)
482-
addMeth nDef
484+
if args[0].eqIdent"auto":
485+
# return type 'auto' cannot be used in forward declarations
486+
addMeth nDef
487+
else:
488+
addMethWithDecl nDef
483489

484490
of nnkStrLit, nnkRStrLit, nnkTripleStrLit:
485491
result.add newCommentStmtNode $def
@@ -489,7 +495,7 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
489495
else:
490496
result.add def # AS-IS
491497

492-
addMeth genNewCls(ident("new" & classId.strVal).exportIfTop, classId, generics, initArgs, initPragmas)
498+
addMethWithDecl genNewCls(ident("new" & classId.strVal).exportIfTop, classId, generics, initArgs, initPragmas)
493499

494500
let ty = nnkRefTy.newTree nnkObjectTy.newTree(emptyn, supClsNode, typDefLs)
495501
let typDef = nnkTypeSection.newTree nnkTypeDef.newTree(classId.exportIfTop, generics, ty)

0 commit comments

Comments
 (0)