diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 9871f4966786d..f537add999a03 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -455,6 +455,16 @@ pub fn align_of_val(val: &T) -> usize { /// ``` #[inline] #[stable(feature = "needs_drop", since = "1.21.0")] +#[rustc_const_unstable(feature = "const_needs_drop")] +#[cfg(not(stage0))] +pub const fn needs_drop() -> bool { + intrinsics::needs_drop::() +} + +#[inline] +#[stable(feature = "needs_drop", since = "1.21.0")] +#[cfg(stage0)] +/// Ceci n'est pas la documentation pub fn needs_drop() -> bool { unsafe { intrinsics::needs_drop::() } } diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index c288285f2bb3f..63408d809ec29 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -1144,6 +1144,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { match &self.item_name(def_id).as_str()[..] { | "size_of" | "min_align_of" + | "needs_drop" => return true, _ => {}, } diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index d2f274231c170..5fee49ba2fcf2 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -65,6 +65,13 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> self.write_scalar(align_val, dest)?; } + "needs_drop" => { + let ty = substs.type_at(0); + let ty_needs_drop = ty.needs_drop(self.tcx.tcx, self.param_env); + let val = Scalar::from_bool(ty_needs_drop); + self.write_scalar(val, dest)?; + } + "size_of" => { let ty = substs.type_at(0); let size = self.layout_of(ty)?.size.bytes() as u128; diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index cf45b541f80d3..a6e2cad509408 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -823,6 +823,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { match &self.tcx.item_name(def_id).as_str()[..] { | "size_of" | "min_align_of" + | "needs_drop" | "type_id" | "bswap" | "bitreverse" diff --git a/src/librustc_typeck/check/intrinsic.rs b/src/librustc_typeck/check/intrinsic.rs index 24db9505cb807..d630c5b3040b7 100644 --- a/src/librustc_typeck/check/intrinsic.rs +++ b/src/librustc_typeck/check/intrinsic.rs @@ -117,7 +117,7 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, (0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe) } else { let unsafety = match &name[..] { - "size_of" | "min_align_of" => hir::Unsafety::Normal, + "size_of" | "min_align_of" | "needs_drop" => hir::Unsafety::Normal, _ => hir::Unsafety::Unsafe, }; let (n_tps, inputs, output) = match &name[..] { diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index bffdf7772401e..17e0b0431da6e 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -2015,7 +2015,7 @@ fn compute_sig_of_foreign_fn_decl<'a, 'tcx>( ) -> ty::PolyFnSig<'tcx> { let unsafety = if abi == abi::Abi::RustIntrinsic { match &*tcx.item_name(def_id).as_str() { - "size_of" | "min_align_of" => hir::Unsafety::Normal, + "size_of" | "min_align_of" | "needs_drop" => hir::Unsafety::Normal, _ => hir::Unsafety::Unsafe, } } else { diff --git a/src/test/run-pass/const-needs_drop.rs b/src/test/run-pass/const-needs_drop.rs new file mode 100644 index 0000000000000..edf3a6b3dcd94 --- /dev/null +++ b/src/test/run-pass/const-needs_drop.rs @@ -0,0 +1,39 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(const_needs_drop)] + +use std::mem; + +struct Trivial(u8, f32); + +struct NonTrivial(u8, String); + +const CONST_U8: bool = mem::needs_drop::(); +const CONST_STRING: bool = mem::needs_drop::(); +const CONST_TRIVIAL: bool = mem::needs_drop::(); +const CONST_NON_TRIVIAL: bool = mem::needs_drop::(); + +static STATIC_U8: bool = mem::needs_drop::(); +static STATIC_STRING: bool = mem::needs_drop::(); +static STATIC_TRIVIAL: bool = mem::needs_drop::(); +static STATIC_NON_TRIVIAL: bool = mem::needs_drop::(); + +fn main() { + assert!(!CONST_U8); + assert!(CONST_STRING); + assert!(!CONST_TRIVIAL); + assert!(CONST_NON_TRIVIAL); + + assert!(!STATIC_U8); + assert!(STATIC_STRING); + assert!(!STATIC_TRIVIAL); + assert!(STATIC_NON_TRIVIAL); +} diff --git a/src/test/run-pass/union/union-nodrop.rs b/src/test/run-pass/union/union-nodrop.rs index 4f2456e43bad1..d024aca0cd701 100644 --- a/src/test/run-pass/union/union-nodrop.rs +++ b/src/test/run-pass/union/union-nodrop.rs @@ -57,15 +57,13 @@ impl Drop for ActuallyDrop { } fn main() { - unsafe { - // NoDrop should not make needs_drop true - assert!(!needs_drop::()); - assert!(!needs_drop::>()); - assert!(!needs_drop::>>()); - // presence of other drop types should still work - assert!(needs_drop::()); - // drop impl on union itself should work - assert!(needs_drop::>()); - assert!(needs_drop::>>()); - } + // NoDrop should not make needs_drop true + assert!(!needs_drop::()); + assert!(!needs_drop::>()); + assert!(!needs_drop::>>()); + // presence of other drop types should still work + assert!(needs_drop::()); + // drop impl on union itself should work + assert!(needs_drop::>()); + assert!(needs_drop::>>()); }