]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc-2632-const-trait-impl/const-drop.rs
Remove unnecessary .as_ref().
[rust.git] / src / test / ui / rfc-2632-const-trait-impl / const-drop.rs
1 // run-pass
2 // revisions: stock precise
3 #![feature(const_trait_impl)]
4 #![feature(const_mut_refs)]
5 #![feature(never_type)]
6 #![cfg_attr(precise, feature(const_precise_live_drops))]
7
8 struct S<'a>(&'a mut u8);
9
10 impl<'a> const Drop for S<'a> {
11     fn drop(&mut self) {
12         *self.0 += 1;
13     }
14 }
15
16 const fn a<T: ~const Drop>(_: T) {}
17
18 const fn b() -> u8 {
19     let mut c = 0;
20     let _ = S(&mut c);
21     a(S(&mut c));
22     c
23 }
24
25 const C: u8 = b();
26
27 macro_rules! implements_const_drop {
28     ($($exp:expr),*$(,)?) => {
29         $(
30             const _: () = a($exp);
31         )*
32     }
33 }
34
35 #[allow(dead_code)]
36 mod t {
37     pub struct Foo;
38     pub enum Bar { A }
39     pub fn foo() {}
40     pub struct ConstDrop;
41
42     impl const Drop for ConstDrop {
43         fn drop(&mut self) {}
44     }
45
46     pub struct HasConstDrop(pub ConstDrop);
47     pub struct TrivialFields(pub u8, pub i8, pub usize, pub isize);
48
49     pub trait SomeTrait {
50         fn foo();
51     }
52     impl const SomeTrait for () {
53         fn foo() {}
54     }
55     // non-const impl
56     impl SomeTrait for i32 {
57         fn foo() {}
58     }
59
60     pub struct ConstDropWithBound<T: SomeTrait>(pub core::marker::PhantomData<T>);
61
62     impl<T: ~const SomeTrait> const Drop for ConstDropWithBound<T> {
63         fn drop(&mut self) {
64             T::foo();
65         }
66     }
67
68     pub struct ConstDropWithNonconstBound<T: SomeTrait>(pub core::marker::PhantomData<T>);
69
70     impl<T: SomeTrait> const Drop for ConstDropWithNonconstBound<T> {
71         fn drop(&mut self) {
72             // Note: we DON'T use the `T: SomeTrait` bound
73         }
74     }
75 }
76
77 use t::*;
78
79 implements_const_drop! {
80     1u8,
81     2,
82     3.0,
83     Foo,
84     Bar::A,
85     foo,
86     ConstDrop,
87     HasConstDrop(ConstDrop),
88     TrivialFields(1, 2, 3, 4),
89     &1,
90     &1 as *const i32,
91     ConstDropWithBound::<()>,
92     ConstDropWithNonconstBound::<i32>,
93     Result::<i32, !>::Ok(1),
94 }
95
96 fn main() {
97     struct HasDropGlue(Box<u8>);
98     struct HasDropImpl;
99     impl Drop for HasDropImpl {
100         fn drop(&mut self) {
101             println!("not trivial drop");
102         }
103     }
104
105     // These types should pass because ~const in a non-const context should have no effect.
106     a(HasDropGlue(Box::new(0)));
107     a(HasDropImpl);
108
109     assert_eq!(C, 2);
110 }