Add is_intrinsic helper

This commit is contained in:
Oli Scherer 2024-02-19 22:51:45 +00:00
parent 7606c13961
commit aa2ae6b491
6 changed files with 15 additions and 4 deletions

View File

@ -545,7 +545,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(def_id) = def_id if let Some(def_id) = def_id
&& self.tcx.def_kind(def_id) == hir::def::DefKind::Fn && self.tcx.def_kind(def_id) == hir::def::DefKind::Fn
&& matches!(self.tcx.intrinsic(def_id), Some(sym::const_eval_select)) && self.tcx.is_intrinsic(def_id, sym::const_eval_select)
{ {
let fn_sig = self.resolve_vars_if_possible(fn_sig); let fn_sig = self.resolve_vars_if_possible(fn_sig);
for idx in 0..=1 { for idx in 0..=1 {

View File

@ -1231,7 +1231,7 @@ impl<'tcx> LateLintPass<'tcx> for MutableTransmutes {
} }
fn def_id_is_transmute(cx: &LateContext<'_>, def_id: DefId) -> bool { fn def_id_is_transmute(cx: &LateContext<'_>, def_id: DefId) -> bool {
matches!(cx.tcx.intrinsic(def_id), Some(sym::transmute)) cx.tcx.is_intrinsic(def_id, sym::transmute)
} }
} }
} }

View File

@ -0,0 +1,10 @@
use rustc_span::{def_id::DefId, Symbol};
use super::TyCtxt;
impl TyCtxt<'_> {
pub fn is_intrinsic(self, def_id: DefId, name: Symbol) -> bool {
let Some(i) = self.intrinsic(def_id) else { return false };
i == name
}
}

View File

@ -149,6 +149,7 @@ mod generic_args;
mod generics; mod generics;
mod impls_ty; mod impls_ty;
mod instance; mod instance;
mod intrinsic;
mod list; mod list;
mod opaque_types; mod opaque_types;
mod parameterized; mod parameterized;

View File

@ -160,7 +160,7 @@ fn remap_mir_for_const_eval_select<'tcx>(
fn_span, fn_span,
.. ..
} if let ty::FnDef(def_id, _) = *const_.ty().kind() } if let ty::FnDef(def_id, _) = *const_.ty().kind()
&& matches!(tcx.intrinsic(def_id), Some(sym::const_eval_select)) => && tcx.is_intrinsic(def_id, sym::const_eval_select) =>
{ {
let [tupled_args, called_in_const, called_at_rt]: [_; 3] = let [tupled_args, called_in_const, called_at_rt]: [_; 3] =
std::mem::take(args).try_into().unwrap(); std::mem::take(args).try_into().unwrap();

View File

@ -334,7 +334,7 @@ fn check_terminator<'tcx>(
// within const fns. `transmute` is allowed in all other const contexts. // within const fns. `transmute` is allowed in all other const contexts.
// This won't really scale to more intrinsics or functions. Let's allow const // This won't really scale to more intrinsics or functions. Let's allow const
// transmutes in const fn before we add more hacks to this. // transmutes in const fn before we add more hacks to this.
if matches!(tcx.intrinsic(fn_def_id), Some(sym::transmute)) { if tcx.is_intrinsic(fn_def_id, sym::transmute) {
return Err(( return Err((
span, span,
"can only call `transmute` from const items, not `const fn`".into(), "can only call `transmute` from const items, not `const fn`".into(),