Skip to content

Commit d5f1215

Browse files
committed
Make the signature of equate_intrinsic_type support items other than ForeignItem
1 parent 7546af0 commit d5f1215

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,35 @@ use crate::errors::{
77
WrongNumberOfGenericArgumentsToIntrinsic,
88
};
99

10-
use hir::def_id::DefId;
1110
use rustc_errors::{codes::*, struct_span_code_err, DiagnosticMessage};
1211
use rustc_hir as hir;
1312
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
1413
use rustc_middle::ty::{self, Ty, TyCtxt};
14+
use rustc_span::def_id::LocalDefId;
1515
use rustc_span::symbol::{kw, sym, Symbol};
16+
use rustc_span::Span;
1617
use rustc_target::spec::abi::Abi;
1718

1819
fn equate_intrinsic_type<'tcx>(
1920
tcx: TyCtxt<'tcx>,
20-
it: &hir::ForeignItem<'_>,
21+
span: Span,
22+
def_id: LocalDefId,
2123
n_tps: usize,
2224
n_lts: usize,
2325
n_cts: usize,
2426
sig: ty::PolyFnSig<'tcx>,
2527
) {
26-
let (own_counts, span) = match &it.kind {
27-
hir::ForeignItemKind::Fn(.., generics) => {
28-
let own_counts = tcx.generics_of(it.owner_id.to_def_id()).own_counts();
28+
let (own_counts, span) = match tcx.hir_node_by_def_id(def_id) {
29+
hir::Node::ForeignItem(hir::ForeignItem {
30+
kind: hir::ForeignItemKind::Fn(.., generics),
31+
..
32+
}) => {
33+
let own_counts = tcx.generics_of(def_id).own_counts();
2934
(own_counts, generics.span)
3035
}
3136
_ => {
32-
struct_span_code_err!(tcx.dcx(), it.span, E0622, "intrinsic must be a function")
33-
.with_span_label(it.span, "expected a function")
37+
struct_span_code_err!(tcx.dcx(), span, E0622, "intrinsic must be a function")
38+
.with_span_label(span, "expected a function")
3439
.emit();
3540
return;
3641
}
@@ -54,23 +59,22 @@ fn equate_intrinsic_type<'tcx>(
5459
&& gen_count_ok(own_counts.types, n_tps, "type")
5560
&& gen_count_ok(own_counts.consts, n_cts, "const")
5661
{
57-
let it_def_id = it.owner_id.def_id;
5862
let _ = check_function_signature(
5963
tcx,
60-
ObligationCause::new(it.span, it_def_id, ObligationCauseCode::IntrinsicType),
61-
it_def_id.into(),
64+
ObligationCause::new(span, def_id, ObligationCauseCode::IntrinsicType),
65+
def_id.into(),
6266
sig,
6367
);
6468
}
6569
}
6670

6771
/// Returns the unsafety of the given intrinsic.
68-
pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: DefId) -> hir::Unsafety {
72+
pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hir::Unsafety {
6973
let has_safe_attr = match tcx.has_attr(intrinsic_id, sym::rustc_safe_intrinsic) {
7074
true => hir::Unsafety::Normal,
7175
false => hir::Unsafety::Unsafe,
7276
};
73-
let is_in_list = match tcx.item_name(intrinsic_id) {
77+
let is_in_list = match tcx.item_name(intrinsic_id.into()) {
7478
// When adding a new intrinsic to this list,
7579
// it's usually worth updating that intrinsic's documentation
7680
// to note that it's safe to call, since
@@ -121,7 +125,7 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: DefId) -> hir
121125
tcx.def_span(intrinsic_id),
122126
DiagnosticMessage::from(format!(
123127
"intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `{}`",
124-
tcx.item_name(intrinsic_id)
128+
tcx.item_name(intrinsic_id.into())
125129
)
126130
)).emit();
127131
}
@@ -133,8 +137,8 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: DefId) -> hir
133137
/// and in `library/core/src/intrinsics.rs`.
134138
pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
135139
let param = |n| Ty::new_param(tcx, n, Symbol::intern(&format!("P{n}")));
136-
let intrinsic_id = it.owner_id.to_def_id();
137-
let intrinsic_name = tcx.item_name(intrinsic_id);
140+
let intrinsic_id = it.owner_id.def_id;
141+
let intrinsic_name = tcx.item_name(intrinsic_id.into());
138142
let name_str = intrinsic_name.as_str();
139143

140144
let bound_vars = tcx.mk_bound_variable_kinds(&[
@@ -470,7 +474,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
470474
};
471475
let sig = tcx.mk_fn_sig(inputs, output, false, unsafety, Abi::RustIntrinsic);
472476
let sig = ty::Binder::bind_with_vars(sig, bound_vars);
473-
equate_intrinsic_type(tcx, it, n_tps, n_lts, 0, sig)
477+
equate_intrinsic_type(tcx, it.span, intrinsic_id, n_tps, n_lts, 0, sig)
474478
}
475479

476480
/// Type-check `extern "platform-intrinsic" { ... }` functions.
@@ -561,5 +565,5 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>)
561565

562566
let sig = tcx.mk_fn_sig(inputs, output, false, hir::Unsafety::Unsafe, Abi::PlatformIntrinsic);
563567
let sig = ty::Binder::dummy(sig);
564-
equate_intrinsic_type(tcx, it, n_tps, 0, n_cts, sig)
568+
equate_intrinsic_type(tcx, it.span, it.owner_id.def_id, n_tps, 0, n_cts, sig)
565569
}

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
14821482
abi: abi::Abi,
14831483
) -> ty::PolyFnSig<'tcx> {
14841484
let unsafety = if abi == abi::Abi::RustIntrinsic {
1485-
intrinsic_operation_unsafety(tcx, def_id.to_def_id())
1485+
intrinsic_operation_unsafety(tcx, def_id)
14861486
} else {
14871487
hir::Unsafety::Unsafe
14881488
};

0 commit comments

Comments
 (0)