15ed94e5903d — Leonard Ritter a month ago
* fixed accidental double drops when returning from nested expressions
2 files changed, 17 insertions(+), 8 deletions(-)

M src/prover.cpp
M testing/test_borrowing.sc
M src/prover.cpp +7 -0
@@ 744,6 744,13 @@ static SCOPES_RESULT(void) move_merge_va
         SCOPES_CHECK_RESULT(drop_values(ctx, mover, todrop));
     }
 
+    // HACK: do not keep returned values visible to parent scopes that are cleaning up
+    if (retdepth == 0) {
+        for (auto id : saved) {
+            ctx.move(id, mover);
+        }
+    }
+
     return {};
 }
 

          
M testing/test_borrowing.sc +10 -8
@@ 561,18 561,20 @@ do
 
 # regression: return in a do-block or label causes double free
      produces: assertion failed: (_refcount >= 0)
-#fn testfunc ()
-    #label ok
-        label ok2
-            merge ok (One 303)
+fn testfunc ()
     fn testf ()
         do
-
-            return (One 304)
-                do
-                    return (One 303)
+            return (One 303)
     testf;
     ;
+testfunc;
+One.test-refcount-balanced;
+
+# error: cannot access value of type (uniqueof One 1000) because it has been moved
+#fn testfunc ()
+    label ok
+        label ok2
+            merge ok (One 303)
 #testfunc;
 #One.test-refcount-balanced;