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