W1034
W1034 — The program writes a variable that a closure copied, and uses the closure after the write. The closure keeps the value that it copied, so it does not see the write. A later release makes this an error (K4).
What the compiler reports
Warning[W1034] at {{.*}}writes_that_a_closure_does_not_see.vx:[[@LINE+4]]:3: the closure in 'add' copied 'x' on line [[#@LINE+3]], so the closure does not see this write
Warning[W1034] at {{.*}}writes_that_a_closure_does_not_see.vx:[[@LINE+4]]:15: the closure in 'get' copied 'n' on line [[#@LINE+3]], so the closure does not see this write
Warning[W1034] at {{.*}}writes_that_a_closure_does_not_see.vx:[[@LINE+4]]:3: the closure in 'add' copied 'x' on line [[#@LINE+3]], so the closure does not see this write
{{^}}11{{$}}
{{^}}1{{$}}
{{^}}4{{$}}
{{^}}22{{$}}
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 closure copies a variable that it does not move. A write to the variable before the last use
// of the closure does not change the closure's copy. The compiler warns about such a write. A
// write after the last use of the closure gets no warning.
fn set_to(p : &mut i32, value : i32) -> void {
*p = value;
}
// The closure copied 10, so `add(1)` is 11.
fn write_before_the_last_use() -> i32 {
let mut x = 10;
let add = |y : i32| x + y;
x = 20;
return add(1);
}
// A write through `&mut` is a write too.
fn write_through_a_reference() -> i32 {
let mut n = 1;
let get = |y : i32| n + y;
set_to(&mut n, 5);
return get(0);
}
// A closure whose body has a `let` of its own.
fn closure_with_a_let_in_its_body() -> i32 {
let mut x = 3;
let add = |y : i32| unsafe { let z = y + 1; x + z };
x = 4;
return add(0);
}
// No warning: a write after the last use of the closure, a write to a variable that the closure
// does not use, and a write to a new variable of the same name.
fn writes_with_no_warning() -> i32 {
let mut x = 1;
let add = |y : i32| x + y;
let first = add(1);
x = 6;
let x0 = x;
let add_x0 = |y : i32| x0 + y;
x = 7;
let add_x = |y : i32| x + y;
for i in 0..2 {
let mut x = 8;
x += i;
}
return first + add_x0(0) + x + add_x(0);
}
fn main() -> i32 {
println!("{}", write_before_the_last_use());
println!("{}", write_through_a_reference());
println!("{}", closure_with_a_let_in_its_body());
println!("{}", writes_with_no_warning());
return 0;
}
// The results: 10 + 1, 1 + 0, 3 + 0 + 1, and 2 + 6 + 7 + 7.
From tests/frontend/pass/writes_that_a_closure_does_not_see.vx, which asserts this diagnostic on every commit.
Related
A tour of VxThe chapter covering the rule this code enforces.
All diagnosticsEvery code the compiler can emit, grouped by stage.
W1033The loop of a
spawn region runs on one thread although each pass writes only memory of its own, so its passes could run at the same time. The warning points at what stops it: a call the compiler cannot show writes nothing, inline MLIR, a read of a row another pass writes, a break out of the loop, and the like. A region that calls barrier() asks for blocks and threads, so it is warned about whenever it runs on one thread, with the first thing that stopped the split: two threads that may write the same element, a break out of a loop that holds a barrier(), a write to block-shared memory with no barrier() after it, and the like.
W1035A match arm in a loop or while can take Cancelled from a wait and stays in the loop. In a cancelled task each wait gives Cancelled at once, so the loop never ends (F4).