]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-bad-nested-calls-free.rs
Rollup merge of #106644 - alexcrichton:update-wasi-toolchain, r=cuviper
[rust.git] / tests / ui / borrowck / borrowck-bad-nested-calls-free.rs
1 // Test that we detect nested calls that could free pointers evaluated
2 // for earlier arguments.
3
4
5
6 fn rewrite(v: &mut Box<usize>) -> usize {
7     *v = Box::new(22);
8     **v
9 }
10
11 fn add(v: &usize, w: usize) -> usize {
12     *v + w
13 }
14
15 fn implicit() {
16     let mut a: Box<_> = Box::new(1);
17
18     // Note the danger here:
19     //
20     //    the pointer for the first argument has already been
21     //    evaluated, but it gets freed when evaluating the second
22     //    argument!
23     add(
24         &*a,
25         rewrite(&mut a)); //~ ERROR cannot borrow
26 }
27
28 fn explicit() {
29     let mut a: Box<_> = Box::new(1);
30     add(
31         &*a,
32         rewrite(&mut a)); //~ ERROR cannot borrow
33 }
34
35 fn main() {}