From aa2ae6b49113e82228a8112ae88d3905afc3ef9b Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 19 Feb 2024 22:51:45 +0000 Subject: [PATCH] Add `is_intrinsic` helper --- compiler/rustc_hir_typeck/src/callee.rs | 2 +- compiler/rustc_lint/src/builtin.rs | 2 +- compiler/rustc_middle/src/ty/intrinsic.rs | 10 ++++++++++ compiler/rustc_middle/src/ty/mod.rs | 1 + compiler/rustc_mir_transform/src/lib.rs | 2 +- .../clippy/clippy_utils/src/qualify_min_const_fn.rs | 2 +- 6 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 compiler/rustc_middle/src/ty/intrinsic.rs diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index b87d71e3533..25d34531379 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -545,7 +545,7 @@ fn confirm_builtin_call( if let Some(def_id) = def_id && 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); for idx in 0..=1 { diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 9f73d2e6812..df76988f1c3 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1231,7 +1231,7 @@ fn get_transmute_from_to<'tcx>( } 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) } } } diff --git a/compiler/rustc_middle/src/ty/intrinsic.rs b/compiler/rustc_middle/src/ty/intrinsic.rs new file mode 100644 index 00000000000..c23b5dc5777 --- /dev/null +++ b/compiler/rustc_middle/src/ty/intrinsic.rs @@ -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 + } +} diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index f005a240504..4bd39322b3d 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -149,6 +149,7 @@ mod generics; mod impls_ty; mod instance; +mod intrinsic; mod list; mod opaque_types; mod parameterized; diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 945c3c662a6..86f37456183 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -160,7 +160,7 @@ fn remap_mir_for_const_eval_select<'tcx>( fn_span, .. } 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] = std::mem::take(args).try_into().unwrap(); diff --git a/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs b/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs index 183dbe3aecc..e369cb9d0a4 100644 --- a/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs +++ b/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs @@ -334,7 +334,7 @@ fn check_terminator<'tcx>( // 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 // 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(( span, "can only call `transmute` from const items, not `const fn`".into(),