Skip to content

fixes #13026 #13028

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

Merged
merged 1 commit into from
Jan 4, 2020
Merged
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
2 changes: 1 addition & 1 deletion compiler/sempass2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ proc trackProc*(c: PContext; s: PSym, body: PNode) =
for i in 1..<params.len:
let param = params[i].sym
if isSinkTypeForParam(param.typ):
createTypeBoundOps(t.graph, t.c, param.typ, param.info)
createTypeBoundOps(t, param.typ, param.info)

if not isEmptyType(s.typ[0]) and
({tfNeedsInit, tfNotNil} * s.typ[0].flags != {} or
Expand Down
84 changes: 83 additions & 1 deletion tests/destructor/ttuple.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

discard """
output: '''5.0 10.0'''
output: '''5.0 10.0
=destroy
=destroy
'''
"""

type
Expand Down Expand Up @@ -46,3 +49,82 @@ let h = MyOpt[float](has: true, val: 5.0)
myproc(h)


#-------------------------------------------------------------
type
MyObject* = object
len*: int
amount: UncheckedArray[float]

MyObjPtr* = ptr MyObject

MyObjContainer* {.byref.} = object
size1: int
size2: int
data: ptr UncheckedArray[MyObjPtr]


proc size1*(m: MyObjContainer): int {.inline.} = m.size1
proc size2*(m: MyObjContainer): int {.inline.} = m.size2

proc allocateMyObjPtr(size2: int): MyObjPtr =
cast[MyObjPtr](allocShared(sizeof(MyObject) + sizeof(float) * size2.int))

proc `=destroy`*(m: var MyObjContainer) {.inline.} =
if m.data != nil:
for i in 0..<m.size1:
if m.data[i] != nil:
deallocShared(m.data[i])
m.data[i] = nil
deallocShared(m.data)
echo "=destroy"
m.data = nil

proc `=sink`*(m: var MyObjContainer, m2: MyObjContainer) {.inline.} =
if m.data != m2.data:
`=destroy`(m)
m.size1 = m2.size1
m.size2 = m2.size2
m.data = m2.data


proc `=`*(m: var MyObjContainer, m2: MyObjContainer) {.error.}
## non copyable

func newMyObjContainer*(size2: Natural): MyObjContainer =
result.size2 = size2

proc push(m: var MyObjContainer, cf: MyObjPtr) =
## Add MyObjPtr to MyObjContainer, shallow copy
m.size1.inc
m.data = cast[ptr UncheckedArray[MyObjPtr]](reallocShared(m.data, m.size1 * sizeof(MyObjPtr)))
m.data[m.size1 - 1] = cf


proc add*(m: var MyObjContainer, amount: float) =
assert m.size2 > 0, "MyObjContainer is not initialized, use newMyObjContainer() to initialize object before use"
let cf = allocateMyObjPtr(m.size2)
for i in 0..<m.size2:
cf.amount[i.int] = amount

m.push(cf)

proc add*(dest: var MyObjContainer, src: sink MyObjContainer) =
# merge containers

for i in 0..<src.size1:
dest.push src.data[i]
src.data[i] = nil


proc test =
var cf1 = newMyObjContainer(100)
cf1.add(1)
cf1.add(2)

var cf3 = newMyObjContainer(100)
cf3.add(2)
cf3.add(3)

cf1.add(cf3)

test()