Skip to content

Commit c076954

Browse files
committed
Make core::mem::needs_drop a const fn
1 parent e783d2b commit c076954

File tree

7 files changed

+57
-2
lines changed

7 files changed

+57
-2
lines changed

src/libcore/mem.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,15 @@ pub fn align_of_val<T: ?Sized>(val: &T) -> usize {
483483
/// ```
484484
#[inline]
485485
#[stable(feature = "needs_drop", since = "1.21.0")]
486+
#[cfg(not(stage0))]
487+
pub const fn needs_drop<T>() -> bool {
488+
intrinsics::needs_drop::<T>()
489+
}
490+
491+
#[inline]
492+
#[stable(feature = "needs_drop", since = "1.21.0")]
493+
#[cfg(stage0)]
494+
/// Ceci n'est pas la documentation
486495
pub fn needs_drop<T>() -> bool {
487496
unsafe { intrinsics::needs_drop::<T>() }
488497
}

src/librustc/ty/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11431143
match &self.item_name(def_id).as_str()[..] {
11441144
| "size_of"
11451145
| "min_align_of"
1146+
| "needs_drop"
11461147
=> return true,
11471148
_ => {},
11481149
}

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
6565
self.write_scalar(align_val, dest)?;
6666
}
6767

68+
"needs_drop" => {
69+
let ty = substs.type_at(0);
70+
let ty_needs_drop = ty.needs_drop(self.tcx.tcx, self.param_env);
71+
let val = Scalar::from_bool(ty_needs_drop);
72+
self.write_scalar(val, dest)?;
73+
}
74+
6875
"size_of" => {
6976
let ty = substs.type_at(0);
7077
let size = self.layout_of(ty)?.size.bytes() as u128;

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
823823
match &self.tcx.item_name(def_id).as_str()[..] {
824824
| "size_of"
825825
| "min_align_of"
826+
| "needs_drop"
826827
| "type_id"
827828
| "bswap"
828829
| "bitreverse"

src/librustc_typeck/check/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
117117
(0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
118118
} else {
119119
let unsafety = match &name[..] {
120-
"size_of" | "min_align_of" => hir::Unsafety::Normal,
120+
"size_of" | "min_align_of" | "needs_drop" => hir::Unsafety::Normal,
121121
_ => hir::Unsafety::Unsafe,
122122
};
123123
let (n_tps, inputs, output) = match &name[..] {

src/librustc_typeck/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,7 @@ fn compute_sig_of_foreign_fn_decl<'a, 'tcx>(
20032003
) -> ty::PolyFnSig<'tcx> {
20042004
let unsafety = if abi == abi::Abi::RustIntrinsic {
20052005
match &*tcx.item_name(def_id).as_str() {
2006-
"size_of" | "min_align_of" => hir::Unsafety::Normal,
2006+
"size_of" | "min_align_of" | "needs_drop" => hir::Unsafety::Normal,
20072007
_ => hir::Unsafety::Unsafe,
20082008
}
20092009
} else {

src/test/run-pass/const-needs_drop.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::mem;
12+
13+
struct Trivial(u8, f32);
14+
15+
struct NonTrivial(u8, String);
16+
17+
const CONST_U8: bool = mem::needs_drop::<u8>();
18+
const CONST_STRING: bool = mem::needs_drop::<String>();
19+
const CONST_TRIVIAL: bool = mem::needs_drop::<Trivial>();
20+
const CONST_NON_TRIVIAL: bool = mem::needs_drop::<NonTrivial>();
21+
22+
static STATIC_U8: bool = mem::needs_drop::<u8>();
23+
static STATIC_STRING: bool = mem::needs_drop::<String>();
24+
static STATIC_TRIVIAL: bool = mem::needs_drop::<Trivial>();
25+
static STATIC_NON_TRIVIAL: bool = mem::needs_drop::<NonTrivial>();
26+
27+
fn main() {
28+
assert!(!CONST_U8);
29+
assert!(CONST_STRING);
30+
assert!(!CONST_TRIVIAL);
31+
assert!(CONST_NON_TRIVIAL);
32+
33+
assert!(!STATIC_U8);
34+
assert!(STATIC_STRING);
35+
assert!(!STATIC_TRIVIAL);
36+
assert!(STATIC_NON_TRIVIAL);
37+
}

0 commit comments

Comments
 (0)