Skip to content

Commit 9fb76b4

Browse files
committed
fix(py),feat(sugar): auto-reorder non-auto restype methods in class
1 parent 83475db commit 9fb76b4

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] =
@@ -384,7 +385,6 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
384385
var defs = newStmtList()
385386
var decls = newStmtList()
386387
template addMeth(def: NimNode) = defs.add def
387-
#[ return type 'auto' cannot be used in forward declarations
388388
template addMethWithDecl(def: NimNode) =
389389
defs.add def
390390
var decl = def.copyNimNode
@@ -393,7 +393,6 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
393393
decl.add def[i].copyNimTree
394394
decl.add newEmptyNode()
395395
decls.add decl
396-
]#
397396
var noNew = true
398397
var
399398
initArgs: seq[NimNode]
@@ -449,7 +448,7 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
449448
args[1]
450449
args[1][1] = classId
451450
if isConstructor:
452-
if args[0].strVal in ["None", "auto"]:
451+
if args[0].eqIdent"auto":
453452
args[0] = newEmptyNode()
454453
expectIdent args[1][0], "self"
455454
markSelfType
@@ -463,6 +462,9 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
463462
else:
464463
if args.len > 1 and args[1][0].eqIdent "self":
465464
markSelfType
465+
if args[0].eqIdent"None":
466+
args[0] = getNoneTypeNode()
467+
pragmas.add ident"discardable"
466468
# Function body
467469
var docNode: NimNode
468470
var parsedbody = recReplaceSuperCall(
@@ -488,7 +490,11 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
488490
newMethProc(topLevel, procName, generics_cpy, args, body,
489491
procType, pragmas=pragmas)
490492
)
491-
addMeth nDef
493+
if args[0].eqIdent"auto":
494+
# return type 'auto' cannot be used in forward declarations
495+
addMeth nDef
496+
else:
497+
addMethWithDecl nDef
492498

493499
of nnkStrLit, nnkRStrLit, nnkTripleStrLit:
494500
result.add newCommentStmtNode $def
@@ -498,7 +504,7 @@ so if wantting the attr inherited from SupCls, just write it as-is (e.g. `self.a
498504
else:
499505
result.add def # AS-IS
500506

501-
addMeth genNewCls(topLevel, ident("new" & classId.strVal), classId, initGenerics, initArgs, initPragmas)
507+
addMethWithDecl genNewCls(topLevel, ident("new" & classId.strVal), classId, initGenerics, initArgs, initPragmas)
502508

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

0 commit comments

Comments
 (0)