Skip to content

Commit 46c00a2

Browse files
committed
Move all intrinsic whitelists into the constness check file
1 parent 52be0b0 commit 46c00a2

File tree

4 files changed

+42
-56
lines changed

4 files changed

+42
-56
lines changed

src/librustc/ty/constness.rs

+35
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,47 @@ impl<'tcx> TyCtxt<'tcx> {
3636
}
3737
}
3838

39+
/// Returns `true` if the `def_id` refers to an intrisic which we've whitelisted
40+
/// for being called from stable `const fn`s (`min_const_fn`).
41+
///
42+
/// Adding more intrinsics requires sign-off from @rust-lang/lang.
43+
fn is_intrinsic_min_const_fn(self, def_id: DefId) -> bool {
44+
match self.item_name(def_id) {
45+
| sym::size_of
46+
| sym::min_align_of
47+
| sym::needs_drop
48+
// Arithmetic:
49+
| sym::add_with_overflow // ~> .overflowing_add
50+
| sym::sub_with_overflow // ~> .overflowing_sub
51+
| sym::mul_with_overflow // ~> .overflowing_mul
52+
| sym::wrapping_add // ~> .wrapping_add
53+
| sym::wrapping_sub // ~> .wrapping_sub
54+
| sym::wrapping_mul // ~> .wrapping_mul
55+
| sym::saturating_add // ~> .saturating_add
56+
| sym::saturating_sub // ~> .saturating_sub
57+
| sym::unchecked_shl // ~> .wrapping_shl
58+
| sym::unchecked_shr // ~> .wrapping_shr
59+
| sym::rotate_left // ~> .rotate_left
60+
| sym::rotate_right // ~> .rotate_right
61+
| sym::ctpop // ~> .count_ones
62+
| sym::ctlz // ~> .leading_zeros
63+
| sym::cttz // ~> .trailing_zeros
64+
| sym::bswap // ~> .swap_bytes
65+
| sym::bitreverse // ~> .reverse_bits
66+
=> true,
67+
_ => false,
68+
}
69+
}
70+
3971
/// Returns `true` if this function must conform to `min_const_fn`
4072
pub fn is_min_const_fn(self, def_id: DefId) -> bool {
4173
// Bail out if the signature doesn't contain `const`
4274
if !self.is_const_fn_raw(def_id) {
4375
return false;
4476
}
77+
if let Abi::RustIntrinsic = self.fn_sig(def_id).abi() {
78+
return self.is_intrinsic_min_const_fn(def_id);
79+
}
4580

4681
if self.features().staged_api {
4782
// in order for a libstd function to be considered min_const_fn

src/librustc_mir/transform/qualify_min_const_fn.rs

+3-52
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use rustc::hir::def_id::DefId;
22
use rustc::hir;
33
use rustc::mir::*;
44
use rustc::ty::{self, Predicate, Ty, TyCtxt, adjustment::{PointerCast}};
5-
use rustc_target::spec::abi;
65
use std::borrow::Cow;
76
use syntax_pos::Span;
87
use syntax::symbol::{sym, Symbol};
@@ -356,33 +355,16 @@ fn check_terminator(
356355
} => {
357356
let fn_ty = func.ty(body, tcx);
358357
if let ty::FnDef(def_id, _) = fn_ty.kind {
359-
360-
// some intrinsics are waved through if called inside the
361-
// standard library. Users never need to call them directly
362-
match tcx.fn_sig(def_id).abi() {
363-
abi::Abi::RustIntrinsic => if !is_intrinsic_whitelisted(tcx, def_id) {
364-
return Err((
365-
span,
366-
"can only call a curated list of intrinsics in `min_const_fn`".into(),
367-
))
368-
},
369-
abi::Abi::Rust if tcx.is_min_const_fn(def_id) => {},
370-
abi::Abi::Rust => return Err((
358+
if !tcx.is_min_const_fn(def_id) {
359+
return Err((
371360
span,
372361
format!(
373362
"can only call other `const fn` within a `const fn`, \
374363
but `{:?}` is not stable as `const fn`",
375364
func,
376365
)
377366
.into(),
378-
)),
379-
abi => return Err((
380-
span,
381-
format!(
382-
"cannot call functions with `{}` abi in `min_const_fn`",
383-
abi,
384-
).into(),
385-
)),
367+
));
386368
}
387369

388370
check_operand(tcx, func, span, def_id, body)?;
@@ -410,34 +392,3 @@ fn check_terminator(
410392
}
411393
}
412394

413-
/// Returns `true` if the `def_id` refers to an intrisic which we've whitelisted
414-
/// for being called from stable `const fn`s (`min_const_fn`).
415-
///
416-
/// Adding more intrinsics requires sign-off from @rust-lang/lang.
417-
fn is_intrinsic_whitelisted(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
418-
match tcx.item_name(def_id) {
419-
| sym::size_of
420-
| sym::min_align_of
421-
| sym::needs_drop
422-
// Arithmetic:
423-
| sym::add_with_overflow // ~> .overflowing_add
424-
| sym::sub_with_overflow // ~> .overflowing_sub
425-
| sym::mul_with_overflow // ~> .overflowing_mul
426-
| sym::wrapping_add // ~> .wrapping_add
427-
| sym::wrapping_sub // ~> .wrapping_sub
428-
| sym::wrapping_mul // ~> .wrapping_mul
429-
| sym::saturating_add // ~> .saturating_add
430-
| sym::saturating_sub // ~> .saturating_sub
431-
| sym::unchecked_shl // ~> .wrapping_shl
432-
| sym::unchecked_shr // ~> .wrapping_shr
433-
| sym::rotate_left // ~> .rotate_left
434-
| sym::rotate_right // ~> .rotate_right
435-
| sym::ctpop // ~> .count_ones
436-
| sym::ctlz // ~> .leading_zeros
437-
| sym::cttz // ~> .trailing_zeros
438-
| sym::bswap // ~> .swap_bytes
439-
| sym::bitreverse // ~> .reverse_bits
440-
=> true,
441-
_ => false,
442-
}
443-
}

src/test/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern "C" {
77
const extern fn bar() {
88
unsafe {
99
regular_in_block();
10-
//~^ ERROR: cannot call functions with `"C"` abi in `min_const_fn`
10+
//~^ ERROR: can only call other `const fn` within a `const fn`
1111
}
1212
}
1313

@@ -16,7 +16,7 @@ extern fn regular() {}
1616
const extern fn foo() {
1717
unsafe {
1818
regular();
19-
//~^ ERROR: cannot call functions with `"C"` abi in `min_const_fn`
19+
//~^ ERROR: can only call other `const fn` within a `const fn`
2020
}
2121
}
2222

src/test/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0723]: cannot call functions with `"C"` abi in `min_const_fn`
1+
error[E0723]: can only call other `const fn` within a `const fn`, but `const regular_in_block` is not stable as `const fn`
22
--> $DIR/const-extern-fn-call-extern-fn.rs:9:9
33
|
44
LL | regular_in_block();
@@ -7,7 +7,7 @@ LL | regular_in_block();
77
= note: for more information, see issue https://github.com/rust-lang/rust/issues/57563
88
= help: add `#![feature(const_fn)]` to the crate attributes to enable
99

10-
error[E0723]: cannot call functions with `"C"` abi in `min_const_fn`
10+
error[E0723]: can only call other `const fn` within a `const fn`, but `const regular` is not stable as `const fn`
1111
--> $DIR/const-extern-fn-call-extern-fn.rs:18:9
1212
|
1313
LL | regular();

0 commit comments

Comments
 (0)