]> git.proxmox.com Git - rustc.git/blob - src/test/ui/span/issue28498-reject-trait-bound.rs
New upstream version 1.38.0+dfsg1
[rustc.git] / src / test / ui / span / issue28498-reject-trait-bound.rs
1 // Demonstrate that having a trait bound causes dropck to reject code
2 // that might indirectly access previously dropped value.
3 //
4 // Compare with run-pass/issue28498-ugeh-with-trait-bound.rs
5
6 use std::fmt;
7
8 #[derive(Debug)]
9 struct ScribbleOnDrop(String);
10
11 impl Drop for ScribbleOnDrop {
12 fn drop(&mut self) {
13 self.0 = format!("DROPPED");
14 }
15 }
16
17 struct Foo<T: fmt::Debug>(u32, T);
18
19 impl<T: fmt::Debug> Drop for Foo<T> {
20 fn drop(&mut self) {
21 // Use of `may_dangle` is unsound, because we access `T` fmt method when we pass
22 // `self.1` below, and thus potentially read from borrowed data.
23 println!("Dropping Foo({}, {:?})", self.0, self.1);
24 }
25 }
26
27 fn main() {
28 let (last_dropped, foo0);
29 let (foo1, first_dropped);
30
31 last_dropped = ScribbleOnDrop(format!("last"));
32 first_dropped = ScribbleOnDrop(format!("first"));
33 foo0 = Foo(0, &last_dropped); // OK
34 foo1 = Foo(1, &first_dropped);
35 //~^ ERROR `first_dropped` does not live long enough
36
37 println!("foo0.1: {:?} foo1.1: {:?}", foo0.1, foo1.1);
38 }