rust/tests/ui/rfcs/rfc-2632-const-trait-impl/const-drop.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

117 lines
2.3 KiB
Rust
Raw Normal View History

2023-07-27 10:51:02 -05:00
// FIXME run-pass
// known-bug: #110395
2021-09-03 04:53:57 -05:00
// revisions: stock precise
2021-09-01 06:55:16 -05:00
#![feature(const_trait_impl)]
#![feature(const_mut_refs)]
#![feature(never_type)]
2023-07-27 10:51:02 -05:00
// #![cfg_attr(precise, feature(const_precise_live_drops))]
2021-09-01 06:55:16 -05:00
use std::marker::Destruct;
2021-09-01 08:07:49 -05:00
struct S<'a>(&'a mut u8);
2021-09-01 06:55:16 -05:00
2021-09-01 08:07:49 -05:00
impl<'a> const Drop for S<'a> {
2021-09-01 06:55:16 -05:00
fn drop(&mut self) {
2021-09-01 08:07:49 -05:00
*self.0 += 1;
2021-09-01 06:55:16 -05:00
}
}
const fn a<T: ~const Destruct>(_: T) {}
2023-07-27 10:51:02 -05:00
//FIXME ~^ ERROR destructor of
2021-09-01 06:55:16 -05:00
2021-09-01 08:07:49 -05:00
const fn b() -> u8 {
let mut c = 0;
let _ = S(&mut c);
2023-07-27 10:51:02 -05:00
//FIXME ~^ ERROR destructor of
2021-09-01 08:07:49 -05:00
a(S(&mut c));
c
}
const C: u8 = b();
2021-09-01 06:55:16 -05:00
2021-09-01 11:34:28 -05:00
macro_rules! implements_const_drop {
($($exp:expr),*$(,)?) => {
$(
const _: () = a($exp);
)*
}
}
#[allow(dead_code)]
mod t {
pub struct Foo;
pub enum Bar { A }
pub fn foo() {}
pub struct ConstDrop;
impl const Drop for ConstDrop {
fn drop(&mut self) {}
}
pub struct HasConstDrop(pub ConstDrop);
pub struct TrivialFields(pub u8, pub i8, pub usize, pub isize);
2022-01-18 03:42:35 -06:00
2022-08-28 01:27:31 -05:00
#[const_trait]
2022-01-18 03:42:35 -06:00
pub trait SomeTrait {
fn foo();
}
impl const SomeTrait for () {
fn foo() {}
}
// non-const impl
impl SomeTrait for i32 {
fn foo() {}
}
2022-01-18 03:42:35 -06:00
pub struct ConstDropWithBound<T: ~const SomeTrait>(pub core::marker::PhantomData<T>);
2022-01-18 03:42:35 -06:00
impl<T: ~const SomeTrait> const Drop for ConstDropWithBound<T> {
fn drop(&mut self) {
T::foo();
}
}
pub struct ConstDropWithNonconstBound<T: SomeTrait>(pub core::marker::PhantomData<T>);
impl<T: SomeTrait> const Drop for ConstDropWithNonconstBound<T> {
fn drop(&mut self) {
// Note: we DON'T use the `T: SomeTrait` bound
}
}
2021-09-01 11:34:28 -05:00
}
use t::*;
implements_const_drop! {
1u8,
2,
3.0,
Foo,
Bar::A,
foo,
ConstDrop,
HasConstDrop(ConstDrop),
TrivialFields(1, 2, 3, 4),
&1,
&1 as *const i32,
2022-01-18 03:42:35 -06:00
ConstDropWithBound::<()>,
ConstDropWithNonconstBound::<i32>,
Result::<i32, !>::Ok(1),
2021-09-01 11:34:28 -05:00
}
2021-09-01 08:07:49 -05:00
fn main() {
2022-07-25 15:36:03 -05:00
struct HasDropGlue(#[allow(unused_tuple_struct_fields)] Box<u8>);
2021-09-01 11:34:28 -05:00
struct HasDropImpl;
impl Drop for HasDropImpl {
fn drop(&mut self) {
println!("not trivial drop");
}
}
// These types should pass because ~const in a non-const context should have no effect.
a(HasDropGlue(Box::new(0)));
a(HasDropImpl);
2021-09-01 08:07:49 -05:00
assert_eq!(C, 2);
}