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 itstasksblock, such as one of the loop body. Join the task in the same block:let h = spawn { .. };and thenh.join();. Areturn,breakorcontinuethat leaves twotasksblocks 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
Ownership and borrowingThe chapter covering the rule this code enforces.
All diagnosticsEvery code the compiler can emit, grouped by stage.
E4013A field moved out of a value that must keep it:
let x = p.a where p's type implements Drop, whose drop needs every field, or let x = make().a, where the value has no name and drops all its fields at the end of the statement. Borrow the field (&p.a), clone it, or bind the value to a variable first.
E4015A spawn region that moves a value from the code around it: let x = t; or f(t) with a parameter that takes t by value, where t is declared before the region. The region only borrows what it uses, and the function drops t after the region. Borrow it (&t), or clone it (t.clone()).