-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Organize intrinsics promotion checks #66275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f1bacb2
52be0b0
46c00a2
cc5b357
1f20b4d
d9cca74
e2bbf06
5cef094
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ use crate::ty::query::Providers; | |
use crate::hir::def_id::DefId; | ||
use crate::hir; | ||
use crate::ty::TyCtxt; | ||
use syntax_pos::symbol::Symbol; | ||
use syntax_pos::symbol::{sym, Symbol}; | ||
use rustc_target::spec::abi::Abi; | ||
use crate::hir::map::blocks::FnLikeNode; | ||
use syntax::attr; | ||
|
||
|
@@ -35,12 +36,51 @@ impl<'tcx> TyCtxt<'tcx> { | |
} | ||
} | ||
|
||
/// Returns `true` if the `def_id` refers to an intrisic which we've whitelisted | ||
/// for being called from stable `const fn`s (`min_const_fn`). | ||
/// | ||
/// Adding more intrinsics requires sign-off from @rust-lang/lang. | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// This list differs from the list in `is_const_intrinsic` in the sense that any item on this | ||
/// list must be on the `is_const_intrinsic` list, too, because if an intrinsic is callable from | ||
/// stable, it must be callable at all. | ||
fn is_intrinsic_min_const_fn(self, def_id: DefId) -> bool { | ||
match self.item_name(def_id) { | ||
| sym::size_of | ||
| sym::min_align_of | ||
| sym::needs_drop | ||
// Arithmetic: | ||
| sym::add_with_overflow // ~> .overflowing_add | ||
| sym::sub_with_overflow // ~> .overflowing_sub | ||
| sym::mul_with_overflow // ~> .overflowing_mul | ||
| sym::wrapping_add // ~> .wrapping_add | ||
| sym::wrapping_sub // ~> .wrapping_sub | ||
| sym::wrapping_mul // ~> .wrapping_mul | ||
| sym::saturating_add // ~> .saturating_add | ||
| sym::saturating_sub // ~> .saturating_sub | ||
| sym::unchecked_shl // ~> .wrapping_shl | ||
| sym::unchecked_shr // ~> .wrapping_shr | ||
| sym::rotate_left // ~> .rotate_left | ||
| sym::rotate_right // ~> .rotate_right | ||
| sym::ctpop // ~> .count_ones | ||
| sym::ctlz // ~> .leading_zeros | ||
| sym::cttz // ~> .trailing_zeros | ||
| sym::bswap // ~> .swap_bytes | ||
| sym::bitreverse // ~> .reverse_bits | ||
=> true, | ||
_ => false, | ||
} | ||
} | ||
|
||
/// Returns `true` if this function must conform to `min_const_fn` | ||
pub fn is_min_const_fn(self, def_id: DefId) -> bool { | ||
// Bail out if the signature doesn't contain `const` | ||
if !self.is_const_fn_raw(def_id) { | ||
return false; | ||
} | ||
if let Abi::RustIntrinsic = self.fn_sig(def_id).abi() { | ||
return self.is_intrinsic_min_const_fn(def_id); | ||
} | ||
|
||
if self.features().staged_api { | ||
// in order for a libstd function to be considered min_const_fn | ||
|
@@ -63,13 +103,82 @@ impl<'tcx> TyCtxt<'tcx> { | |
|
||
|
||
pub fn provide(providers: &mut Providers<'_>) { | ||
/// only checks whether the function has a `const` modifier | ||
/// Const evaluability whitelist is here to check evaluability at the | ||
/// top level beforehand. | ||
fn is_const_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> Option<bool> { | ||
match tcx.fn_sig(def_id).abi() { | ||
Abi::RustIntrinsic | | ||
Abi::PlatformIntrinsic => { | ||
// FIXME: deduplicate these two lists as much as possible | ||
match tcx.item_name(def_id) { | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we de-dup this list with the min-const-fn whitelist by having this one first consult the min-const-fn list? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @oli-obk said he'd do some clean-up in a follow-up PR. For now we just want to get our const check in order. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we leave a FIXME tho? |
||
// Keep this list in the same order as the match patterns in | ||
// `librustc_mir/interpret/intrinsics.rs` | ||
|
||
// This whitelist is a list of intrinsics that have a miri-engine implementation | ||
// and can thus be called when enabling enough feature gates. The similar | ||
// whitelist in `is_intrinsic_min_const_fn` (in this file), exists for allowing | ||
// the intrinsics to be called by stable const fns. | ||
| sym::caller_location | ||
|
||
| sym::min_align_of | ||
| sym::pref_align_of | ||
| sym::needs_drop | ||
| sym::size_of | ||
| sym::type_id | ||
| sym::type_name | ||
|
||
| sym::ctpop | ||
| sym::cttz | ||
| sym::cttz_nonzero | ||
| sym::ctlz | ||
| sym::ctlz_nonzero | ||
| sym::bswap | ||
| sym::bitreverse | ||
|
||
| sym::wrapping_add | ||
| sym::wrapping_sub | ||
| sym::wrapping_mul | ||
| sym::add_with_overflow | ||
| sym::sub_with_overflow | ||
| sym::mul_with_overflow | ||
|
||
| sym::saturating_add | ||
| sym::saturating_sub | ||
|
||
| sym::unchecked_shl | ||
| sym::unchecked_shr | ||
|
||
| sym::rotate_left | ||
| sym::rotate_right | ||
|
||
| sym::ptr_offset_from | ||
|
||
| sym::transmute | ||
|
||
| sym::simd_insert | ||
|
||
| sym::simd_extract | ||
|
||
=> Some(true), | ||
|
||
_ => Some(false) | ||
} | ||
} | ||
_ => None | ||
} | ||
} | ||
|
||
/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether | ||
/// said intrinsic is on the whitelist for being const callable. | ||
fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool { | ||
let hir_id = tcx.hir().as_local_hir_id(def_id) | ||
.expect("Non-local call to local provider is_const_fn"); | ||
|
||
let node = tcx.hir().get(hir_id); | ||
if let Some(fn_like) = FnLikeNode::from_node(node) { | ||
|
||
if let Some(whitelisted) = is_const_intrinsic(tcx, def_id) { | ||
whitelisted | ||
} else if let Some(fn_like) = FnLikeNode::from_node(node) { | ||
fn_like.constness() == hir::Constness::Const | ||
} else if let hir::Node::Ctor(_) = node { | ||
true | ||
|
Uh oh!
There was an error while loading. Please reload this page.