E4014

E4014 — A task that can still run when a variable that it uses ends or changes. A task in a loop that the same pass does not join runs at the same time as the next pass, so it must not write a variable from outside the loop, or read one that the loop writes. A task must not use a variable that ends before its tasks block, such as one of the loop body. Join the task in the same block: let h = spawn { .. }; and then h.join();. A return, break or continue that leaves two tasks blocks waits only for the tasks of the inner one, so it cannot drop a value from outside the inner block yet.

What the compiler reports

Error[E4014] at {{.*}}.vx:[[@LINE+11]]:{{[0-9]+}}: this `return` leaves two `tasks` blocks, and it drops 'n' first

The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.

A program that triggers it

//
// Part of the Vx Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// A `return` inside a `tasks` block waits for the tasks of that block before it drops
// anything. A `return` that also leaves an outer `tasks` block does not wait for the tasks
// of the outer block. So it cannot drop a value from outside the inner block yet.
//

struct Noisy {
  id : i32,
}

impl Drop for Noisy {
  fn drop(self : &mut Noisy) -> void {
    println!("dropped {}", self.id);
  }
}

fn look(n : &Noisy) -> void {
  println!("{}", n.id);
}

fn leave_two(stop : bool) -> i32 {
  let n = Noisy {
    id: 1
  };
  tasks {
    spawn {
      look(&n);
    };
    tasks {
      if stop {
        return 1;
      }
    }
  }
  return n.id;
}

fn main() -> i32 {
  return leave_two(true);
}

From tests/frontend/fail/return_that_leaves_two_tasks_blocks_and_drops_a_value.vx, which asserts this diagnostic on every commit.

Related