]> git.lizzy.rs Git - rust.git/blob - tests/ui/span/vec_refs_data_with_early_death.rs
Optimize `TyKind::eq`.
[rust.git] / tests / ui / span / vec_refs_data_with_early_death.rs
1 // This test is a simple example of code that violates the dropck
2 // rules: it pushes `&x` and `&y` into a bag (with dtor), but the
3 // referenced data will be dropped before the bag is.
4
5
6
7
8
9
10
11 fn main() {
12     let mut v = Bag::new();
13
14     let x: i8 = 3;
15     let y: i8 = 4;
16
17     v.push(&x);
18     //~^ ERROR `x` does not live long enough
19     v.push(&y);
20     //~^ ERROR `y` does not live long enough
21
22     assert_eq!(v.0, [&3, &4]);
23 }
24
25 //`Vec<T>` is #[may_dangle] w.r.t. `T`; putting a bag over its head
26 // forces borrowck to treat dropping the bag as a potential use.
27 struct Bag<T>(Vec<T>);
28 impl<T> Drop for Bag<T> { fn drop(&mut self) { } }
29
30 impl<T> Bag<T> {
31     fn new() -> Self { Bag(Vec::new()) }
32     fn push(&mut self, t: T) { self.0.push(t); }
33 }