Skip to content

Commit 0a6b691

Browse files
committed
Add a query for checking whether a function is an intrinsic.
1 parent 18bd2dd commit 0a6b691

File tree

17 files changed

+51
-44
lines changed

17 files changed

+51
-44
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_hir::def_id::{DefId, LocalDefId};
44
use rustc_middle::ty::query::Providers;
55
use rustc_middle::ty::{DefIdTree, TyCtxt};
66
use rustc_span::symbol::Symbol;
7-
use rustc_target::spec::abi::Abi;
87

98
/// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it
109
pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
@@ -34,10 +33,7 @@ fn impl_constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
3433
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
3534
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
3635
// foreign items cannot be evaluated at compile-time.
37-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
38-
let is_const = if let Abi::RustIntrinsic | Abi::PlatformIntrinsic =
39-
tcx.hir().get_foreign_abi(hir_id)
40-
{
36+
let is_const = if tcx.is_intrinsic(def_id) {
4137
tcx.lookup_const_stability(def_id).is_some()
4238
} else {
4339
false

compiler/rustc_const_eval/src/interpret/terminator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
312312
};
313313

314314
match instance.def {
315-
ty::InstanceDef::Intrinsic(..) => {
316-
assert!(caller_abi == Abi::RustIntrinsic || caller_abi == Abi::PlatformIntrinsic);
315+
ty::InstanceDef::Intrinsic(def_id) => {
316+
assert!(self.tcx.is_intrinsic(def_id));
317317
// caller_fn_abi is not relevant here, we interpret the arguments directly for each intrinsic.
318318
M::call_intrinsic(self, instance, args, ret, unwind)
319319
}

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
706706

707707
#[instrument(level = "debug", skip(self))]
708708
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
709-
use rustc_target::spec::abi::Abi::RustIntrinsic;
710-
711709
self.super_terminator(terminator, location);
712710

713711
match &terminator.kind {
@@ -889,7 +887,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
889887
return;
890888
}
891889

892-
let is_intrinsic = tcx.fn_sig(callee).abi() == RustIntrinsic;
890+
let is_intrinsic = tcx.is_intrinsic(callee);
893891

894892
if !tcx.is_const_fn_raw(callee) {
895893
if tcx.trait_of_item(callee).is_some() {

compiler/rustc_lint/src/builtin.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,6 @@ declare_lint_pass!(MutableTransmutes => [MUTABLE_TRANSMUTES]);
12521252

12531253
impl<'tcx> LateLintPass<'tcx> for MutableTransmutes {
12541254
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
1255-
use rustc_target::spec::abi::Abi::RustIntrinsic;
12561255
if let Some((&ty::Ref(_, _, from_mt), &ty::Ref(_, _, to_mt))) =
12571256
get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (ty1.kind(), ty2.kind()))
12581257
{
@@ -1287,8 +1286,7 @@ impl<'tcx> LateLintPass<'tcx> for MutableTransmutes {
12871286
}
12881287

12891288
fn def_id_is_transmute(cx: &LateContext<'_>, def_id: DefId) -> bool {
1290-
cx.tcx.fn_sig(def_id).abi() == RustIntrinsic
1291-
&& cx.tcx.item_name(def_id) == sym::transmute
1289+
cx.tcx.is_intrinsic(def_id) && cx.tcx.item_name(def_id) == sym::transmute
12921290
}
12931291
}
12941292
}

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
17521752
fn get_may_have_doc_links(self, index: DefIndex) -> bool {
17531753
self.root.tables.may_have_doc_links.get(self, index).is_some()
17541754
}
1755+
1756+
fn get_is_intrinsic(self, index: DefIndex) -> bool {
1757+
self.root.tables.is_intrinsic.get(self, index).is_some()
1758+
}
17551759
}
17561760

17571761
impl CrateMetadata {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
224224
tcx.arena.alloc_slice(&result)
225225
}
226226
defined_lib_features => { cdata.get_lib_features(tcx) }
227+
is_intrinsic => { cdata.get_is_intrinsic(def_id.index) }
227228
defined_lang_items => { cdata.get_lang_items(tcx) }
228229
diagnostic_items => { cdata.get_diagnostic_items() }
229230
missing_lang_items => { cdata.get_missing_lang_items(tcx) }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13131313
}
13141314
if impl_item.kind == ty::AssocKind::Fn {
13151315
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
1316+
if tcx.is_intrinsic(def_id) {
1317+
self.tables.is_intrinsic.set(def_id.index, ());
1318+
}
13161319
}
13171320
}
13181321

@@ -1557,6 +1560,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15571560
}
15581561
if let hir::ItemKind::Fn(..) = item.kind {
15591562
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
1563+
if tcx.is_intrinsic(def_id) {
1564+
self.tables.is_intrinsic.set(def_id.index, ());
1565+
}
15601566
}
15611567
if let hir::ItemKind::Impl { .. } = item.kind {
15621568
if let Some(trait_ref) = self.tcx.impl_trait_ref(def_id) {
@@ -1953,6 +1959,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19531959
self.encode_item_type(def_id);
19541960
if let hir::ForeignItemKind::Fn(..) = nitem.kind {
19551961
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
1962+
if tcx.is_intrinsic(def_id) {
1963+
self.tables.is_intrinsic.set(def_id.index, ());
1964+
}
19561965
}
19571966
}
19581967
}

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ define_tables! {
340340
impl_parent: Table<DefIndex, RawDefId>,
341341
impl_polarity: Table<DefIndex, ty::ImplPolarity>,
342342
impl_constness: Table<DefIndex, hir::Constness>,
343+
is_intrinsic: Table<DefIndex, ()>,
343344
impl_defaultness: Table<DefIndex, hir::Defaultness>,
344345
// FIXME(eddyb) perhaps compute this on the fly if cheap enough?
345346
coerce_unsized_info: Table<DefIndex, Lazy!(ty::adjustment::CoerceUnsizedInfo)>,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,11 @@ rustc_queries! {
15981598
desc { "calculating the lib features defined in a crate" }
15991599
separate_provide_extern
16001600
}
1601+
/// Whether the function is an intrinsic
1602+
query is_intrinsic(def_id: DefId) -> bool {
1603+
desc { |tcx| "is_intrinsic({})", tcx.def_path_str(def_id) }
1604+
separate_provide_extern
1605+
}
16011606
/// Returns the lang items defined in another crate by loading it from metadata.
16021607
query get_lang_items(_: ()) -> LanguageItems {
16031608
storage(ArenaCacheSelector<'tcx>)

compiler/rustc_middle/src/ty/util.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_hir::def_id::DefId;
2020
use rustc_macros::HashStable;
2121
use rustc_span::{sym, DUMMY_SP};
2222
use rustc_target::abi::{Integer, Size, TargetDataLayout};
23+
use rustc_target::spec::abi::Abi;
2324
use smallvec::SmallVec;
2425
use std::{fmt, iter};
2526

@@ -1169,6 +1170,12 @@ pub fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
11691170
.any(|items| items.iter().any(|item| item.has_name(sym::hidden)))
11701171
}
11711172

1173+
/// Determines whether an item is an intrinsic by Abi.
1174+
pub fn is_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
1175+
matches!(tcx.fn_sig(def_id).abi(), Abi::RustIntrinsic | Abi::PlatformIntrinsic)
1176+
}
1177+
11721178
pub fn provide(providers: &mut ty::query::Providers) {
1173-
*providers = ty::query::Providers { normalize_opaque_types, is_doc_hidden, ..*providers }
1179+
*providers =
1180+
ty::query::Providers { normalize_opaque_types, is_doc_hidden, is_intrinsic, ..*providers }
11741181
}

0 commit comments

Comments
 (0)