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

Non-var destructors become the default #24222

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

## Changes affecting backward compatibility

- Non-var destructors become the default. Use `-d:nimLegacyVarDestructor` to emulate old behaviors.

## Standard library additions and changes

Expand Down
14 changes: 6 additions & 8 deletions compiler/liftdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ template asink*(t: PType): PSym = getAttachedOp(c.g, t, attachedSink)

proc fillBody(c: var TLiftCtx; t: PType; body, x, y: PNode)
proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp;
info: TLineInfo; idgen: IdGenerator; isDistinct = false): PSym
info: TLineInfo; idgen: IdGenerator): PSym

proc createTypeBoundOps*(g: ModuleGraph; c: PContext; orig: PType; info: TLineInfo;
idgen: IdGenerator)
Expand Down Expand Up @@ -1063,9 +1063,7 @@ proc produceSymDistinctType(g: ModuleGraph; c: PContext; typ: PType;
assert typ.kind == tyDistinct
let baseType = typ.elementType
if getAttachedOp(g, baseType, kind) == nil:
# TODO: fixme `isDistinct` is a fix for #23552; remove it after
# `-d:nimPreviewNonVarDestructor` becomes the default
discard produceSym(g, c, baseType, kind, info, idgen, isDistinct = true)
discard produceSym(g, c, baseType, kind, info, idgen)
result = getAttachedOp(g, baseType, kind)
setAttachedOp(g, idgen.module, typ, kind, result)

Expand Down Expand Up @@ -1104,7 +1102,7 @@ proc symDupPrototype(g: ModuleGraph; typ: PType; owner: PSym; kind: TTypeAttache
incl result.flags, sfGeneratedOp

proc symPrototype(g: ModuleGraph; typ: PType; owner: PSym; kind: TTypeAttachedOp;
info: TLineInfo; idgen: IdGenerator; isDiscriminant = false; isDistinct = false): PSym =
info: TLineInfo; idgen: IdGenerator; isDiscriminant = false): PSym =
if kind == attachedDup:
return symDupPrototype(g, typ, owner, kind, info, idgen)

Expand All @@ -1115,7 +1113,7 @@ proc symPrototype(g: ModuleGraph; typ: PType; owner: PSym; kind: TTypeAttachedOp
idgen, result, info)

if kind == attachedDestructor and g.config.selectedGC in {gcArc, gcOrc, gcAtomicArc} and
((g.config.isDefined("nimPreviewNonVarDestructor") and not isDiscriminant) or (typ.kind in {tyRef, tyString, tySequence} and not isDistinct)):
not g.config.isDefined("nimLegacyVarDestructor") and not isDiscriminant:
dest.typ = typ
else:
dest.typ = makeVarType(typ.owner, typ, idgen)
Expand Down Expand Up @@ -1157,13 +1155,13 @@ proc genTypeFieldCopy(c: var TLiftCtx; t: PType; body, x, y: PNode) =
body.add newAsgnStmt(xx, yy)

proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp;
info: TLineInfo; idgen: IdGenerator; isDistinct = false): PSym =
info: TLineInfo; idgen: IdGenerator): PSym =
if typ.kind == tyDistinct:
return produceSymDistinctType(g, c, typ, kind, info, idgen)

result = getAttachedOp(g, typ, kind)
if result == nil:
result = symPrototype(g, typ, typ.owner, kind, info, idgen, isDistinct = isDistinct)
result = symPrototype(g, typ, typ.owner, kind, info, idgen)

var a = TLiftCtx(info: info, g: g, kind: kind, c: c, asgnForType: typ, idgen: idgen,
fn: result)
Expand Down
1 change: 0 additions & 1 deletion compiler/nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ define:nimPreviewSlimSystem
define:nimPreviewCstringConversion
define:nimPreviewProcConversion
define:nimPreviewRangeDefault
define:nimPreviewNonVarDestructor
threads:off

#import:"$projectpath/testability"
Expand Down
10 changes: 1 addition & 9 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ proc arrPut[I: Ordinal;T,S](a: T; i: I;
const arcLikeMem = defined(gcArc) or defined(gcAtomicArc) or defined(gcOrc)


when defined(nimAllowNonVarDestructor) and arcLikeMem and defined(nimPreviewNonVarDestructor):
when defined(nimAllowNonVarDestructor) and arcLikeMem and not defined(nimLegacyVarDestructor):
proc `=destroy`*[T](x: T) {.inline, magic: "Destroy".} =
## Generic `destructor`:idx: implementation that can be overridden.
discard
Expand All @@ -374,15 +374,7 @@ else:
## Generic `destructor`:idx: implementation that can be overridden.
discard

when defined(nimAllowNonVarDestructor) and arcLikeMem:
proc `=destroy`*(x: string) {.inline, magic: "Destroy", enforceNoRaises.} =
discard

proc `=destroy`*[T](x: seq[T]) {.inline, magic: "Destroy".} =
discard

proc `=destroy`*[T](x: ref T) {.inline, magic: "Destroy".} =
discard

when defined(nimHasDup):
proc `=dup`*[T](x: T): T {.inline, magic: "Dup".} =
Expand Down
2 changes: 1 addition & 1 deletion tests/arc/thard_alignment.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discard """
disabled: "arm64"
cmd: "nim c --mm:arc -u:nimPreviewNonVarDestructor $file"
cmd: "nim c --mm:arc -d:nimLegacyVarDestructor $file"
output: "y"
"""

Expand Down
1 change: 0 additions & 1 deletion tests/config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ switch("define", "nimPreviewFloatRoundtrip")
switch("define", "nimPreviewJsonutilsHoleyEnum")
switch("define", "nimPreviewHashRef")
switch("define", "nimPreviewRangeDefault")
switch("define", "nimPreviewNonVarDestructor")

switch("warningAserror", "UnnamedBreak")
when not defined(testsConciseTypeMismatch):
Expand Down
2 changes: 1 addition & 1 deletion tests/destructor/tdistinctseq.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "-u:nimPreviewNonVarDestructor;"
matrix: "-d:nimLegacyVarDestructor;"
"""
type DistinctSeq* = distinct seq[int]

Expand Down
Loading