From b75ad05267bad6f10e1939354eac14ac821fb8c6 Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 24 May 2019 00:12:36 +0200 Subject: [PATCH] fixes move analyser; refs #11257 --- compiler/injectdestructors.nim | 10 +++++----- tests/destructor/tconsume_twice.nim | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 tests/destructor/tconsume_twice.nim diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index a02621aa3..b1a5e63c0 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -178,13 +178,13 @@ proc isLastRead(location: PNode; c: var Con; pc, comesFrom: int): int = pc = pc + c.g[pc].dest of fork: # every branch must lead to the last read of the location: - var variantA = isLastRead(location, c, pc+1, pc) + let variantA = isLastRead(location, c, pc+1, pc) if variantA < 0: return -1 - let variantB = isLastRead(location, c, pc + c.g[pc].dest, pc) + var variantB = isLastRead(location, c, pc + c.g[pc].dest, pc) if variantB < 0: return -1 - elif variantA == high(int): - variantA = variantB - pc = variantA + elif variantB == high(int): + variantB = variantA + pc = variantB of InstrKind.join: let dest = pc + c.g[pc].dest if dest == comesFrom: return pc + 1 diff --git a/tests/destructor/tconsume_twice.nim b/tests/destructor/tconsume_twice.nim new file mode 100644 index 000000000..a06ea4efd --- /dev/null +++ b/tests/destructor/tconsume_twice.nim @@ -0,0 +1,12 @@ +discard """ + cmd: "nim c --newruntime $file" + errormsg: "sink parameter `a` is already consumed at tconsume_twice.nim(8, 6)" + line: 10 +""" + +proc consumeTwice(a: owned proc()): owned proc() = + if a == nil: + return + return a + +assert consumeTwice(proc() = discard) != nil