]> git.lizzy.rs Git - rust.git/blob - tests/ui/dropck/issue-28498-ugeh-with-trait-bound.rs
Merge commit '7f27e2e74ef957baa382dc05cf08df6368165c74' into clippyup
[rust.git] / tests / ui / dropck / issue-28498-ugeh-with-trait-bound.rs
1 // run-pass
2
3 // Demonstrate the use of the unguarded escape hatch with a trait bound
4 // to assert that destructor will not access any dead data.
5 //
6 // Compare with ui/span/issue28498-reject-trait-bound.rs
7
8 #![feature(dropck_eyepatch)]
9
10 use std::fmt;
11
12 #[derive(Debug)]
13 struct ScribbleOnDrop(String);
14
15 impl Drop for ScribbleOnDrop {
16     fn drop(&mut self) {
17         self.0 = format!("DROPPED");
18     }
19 }
20
21 struct Foo<T: fmt::Debug>(u32, T);
22
23 unsafe impl<#[may_dangle] T: fmt::Debug> Drop for Foo<T> {
24     fn drop(&mut self) {
25         // Use of `may_dangle` is sound, because destructor never accesses
26         // the `Debug::fmt` method of `T`, despite having it available.
27         println!("Dropping Foo({}, _)", self.0);
28     }
29 }
30
31 fn main() {
32     let (last_dropped, foo0);
33     let (foo1, first_dropped);
34
35     last_dropped = ScribbleOnDrop(format!("last"));
36     first_dropped = ScribbleOnDrop(format!("first"));
37     foo0 = Foo(0, &last_dropped);
38     foo1 = Foo(1, &first_dropped);
39
40     println!("foo0.1: {:?} foo1.1: {:?}", foo0.1, foo1.1);
41 }