]> git.lizzy.rs Git - rust.git/commitdiff
Make core::mem::needs_drop a const fn
authorMichael Bradshaw <mjbshaw@google.com>
Wed, 26 Sep 2018 14:55:57 +0000 (07:55 -0700)
committerMichael Bradshaw <mjbshaw@google.com>
Wed, 26 Sep 2018 18:04:56 +0000 (11:04 -0700)
src/libcore/mem.rs
src/librustc/ty/context.rs
src/librustc_mir/interpret/intrinsics.rs
src/librustc_mir/transform/qualify_consts.rs
src/librustc_typeck/check/intrinsic.rs
src/librustc_typeck/collect.rs
src/test/run-pass/const-needs_drop.rs [new file with mode: 0644]

index c99c9f96f12d67d712b0fef3a413440d57e67245..13c0b89f5df96fccbe866344a345525fe73a968e 100644 (file)
@@ -483,6 +483,15 @@ pub fn align_of_val<T: ?Sized>(val: &T) -> usize {
 /// ```
 #[inline]
 #[stable(feature = "needs_drop", since = "1.21.0")]
+#[cfg(not(stage0))]
+pub const fn needs_drop<T>() -> bool {
+    intrinsics::needs_drop::<T>()
+}
+
+#[inline]
+#[stable(feature = "needs_drop", since = "1.21.0")]
+#[cfg(stage0)]
+/// Ceci n'est pas la documentation
 pub fn needs_drop<T>() -> bool {
     unsafe { intrinsics::needs_drop::<T>() }
 }
index 37ef408c6bdee6dcf8631f25b2b7649b6d735e7c..ccf78eb93c57e9401f12275f9dbb0cb85110f5d4 100644 (file)
@@ -1143,6 +1143,7 @@ pub fn is_min_const_fn(self, def_id: DefId) -> bool {
                 match &self.item_name(def_id).as_str()[..] {
                     | "size_of"
                     | "min_align_of"
+                    | "needs_drop"
                     => return true,
                     _ => {},
                 }
index d2f274231c17049806e69b1ac1a26e4735fc510d..5fee49ba2fcf2ddb5bca8745a35b1bfc72510055 100644 (file)
@@ -65,6 +65,13 @@ pub fn emulate_intrinsic(
                 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;
index a997bd37c50a39a8468fc0f7afeea95154f1d51d..fed634f0f257cc72a1295a8f3570325fc4971b60 100644 (file)
@@ -823,6 +823,7 @@ fn visit_terminator_kind(&mut self,
                         match &self.tcx.item_name(def_id).as_str()[..] {
                             | "size_of"
                             | "min_align_of"
+                            | "needs_drop"
                             | "type_id"
                             | "bswap"
                             | "bitreverse"
index 8215ae211c0b9285d940ccc9dcda665cd217bab0..499d58bc2eec149a7a8e78c650194722fbf2291f 100644 (file)
@@ -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[..] {
index bb5753f432a026060ca59aaea5607a5a0acbc451..996468ee38263c1ef83a851f5a1e9bc8b2e55184 100644 (file)
@@ -2003,7 +2003,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 (file)
index 0000000..2e38159
--- /dev/null
@@ -0,0 +1,37 @@
+// 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::mem;
+
+struct Trivial(u8, f32);
+
+struct NonTrivial(u8, String);
+
+const CONST_U8: bool = mem::needs_drop::<u8>();
+const CONST_STRING: bool = mem::needs_drop::<String>();
+const CONST_TRIVIAL: bool = mem::needs_drop::<Trivial>();
+const CONST_NON_TRIVIAL: bool = mem::needs_drop::<NonTrivial>();
+
+static STATIC_U8: bool = mem::needs_drop::<u8>();
+static STATIC_STRING: bool = mem::needs_drop::<String>();
+static STATIC_TRIVIAL: bool = mem::needs_drop::<Trivial>();
+static STATIC_NON_TRIVIAL: bool = mem::needs_drop::<NonTrivial>();
+
+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);
+}