Skip to content

Commit

Permalink
small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zerbina committed Feb 15, 2023
1 parent 6898e92 commit 3d10776
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 35 deletions.
3 changes: 1 addition & 2 deletions compiler/sem/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ import
aliasanalysis,
liftdestructors,
mirexec,
sighashes,
varpartitions
sighashes
],
compiler/utils/[
cursors,
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/tmir_exec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ proc useChain(p: Program, defId, start: int): seq[int] =
## Computes and returns the 'use's connected to the 'use' with ID `start`.
## Reaching `defId` breaks the chain. The list is sorted in post-order.
## Also validates that each instruction is really only visited once.
# TODO: remove `tree` one traversal is separated from the MIR
# TODO: remove `tree` once traversal is separated from the MIR
var
tree = newSeq[MirNode](p.code.len)
visited = newSeq[bool](p.code.len)
Expand Down
14 changes: 7 additions & 7 deletions tests/lang_objects/destructor/mhelper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ type
## Something that represents a resource
content*: bool

Value* = object
Value*[T] = object
has: bool
content*: int
content*: T

var numCopies*, numSinks*, numDestroy*: int

Expand All @@ -32,19 +32,19 @@ proc `=sink`(x: var Resource, y: Resource) =

## ------ `Value` hooks

proc `=destroy`(x: var Value) =
proc `=destroy`[T](x: var Value[T]) =
if x.has:
inc numDestroy

proc `=copy`(x: var Value, y: Value) =
proc `=copy`[T](x: var Value[T], y: Value[T]) =
`=destroy`(x)
wasMoved(x)
if y.has:
inc numCopies
x.has = true
x.content = y.content

proc `=sink`(x: var Value, y: Value) =
proc `=sink`[T](x: var Value[T], y: Value[T]) =
`=destroy`(x)
wasMoved(x)
if y.has:
Expand All @@ -69,8 +69,8 @@ template test*(name, code: untyped) =
template initResource*(): untyped =
Resource(content: true)

template initValue*(x: int): untyped =
Value(has: true, content: x)
template initValue*[T](x: T): untyped =
Value[T](has: true, content: x)


func use*[T](x: T) =
Expand Down
1 change: 1 addition & 0 deletions tests/lang_objects/destructor/tglobals.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
discard """
description: "Tests for globals that have lifetime hooks attached"
targets: "c js !vm"
matrix: "--cursorInference:off"
"""

import mhelper except test
Expand Down
32 changes: 7 additions & 25 deletions tests/lang_objects/destructor/tview_ownership.nim
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
discard """
description: "Tests that views are correctly treated as not owning"
matrix: "--gc:arc"
targets: c
matrix: "--gc:arc --cursorInference:off"
targets: "c !js !vm"
"""

type
Resource[T] = object
hasContent: bool
content: T

var numCopies, numDestroy: int

proc `=destroy`[T](x: var Resource[T]) =
if x.hasContent:
inc numDestroy
`=destroy`(x.content)

proc `=copy`[T](x: var Resource[T], y: Resource[T]) =
`=destroy`(x)
if y.hasContent:
inc numCopies
x.hasContent = true
x.content = y.content

proc initResource[T](x: T): Resource[T] =
Resource[T](hasContent: true, content: x)
import mhelper

template testCase(name: untyped, pairs: array, code: untyped) {.dirty.} =
block:
numCopies = 0
numDestroy = 0

# wrap the code in a procedure so that variables defined in it are not
# globals (which would interfere with the test)
proc inner() =
code

Expand All @@ -40,12 +22,12 @@ template testCase(name: untyped, pairs: array, code: untyped) {.dirty.} =

testCase "openarray", [(0, 2), (1, 3)]:
let
x = [initResource("a"), initResource("b")]
x = [initValue("a"), initValue("b")]
y = toOpenArray(x, 0, 1)[0] # may copy

testCase "openarray", [(1, 3)]:
var
x = [initResource("a"), initResource("b")]
x = [initValue("a"), initValue("b")]
y = toOpenArray(x, 0, 1)[0] # must copy

# modify the source location so that a copy is required:
Expand Down

0 comments on commit 3d10776

Please sign in to comment.