Skip to content

Commit 09fd556

Browse files
committed
Make check_intrinsic_type not require ForeignItems anymore
1 parent 79daf61 commit 09fd556

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,18 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
533533
match abi {
534534
Abi::RustIntrinsic => {
535535
for item in items {
536-
let item = tcx.hir().foreign_item(item.id);
537-
intrinsic::check_intrinsic_type(tcx, item);
536+
intrinsic::check_intrinsic_type(tcx, item.id.owner_id.def_id, item.span);
538537
}
539538
}
540539

541540
Abi::PlatformIntrinsic => {
542541
for item in items {
543-
let item = tcx.hir().foreign_item(item.id);
544-
intrinsic::check_platform_intrinsic_type(tcx, item);
542+
intrinsic::check_platform_intrinsic_type(
543+
tcx,
544+
item.id.owner_id.def_id,
545+
item.span,
546+
item.ident.name,
547+
);
545548
}
546549
}
547550

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
1313
use rustc_middle::ty::{self, Ty, TyCtxt};
1414
use rustc_span::def_id::LocalDefId;
1515
use rustc_span::symbol::{kw, sym};
16-
use rustc_span::Span;
16+
use rustc_span::{Span, Symbol};
1717
use rustc_target::spec::abi::Abi;
1818

1919
fn equate_intrinsic_type<'tcx>(
@@ -136,19 +136,18 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -
136136

137137
/// Remember to add all intrinsics here, in `compiler/rustc_codegen_llvm/src/intrinsic.rs`,
138138
/// and in `library/core/src/intrinsics.rs`.
139-
pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
140-
let generics = tcx.generics_of(it.owner_id);
139+
pub fn check_intrinsic_type(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId, span: Span) {
140+
let generics = tcx.generics_of(intrinsic_id);
141141
let param = |n| {
142142
if let Some(&ty::GenericParamDef {
143143
name, kind: ty::GenericParamDefKind::Type { .. }, ..
144144
}) = generics.opt_param_at(n as usize, tcx)
145145
{
146146
Ty::new_param(tcx, n, name)
147147
} else {
148-
Ty::new_error_with_message(tcx, tcx.def_span(it.owner_id), "expected param")
148+
Ty::new_error_with_message(tcx, span, "expected param")
149149
}
150150
};
151-
let intrinsic_id = it.owner_id.def_id;
152151
let intrinsic_name = tcx.item_name(intrinsic_id.into());
153152
let name_str = intrinsic_name.as_str();
154153

@@ -191,7 +190,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
191190
| "umin" => (1, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], param(0)),
192191
"fence" | "singlethreadfence" => (0, Vec::new(), Ty::new_unit(tcx)),
193192
op => {
194-
tcx.dcx().emit_err(UnrecognizedAtomicOperation { span: it.span, op });
193+
tcx.dcx().emit_err(UnrecognizedAtomicOperation { span, op });
195194
return;
196195
}
197196
};
@@ -479,33 +478,36 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
479478
sym::debug_assertions => (0, Vec::new(), tcx.types.bool),
480479

481480
other => {
482-
tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other });
481+
tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span, name: other });
483482
return;
484483
}
485484
};
486485
(n_tps, 0, inputs, output, unsafety)
487486
};
488487
let sig = tcx.mk_fn_sig(inputs, output, false, unsafety, Abi::RustIntrinsic);
489488
let sig = ty::Binder::bind_with_vars(sig, bound_vars);
490-
equate_intrinsic_type(tcx, it.span, intrinsic_id, n_tps, n_lts, 0, sig)
489+
equate_intrinsic_type(tcx, span, intrinsic_id, n_tps, n_lts, 0, sig)
491490
}
492491

493492
/// Type-check `extern "platform-intrinsic" { ... }` functions.
494-
pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
495-
let generics = tcx.generics_of(it.owner_id);
493+
pub fn check_platform_intrinsic_type(
494+
tcx: TyCtxt<'_>,
495+
intrinsic_id: LocalDefId,
496+
span: Span,
497+
name: Symbol,
498+
) {
499+
let generics = tcx.generics_of(intrinsic_id);
496500
let param = |n| {
497501
if let Some(&ty::GenericParamDef {
498502
name, kind: ty::GenericParamDefKind::Type { .. }, ..
499503
}) = generics.opt_param_at(n as usize, tcx)
500504
{
501505
Ty::new_param(tcx, n, name)
502506
} else {
503-
Ty::new_error_with_message(tcx, tcx.def_span(it.owner_id), "expected param")
507+
Ty::new_error_with_message(tcx, span, "expected param")
504508
}
505509
};
506510

507-
let name = it.ident.name;
508-
509511
let (n_tps, n_cts, inputs, output) = match name {
510512
sym::simd_eq | sym::simd_ne | sym::simd_lt | sym::simd_le | sym::simd_gt | sym::simd_ge => {
511513
(2, 0, vec![param(0), param(0)], param(1))
@@ -578,12 +580,12 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>)
578580
sym::simd_shuffle_generic => (2, 1, vec![param(0), param(0)], param(1)),
579581
_ => {
580582
let msg = format!("unrecognized platform-specific intrinsic function: `{name}`");
581-
tcx.dcx().span_err(it.span, msg);
583+
tcx.dcx().span_err(span, msg);
582584
return;
583585
}
584586
};
585587

586588
let sig = tcx.mk_fn_sig(inputs, output, false, hir::Unsafety::Unsafe, Abi::PlatformIntrinsic);
587589
let sig = ty::Binder::dummy(sig);
588-
equate_intrinsic_type(tcx, it.span, it.owner_id.def_id, n_tps, 0, n_cts, sig)
590+
equate_intrinsic_type(tcx, span, intrinsic_id, n_tps, 0, n_cts, sig)
589591
}

0 commit comments

Comments
 (0)