2015-10-08 12:12:26 +00:00
|
|
|
// Demonstrate that having a trait bound causes dropck to reject code
|
|
|
|
// that might indirectly access previously dropped value.
|
|
|
|
//
|
|
|
|
// Compare with run-pass/issue28498-ugeh-with-trait-bound.rs
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct ScribbleOnDrop(String);
|
|
|
|
|
|
|
|
impl Drop for ScribbleOnDrop {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.0 = format!("DROPPED");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 18:49:05 +00:00
|
|
|
struct Foo<T: fmt::Debug>(u32, T);
|
2015-10-08 12:12:26 +00:00
|
|
|
|
2019-07-10 18:49:05 +00:00
|
|
|
impl<T: fmt::Debug> Drop for Foo<T> {
|
2015-10-08 12:12:26 +00:00
|
|
|
fn drop(&mut self) {
|
2019-07-11 18:44:56 +00:00
|
|
|
// Use of `may_dangle` is unsound, because we access `T` fmt method when we pass
|
|
|
|
// `self.1` below, and thus potentially read from borrowed data.
|
2015-10-08 12:12:26 +00:00
|
|
|
println!("Dropping Foo({}, {:?})", self.0, self.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let (last_dropped, foo0);
|
|
|
|
let (foo1, first_dropped);
|
|
|
|
|
|
|
|
last_dropped = ScribbleOnDrop(format!("last"));
|
|
|
|
first_dropped = ScribbleOnDrop(format!("first"));
|
2019-04-22 07:40:08 +00:00
|
|
|
foo0 = Foo(0, &last_dropped); // OK
|
2015-10-08 12:12:26 +00:00
|
|
|
foo1 = Foo(1, &first_dropped);
|
2017-12-14 01:27:23 +00:00
|
|
|
//~^ ERROR `first_dropped` does not live long enough
|
2015-10-08 12:12:26 +00:00
|
|
|
|
|
|
|
println!("foo0.1: {:?} foo1.1: {:?}", foo0.1, foo1.1);
|
|
|
|
}
|