diff --git a/compiler/commands.nim b/compiler/commands.nim index ce50b1a791..bae1fda386 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -666,6 +666,10 @@ proc processSwitch(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; expectArg(switch, arg, pass, info) if config != nil: config.cppDefine(arg) + of "newruntime": + expectNoArg(switch, arg, pass, info) + newDestructors = true + defineSymbol("nimNewRuntime") else: if strutils.find(switch, '.') >= 0: options.setConfigVar(switch, arg) else: invalidCmdLineOption(pass, switch, info) diff --git a/compiler/destroyer.nim b/compiler/destroyer.nim index a650df7dd0..e8db4bc2b6 100644 --- a/compiler/destroyer.nim +++ b/compiler/destroyer.nim @@ -290,4 +290,4 @@ proc injectDestructorCalls*(owner: PSym; n: PNode): PNode = else: result.add body - echo "transformed into: ", result + #echo "transformed into: ", result diff --git a/compiler/options.nim b/compiler/options.nim index 49a317582b..86e6006d76 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -14,7 +14,6 @@ const hasTinyCBackend* = defined(tinyc) useEffectSystem* = true useWriteTracking* = false - newDestructors* = defined(nimV2) hasFFI* = defined(useFFI) newScopeForIf* = true useCaas* = not defined(noCaas) @@ -145,6 +144,7 @@ var isServing*: bool = false gNoNimblePath* = false gExperimentalMode*: bool + newDestructors*: bool proc importantComments*(): bool {.inline.} = gCmd in {cmdDoc, cmdIdeTools} proc usesNativeGC*(): bool {.inline.} = gSelectedGC >= gcRefc diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index e8989f8328..2499c2ab67 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1383,7 +1383,7 @@ proc semAsgn(c: PContext, n: PNode; mode=asgnNormal): PNode = typeMismatch(n.info, lhs.typ, rhs.typ) n.sons[1] = fitNode(c, le, rhs, n.info) - when not newDestructors: + if not newDestructors: if tfHasAsgn in lhs.typ.flags and not lhsIsResult and mode != noOverloadedAsgn: return overloadedAsgn(c, lhs, n.sons[1]) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index c8eda9d941..b65443b2fc 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -100,7 +100,7 @@ proc semProc(c: PContext, n: PNode): PNode include semdestruct proc semDestructorCheck(c: PContext, n: PNode, flags: TExprFlags) {.inline.} = - when not newDestructors: + if not newDestructors: if efAllowDestructor notin flags and n.kind in nkCallKinds+{nkObjConstr,nkBracket}: if instantiateDestructor(c, n.typ) != nil: @@ -608,7 +608,7 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode = if def.kind == nkPar: v.ast = def[j] setVarType(v, tup.sons[j]) b.sons[j] = newSymNode(v) - when not newDestructors: addDefer(c, result, v) + if not newDestructors: addDefer(c, result, v) checkNilable(v) if sfCompileTime in v.flags: hasCompileTime = true if hasCompileTime: vm.setupCompileTimeVar(c.module, c.cache, result) @@ -1278,9 +1278,8 @@ proc semOverride(c: PContext, s: PSym, n: PNode) = case s.name.s.normalize of "destroy", "=destroy": doDestructorStuff(c, s, n) - when not newDestructors: - if not experimentalMode(c): - localError n.info, "use the {.experimental.} pragma to enable destructors" + if not newDestructors and not experimentalMode(c): + localError n.info, "use the {.experimental.} pragma to enable destructors" incl(s.flags, sfUsed) of "deepcopy", "=deepcopy": if s.typ.len == 2 and diff --git a/compiler/transf.nim b/compiler/transf.nim index 2a33c60a61..fa468d4933 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -43,7 +43,7 @@ type inlining: int # > 0 if we are in inlining context (copy vars) nestedProcs: int # > 0 if we are in a nested proc contSyms, breakSyms: seq[PSym] # to transform 'continue' and 'break' - deferDetected, tooEarly: bool + deferDetected, tooEarly, needsDestroyPass: bool PTransf = ref TTransfContext proc newTransNode(a: PNode): PTransNode {.inline.} = @@ -780,7 +780,8 @@ proc transform(c: PTransf, n: PNode): PTransNode = nkBlockStmt, nkBlockExpr}: oldDeferAnchor = c.deferAnchor c.deferAnchor = n - + if n.typ != nil and tfHasAsgn in n.typ.flags: + c.needsDestroyPass = true case n.kind of nkSym: result = transformSym(c, n) @@ -972,7 +973,7 @@ proc transformBody*(module: PSym, n: PNode, prc: PSym): PNode = #result = liftLambdas(prc, result) incl(result.flags, nfTransf) when useEffectSystem: trackProc(prc, result) - if prc.kind == skFunc: + if c.needsDestroyPass and newDestructors: result = injectDestructorCalls(prc, result) #if prc.name.s == "testbody": # echo renderTree(result) @@ -989,6 +990,8 @@ proc transformStmt*(module: PSym, n: PNode): PNode = when useEffectSystem: trackTopLevelStmt(module, result) #if n.info ?? "temp.nim": # echo renderTree(result, {renderIds}) + if c.needsDestroyPass and newDestructors: + result = injectDestructorCalls(module, result) proc transformExpr*(module: PSym, n: PNode): PNode = if nfTransf in n.flags: @@ -998,3 +1001,5 @@ proc transformExpr*(module: PSym, n: PNode): PNode = result = processTransf(c, n, module) liftDefer(c, result) incl(result.flags, nfTransf) + if c.needsDestroyPass and newDestructors: + result = injectDestructorCalls(module, result)