From b688250202ea479a51cbd20003e2ea2a147a3c3d Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 29 Nov 2019 11:55:50 +0100 Subject: [PATCH] fixes #12766 --- compiler/injectdestructors.nim | 20 ++++--- tests/destructor/const_smart_ptr.nim | 77 +++++++++++++++++++++++++++ tests/destructor/tconst_smart_ptr.nim | 7 +++ 3 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 tests/destructor/const_smart_ptr.nim create mode 100644 tests/destructor/tconst_smart_ptr.nim diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index 7fc2ccd784f34..de723b776094f 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -239,7 +239,7 @@ proc getTemp(c: var Con; typ: PType; info: TLineInfo): PNode = proc genWasMoved(n: PNode; c: var Con): PNode = result = newNodeI(nkCall, n.info) result.add(newSymNode(createMagic(c.graph, "wasMoved", mWasMoved))) - result.add n #mWasMoved does not take the address + result.add copyTree(n) #mWasMoved does not take the address proc genDefaultCall(t: PType; c: Con; info: TLineInfo): PNode = result = newNodeI(nkCall, info) @@ -401,6 +401,7 @@ proc pArg(arg: PNode; c: var Con; isSink: bool): PNode = # different and can deal with 'const string sunk into var'. result = passCopyToSink(arg, c) elif arg.kind in nkLiterals: + # literals are save to share accross ASTs (for now!) result = arg # literal to sink parameter: nothing to do elif arg.kind in {nkBracket, nkObjConstr, nkTupleConstr, nkClosure}: # object construction to sink parameter: nothing to do @@ -497,19 +498,22 @@ proc p(n: PNode; c: var Con; consumed = false): PNode = of nkCallKinds: let parameters = n[0].typ let L = if parameters != nil: parameters.len else: 0 + result = shallowCopy(n) for i in 1.. 0: + result.data = cast[ptr UncheckedArray[float]](createShared(float, size)) + result.setTo(initial_value) + +#---------------------------------------------------------------------- + + +proc test*(x1: int): ConstPtr[MySeqNonCopyable] {.inline.} = # remove inline here to make it work as expected + if x1 == 0: + let x = newMySeq(1, 0.0) + result = newConstPtr(x) + else: + let y = newMySeq(x1, 0.0) + result = newConstPtr(y) + +discard test(10) diff --git a/tests/destructor/tconst_smart_ptr.nim b/tests/destructor/tconst_smart_ptr.nim new file mode 100644 index 0000000000000..39fe12612b0dc --- /dev/null +++ b/tests/destructor/tconst_smart_ptr.nim @@ -0,0 +1,7 @@ +discard """ + action: "compile" +""" + +import const_smart_ptr + +discard test(0)