]> git.lizzy.rs Git - rust.git/blob - src/test/ui/span/vec_refs_data_with_early_death.rs
Auto merge of #50521 - gnzlbg:simd_float, r=alexcrichton
[rust.git] / src / test / ui / span / vec_refs_data_with_early_death.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // This test is a simple example of code that violates the dropck
12 // rules: it pushes `&x` and `&y` into a bag (with dtor), but the
13 // referenced data will be dropped before the bag is.
14
15
16
17
18
19
20
21 fn main() {
22     let mut v = Bag::new();
23
24     let x: i8 = 3;
25     let y: i8 = 4;
26
27     v.push(&x);
28     //~^ ERROR `x` does not live long enough
29     v.push(&y);
30     //~^ ERROR `y` does not live long enough
31
32     assert_eq!(v.0, [&3, &4]);
33 }
34
35 //`Vec<T>` is #[may_dangle] w.r.t. `T`; putting a bag over its head
36 // forces borrowck to treat dropping the bag as a potential use.
37 struct Bag<T>(Vec<T>);
38 impl<T> Drop for Bag<T> { fn drop(&mut self) { } }
39
40 impl<T> Bag<T> {
41     fn new() -> Self { Bag(Vec::new()) }
42     fn push(&mut self, t: T) { self.0.push(t); }
43 }