1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
//@ edition: 2024
use std::fmt::Display;
fn display_len<T>(x: &Vec<T>) -> impl Display {
//~^ NOTE in this expansion of desugaring of `impl Trait`
//~| NOTE in this expansion of desugaring of `impl Trait`
//~| NOTE in this expansion of desugaring of `impl Trait`
//~| NOTE in this expansion of desugaring of `impl Trait`
//~| NOTE in this expansion of desugaring of `impl Trait`
x.len()
}
fn conflicting_borrow() {
let mut x = vec![];
let a = display_len(&x);
//~^ NOTE this call may capture more lifetimes than intended
//~| NOTE immutable borrow occurs here
x.push(1);
//~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
//~| NOTE mutable borrow occurs here
println!("{a}");
//~^ NOTE immutable borrow later used here
}
fn needs_static() {
let x = vec![1];
//~^ NOTE binding `x` declared here
let a = display_len(&x);
//~^ ERROR `x` does not live long enough
//~| NOTE this call may capture more lifetimes than intended
//~| NOTE argument requires that `x` is borrowed for `'static`
//~| NOTE borrowed value does not live long enoug
fn needs_static(_: impl Sized + 'static) {}
needs_static(a);
}
//~^ NOTE `x` dropped here while still borrowed
fn is_moved() {
let x = vec![1];
//~^ NOTE binding `x` declared here
let a = display_len(&x);
//~^ NOTE this call may capture more lifetimes than intended
//~| NOTE borrow of `x` occurs here
fn mv(_: impl Sized) {}
mv(x);
//~^ ERROR cannot move out of `x` because it is borrowed
//~| NOTE move out of `x` occurs here
}
//~^ NOTE borrow might be used here, when `a` is dropped
fn display_len_mut<T>(x: &mut Vec<T>) -> impl Display {
//~^ NOTE in this expansion of desugaring of `impl Trait`
//~| NOTE in this expansion of desugaring of `impl Trait`
//~| NOTE in this expansion of desugaring of `impl Trait`
x.len()
}
fn conflicting_borrow_mut() {
let mut x = vec![];
let a = display_len_mut(&mut x);
//~^ NOTE this call may capture more lifetimes than intended
//~| NOTE first mutable borrow occurs here
x.push(1);
//~^ ERROR cannot borrow `x` as mutable more than once
//~| NOTE second mutable borrow occurs here
println!("{a}");
//~^ NOTE first borrow later used here
}
fn needs_static_mut() {
let mut x = vec![1];
//~^ NOTE binding `x` declared here
let a = display_len_mut(&mut x);
//~^ ERROR `x` does not live long enough
//~| NOTE this call may capture more lifetimes than intended
//~| NOTE argument requires that `x` is borrowed for `'static`
//~| NOTE borrowed value does not live long enough
fn needs_static(_: impl Sized + 'static) {}
needs_static(a);
}
//~^ NOTE `x` dropped here while still borrowed
fn is_move_mut() {
let mut x = vec![1];
//~^ NOTE binding `x` declared here
let a = display_len_mut(&mut x);
//~^ NOTE this call may capture more lifetimes than intended
//~| NOTE borrow of `x` occurs here
fn mv(_: impl Sized) {}
mv(x);
//~^ ERROR cannot move out of `x` because it is borrowed
//~| NOTE move out of `x` occurs here
}
//~^ NOTE borrow might be used here, when `a` is dropped
struct S { f: i32 }
fn display_field<T: Copy + Display>(t: &T) -> impl Display {
//~^ NOTE in this expansion of desugaring of `impl Trait`
//~| NOTE in this expansion of desugaring of `impl Trait`
//~| NOTE in this expansion of desugaring of `impl Trait`
*t
}
fn conflicting_borrow_field() {
let mut s = S { f: 0 };
let a = display_field(&s.f);
//~^ NOTE this call may capture more lifetimes than intended
//~| NOTE `s.f` is borrowed here
s.f = 1;
//~^ ERROR cannot assign to `s.f` because it is borrowed
//~| NOTE `s.f` is assigned to here but it was already borrowed
println!("{a}");
//~^ NOTE borrow later used here
}
fn display_field_mut<T: Copy + Display>(t: &mut T) -> impl Display {
*t
}
fn conflicting_borrow_field_mut() {
let mut s = S { f: 0 };
let a = display_field(&mut s.f);
//~^ NOTE this call may capture more lifetimes than intended
//~| NOTE `s.f` is borrowed here
s.f = 1;
//~^ ERROR cannot assign to `s.f` because it is borrowed
//~| NOTE `s.f` is assigned to here but it was already borrowed
println!("{a}");
//~^ NOTE borrow later used here
}
fn field_move() {
let mut s = S { f: 0 };
let a = display_field(&mut s.f);
//~^ NOTE this call may capture more lifetimes than intended
//~| NOTE `s.f` is borrowed here
s.f;
//~^ ERROR cannot use `s.f` because it was mutably borrowed
//~| NOTE use of borrowed `s.f`
println!("{a}");
//~^ NOTE borrow later used here
}
struct Z {
f: Vec<i32>,
}
fn live_long() {
let x;
{
let z = Z { f: vec![1] };
//~^ NOTE binding `z` declared here
x = display_len(&z.f);
//~^ ERROR `z.f` does not live long enough
//~| NOTE this call may capture more lifetimes than intended
//~| NOTE values in a scope are dropped in the opposite order they are defined
//~| NOTE borrowed value does not live long enough
}
//~^ NOTE `z.f` dropped here while still borrowed
}
//~^ NOTE borrow might be used here, when `x` is dropped
fn temp() {
let x = { let x = display_len(&mut vec![0]); x };
//~^ ERROR temporary value dropped while borrowed
//~| NOTE this call may capture more lifetimes than intended
//~| NOTE consider using a `let` binding to create a longer lived value
//~| NOTE borrow later used here
//~| NOTE temporary value is freed at the end of this statement
}
// FIXME: This doesn't display a useful Rust 2024 suggestion :(
fn returned() -> impl Sized {
let x = vec![0];
//~^ NOTE binding `x` declared here
display_len(&x)
//~^ ERROR `x` does not live long enough
//~| NOTE borrowed value does not live long enough
//~| NOTE argument requires that `x` is borrowed for `'static`
}
//~^ NOTE `x` dropped here while still borrowed
fn capture_apit(x: &impl Sized) -> impl Sized {}
//~^ NOTE you could use a `use<...>` bound to explicitly specify captures, but
fn test_apit() {
let x = String::new();
//~^ NOTE binding `x` declared here
let y = capture_apit(&x);
//~^ NOTE borrow of `x` occurs here
//~| NOTE this call may capture more lifetimes than intended
drop(x);
//~^ ERROR cannot move out of `x` because it is borrowed
//~| NOTE move out of `x` occurs here
}
//~^ NOTE borrow might be used here, when `y` is dropped
fn main() {}
|