2 #![feature(dropck_eyepatch)]
4 // The point of this test is to test uses of `#[may_dangle]` attribute
5 // where the formal declaration order (in the impl generics) does not
6 // match the actual usage order (in the type instantiation).
8 // See also dropck-eyepatch.rs for more information about the general
9 // structure of the test.
11 trait Foo { fn foo(&self, _: &str); }
13 struct Dt<A: Foo>(&'static str, A);
14 struct Dr<'a, B:'a+Foo>(&'static str, &'a B);
15 struct Pt<A: Foo, B: Foo>(&'static str, A, B);
16 struct Pr<'a, 'b, B:'a+'b+Foo>(&'static str, &'a B, &'b B);
17 struct St<A: Foo>(&'static str, A);
18 struct Sr<'a, B:'a+Foo>(&'static str, &'a B);
20 impl<A: Foo> Drop for Dt<A> {
21 fn drop(&mut self) { println!("drop {}", self.0); self.1.foo(self.0); }
23 impl<'a, B: Foo> Drop for Dr<'a, B> {
24 fn drop(&mut self) { println!("drop {}", self.0); self.1.foo(self.0); }
26 unsafe impl<B: Foo, #[may_dangle] A: Foo> Drop for Pt<A, B> {
27 // (unsafe to access self.1 due to #[may_dangle] on A)
28 fn drop(&mut self) { println!("drop {}", self.0); self.2.foo(self.0); }
30 unsafe impl<'b, #[may_dangle] 'a, B: Foo> Drop for Pr<'a, 'b, B> {
31 // (unsafe to access self.1 due to #[may_dangle] on 'a)
32 fn drop(&mut self) { println!("drop {}", self.0); self.2.foo(self.0); }
36 use std::cell::RefCell;
38 impl Foo for RefCell<String> {
39 fn foo(&self, s: &str) {
40 let s2 = format!("{}|{}", *self.borrow(), s);
41 *self.borrow_mut() = s2;
45 impl<'a, T:Foo> Foo for &'a T {
46 fn foo(&self, s: &str) {
51 struct CheckOnDrop(RefCell<String>, &'static str);
52 impl Drop for CheckOnDrop {
53 fn drop(&mut self) { assert_eq!(*self.0.borrow(), self.1); }
57 let (c, dt, dr, pt, pr, st, sr)
58 : (CheckOnDrop, Dt<_>, Dr<_>, Pt<_, _>, Pr<_>, St<_>, Sr<_>);
59 c_long = CheckOnDrop(RefCell::new("c_long".to_string()),
60 "c_long|pr|pt|dr|dt");
61 c = CheckOnDrop(RefCell::new("c".to_string()),
64 // No error: sufficiently long-lived state can be referenced in dtors
65 dt = Dt("dt", &c_long.0);
66 dr = Dr("dr", &c_long.0);
68 // No error: Drop impl asserts .1 (A and &'a _) are not accessed
69 pt = Pt("pt", &c.0, &c_long.0);
70 pr = Pr("pr", &c.0, &c_long.0);
72 // No error: St and Sr have no destructor.
76 println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0));
77 assert_eq!(*c_long.0.borrow(), "c_long");
78 assert_eq!(*c.0.borrow(), "c");