2 //[nll]compile-flags: -Z borrowck=mir
4 //[g2p]compile-flags: -Z borrowck=mir -Z two-phase-beyond-autoref
5 // the above revision is disabled until two-phase-beyond-autoref support is better
7 // This is a test checking that when we limit two-phase borrows to
8 // method receivers, we do not let other kinds of auto-ref to leak
11 // The g2p revision illustrates the "undesirable" behavior you would
12 // otherwise observe without limiting the phasing to autoref on method
13 // receivers (namely, in many cases demonstrated below, the error
16 use std::ops::{Index, IndexMut};
18 fn foo(x: &mut u32, y: u32) {
22 fn deref_coercion(x: &mut u32) {
24 // Above error is a known limitation of AST borrowck
27 // While adding a flag to adjustments (indicating whether they
28 // should support two-phase borrows, here are the cases I
31 // - [x] Resolving overloaded_call_traits (call, call_mut, call_once)
32 // - [x] deref_coercion (shown above)
33 // - [x] coerce_unsized e.g., `&[T; n]`, `&mut [T; n] -> &[T]`,
34 // `&mut [T; n] -> &mut [T]`, `&Concrete -> &Trait`
35 // - [x] Method Call Receivers (the case we want to support!)
36 // - [x] ExprKind::Index and ExprKind::Unary Deref; only need to handle coerce_index_op
37 // - [x] overloaded_binops
39 fn overloaded_call_traits() {
40 // Regarding overloaded call traits, note that there is no
41 // scenario where adding two-phase borrows should "fix" these
42 // cases, because either we will resolve both invocations to
43 // `call_mut` (in which case the inner call requires a mutable
44 // borrow which will conflict with the outer reservation), or we
45 // will resolve both to `call` (which will just work, regardless
46 // of two-phase borrow support), or we will resolve both to
47 // `call_once` (in which case the inner call requires moving the
48 // receiver, invalidating the outer call).
50 fn twice_ten_sm<F: FnMut(i32) -> i32>(f: &mut F) {
52 //[nll]~^ ERROR cannot borrow `*f` as mutable more than once at a time
53 //[g2p]~^^ ERROR cannot borrow `*f` as mutable more than once at a time
55 fn twice_ten_si<F: Fn(i32) -> i32>(f: &mut F) {
58 fn twice_ten_so<F: FnOnce(i32) -> i32>(f: Box<F>) {
60 //[nll]~^ ERROR use of moved value: `f`
61 //[g2p]~^^ ERROR use of moved value: `f`
64 fn twice_ten_om(f: &mut dyn FnMut(i32) -> i32) {
66 //[nll]~^ ERROR cannot borrow `*f` as mutable more than once at a time
67 //[g2p]~^^ ERROR cannot borrow `*f` as mutable more than once at a time
69 fn twice_ten_oi(f: &mut dyn Fn(i32) -> i32) {
72 fn twice_ten_oo(f: Box<dyn FnOnce(i32) -> i32>) {
74 //[nll]~^ ERROR use of moved value: `f`
75 //[g2p]~^^ ERROR use of moved value: `f`
78 twice_ten_sm(&mut |x| x + 1);
79 twice_ten_si(&mut |x| x + 1);
80 twice_ten_so(Box::new(|x| x + 1));
81 twice_ten_om(&mut |x| x + 1);
82 twice_ten_oi(&mut |x| x + 1);
83 twice_ten_oo(Box::new(|x| x + 1));
87 fn m(&mut self, x: i32) -> i32 { x + 1 }
88 fn i(&self, x: i32) -> i32 { x + 1 }
93 impl TwoMethods for T { }
98 fn m(&mut self, x: i32) -> i32 { x + 1 }
99 fn i(&self, x: i32) -> i32 { x + 1 }
102 impl TwoMethods for [i32; 3] { }
104 fn double_access<X: Copy>(m: &mut [X], s: &[X]) {
108 fn coerce_unsized() {
109 let mut a = [1, 2, 3];
112 double_access(&mut a, &a);
113 //[nll]~^ ERROR cannot borrow `a` as immutable because it is also borrowed as mutable [E0502]
114 //[g2p]~^^ ERROR cannot borrow `a` as immutable because it is also borrowed as mutable [E0502]
118 // Above error is an expected limitation of AST borrowck
123 impl Index<i32> for I {
125 fn index(&self, _: i32) -> &i32 {
130 impl IndexMut<i32> for I {
131 fn index_mut(&mut self, _: i32) -> &mut i32 {
136 fn coerce_index_op() {
139 //[nll]~^ ERROR cannot borrow `i` as immutable because it is also borrowed as mutable [E0502]
144 //[nll]~^ ERROR cannot borrow `i` as immutable because it is also borrowed as mutable [E0502]
149 // As a reminder, this is the basic case we want to ensure we handle.
150 let mut v = vec![1, 2, 3];
152 // Error above is an expected limitation of AST borrowck
154 // (as a rule, pnkfelix does not like to write tests with dead code.)
156 deref_coercion(&mut 5);
157 overloaded_call_traits();
162 // Error above is an expected limitation of AST borrowck
166 // Error above is an expected limitation of AST borrowck