Skip to content

Commit 2eb1c08

Browse files
Use local key in providers
1 parent a01b4cc commit 2eb1c08

File tree

65 files changed

+458
-395
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+458
-395
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ pub fn crates_export_threshold(crate_types: &[CrateType]) -> SymbolExportLevel {
4141
}
4242
}
4343

44-
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<SymbolExportInfo> {
45-
assert_eq!(cnum, LOCAL_CRATE);
46-
44+
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, (): ()) -> DefIdMap<SymbolExportInfo> {
4745
if !tcx.sess.opts.output_types.should_codegen() {
4846
return Default::default();
4947
}
@@ -154,10 +152,10 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
154152
reachable_non_generics
155153
}
156154

157-
fn is_reachable_non_generic_provider_local(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
155+
fn is_reachable_non_generic_provider_local(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
158156
let export_threshold = threshold(tcx);
159157

160-
if let Some(&info) = tcx.reachable_non_generics(def_id.krate).get(&def_id) {
158+
if let Some(&info) = tcx.reachable_non_generics(LOCAL_CRATE).get(&def_id.to_def_id()) {
161159
info.level.is_below_threshold(export_threshold)
162160
} else {
163161
false
@@ -170,10 +168,8 @@ fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> b
170168

171169
fn exported_symbols_provider_local(
172170
tcx: TyCtxt<'_>,
173-
cnum: CrateNum,
171+
(): (),
174172
) -> &[(ExportedSymbol<'_>, SymbolExportInfo)] {
175-
assert_eq!(cnum, LOCAL_CRATE);
176-
177173
if !tcx.sess.opts.output_types.should_codegen() {
178174
return &[];
179175
}

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
4343
}
4444
}
4545

46-
fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
46+
fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
4747
if cfg!(debug_assertions) {
4848
let def_kind = tcx.def_kind(did);
4949
assert!(
@@ -52,7 +52,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
5252
);
5353
}
5454

55-
let did = did.expect_local();
5655
let attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(did));
5756
let mut codegen_fn_attrs = CodegenFnAttrs::new();
5857
if tcx.should_inherit_track_caller(did) {

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3232
/// it is a trait impl/function, return if it has a `const` modifier. If it is an intrinsic,
3333
/// report whether said intrinsic has a `rustc_const_{un,}stable` attribute. Otherwise, return
3434
/// `Constness::NotConst`.
35-
fn constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
36-
let def_id = def_id.expect_local();
35+
fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
3736
let node = tcx.hir().get_by_def_id(def_id);
3837

3938
match node {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
246246
self.check_local_or_return_ty(return_ty.skip_binder(), RETURN_PLACE);
247247
}
248248

249-
if !tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
249+
if !tcx.has_attr(def_id, sym::rustc_do_not_const_check) {
250250
self.visit_body(&body);
251251
}
252252

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn check_live_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
3030
return;
3131
}
3232

33-
if tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
33+
if tcx.has_attr(def_id, sym::rustc_do_not_const_check) {
3434
return;
3535
}
3636

compiler/rustc_hir/src/hir_id.rs

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ impl From<OwnerId> for HirId {
2222
}
2323
}
2424

25+
impl From<OwnerId> for DefId {
26+
fn from(value: OwnerId) -> Self {
27+
value.to_def_id()
28+
}
29+
}
30+
2531
impl OwnerId {
2632
#[inline]
2733
pub fn to_def_id(self) -> DefId {

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ fn check_enum(tcx: TyCtxt<'_>, def_id: LocalDefId) {
11611161
def.destructor(tcx); // force the destructor to be evaluated
11621162

11631163
if def.variants().is_empty() {
1164-
if let Some(attr) = tcx.get_attrs(def_id.to_def_id(), sym::repr).next() {
1164+
if let Some(attr) = tcx.get_attrs(def_id, sym::repr).next() {
11651165
struct_span_err!(
11661166
tcx.sess,
11671167
attr.span,

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,13 @@ fn compare_asyncness<'tcx>(
583583
#[instrument(skip(tcx), level = "debug", ret)]
584584
pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
585585
tcx: TyCtxt<'tcx>,
586-
def_id: DefId,
586+
impl_m_def_id: LocalDefId,
587587
) -> Result<&'tcx FxHashMap<DefId, Ty<'tcx>>, ErrorGuaranteed> {
588-
let impl_m = tcx.opt_associated_item(def_id).unwrap();
588+
let impl_m = tcx.opt_associated_item(impl_m_def_id.to_def_id()).unwrap();
589589
let trait_m = tcx.opt_associated_item(impl_m.trait_item_def_id.unwrap()).unwrap();
590590
let impl_trait_ref =
591591
tcx.impl_trait_ref(impl_m.impl_container(tcx).unwrap()).unwrap().subst_identity();
592-
let param_env = tcx.param_env(def_id);
592+
let param_env = tcx.param_env(impl_m_def_id);
593593

594594
// First, check a few of the same things as `compare_impl_method`,
595595
// just so we don't ICE during substitution later.
@@ -599,7 +599,6 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
599599

600600
let trait_to_impl_substs = impl_trait_ref.substs;
601601

602-
let impl_m_def_id = impl_m.def_id.expect_local();
603602
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m_def_id);
604603
let return_span = tcx.hir().fn_decl_by_hir_id(impl_m_hir_id).unwrap().output.span();
605604
let cause = ObligationCause::new(

compiler/rustc_hir_analysis/src/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ pub fn provide(providers: &mut Providers) {
109109
};
110110
}
111111

112-
fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::Destructor> {
113-
tcx.calculate_dtor(def_id, dropck::check_drop_impl)
112+
fn adt_destructor(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::Destructor> {
113+
tcx.calculate_dtor(def_id.to_def_id(), dropck::check_drop_impl)
114114
}
115115

116116
/// Given a `DefId` for an opaque type in return position, find its parent item's return

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,8 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
368368
}
369369
}
370370

371-
pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedInfo {
371+
pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) -> CoerceUnsizedInfo {
372372
debug!("compute_coerce_unsized_info(impl_did={:?})", impl_did);
373-
374-
// this provider should only get invoked for local def-ids
375-
let impl_did = impl_did.expect_local();
376373
let span = tcx.def_span(impl_did);
377374

378375
let coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, Some(span));

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use rustc_errors::struct_span_err;
1111
use rustc_hir as hir;
1212
use rustc_hir::def::DefKind;
13-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
13+
use rustc_hir::def_id::{DefId, LocalDefId};
1414
use rustc_middle::ty::fast_reject::{simplify_type, SimplifiedType, TreatParams, TreatProjections};
1515
use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt};
1616
use rustc_span::symbol::sym;
@@ -24,17 +24,15 @@ pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls {
2424
collect.impls_map
2525
}
2626

27-
pub fn crate_incoherent_impls(tcx: TyCtxt<'_>, (_, simp): (CrateNum, SimplifiedType)) -> &[DefId] {
27+
pub fn crate_incoherent_impls(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
2828
let crate_map = tcx.crate_inherent_impls(());
2929
tcx.arena.alloc_from_iter(
3030
crate_map.incoherent_impls.get(&simp).unwrap_or(&Vec::new()).iter().map(|d| d.to_def_id()),
3131
)
3232
}
3333

3434
/// On-demand query: yields a vector of the inherent impls for a specific type.
35-
pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: DefId) -> &[DefId] {
36-
let ty_def_id = ty_def_id.expect_local();
37-
35+
pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: LocalDefId) -> &[DefId] {
3836
let crate_map = tcx.crate_inherent_impls(());
3937
match crate_map.inherent_impls.get(&ty_def_id) {
4038
Some(v) => &v[..],

compiler/rustc_hir_analysis/src/collect.rs

+29-32
Original file line numberDiff line numberDiff line change
@@ -839,17 +839,15 @@ fn convert_variant(
839839
adt_kind,
840840
parent_did.to_def_id(),
841841
recovered,
842-
adt_kind == AdtKind::Struct && tcx.has_attr(parent_did.to_def_id(), sym::non_exhaustive)
843-
|| variant_did.map_or(false, |variant_did| {
844-
tcx.has_attr(variant_did.to_def_id(), sym::non_exhaustive)
845-
}),
842+
adt_kind == AdtKind::Struct && tcx.has_attr(parent_did, sym::non_exhaustive)
843+
|| variant_did
844+
.map_or(false, |variant_did| tcx.has_attr(variant_did, sym::non_exhaustive)),
846845
)
847846
}
848847

849-
fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtDef<'_> {
848+
fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
850849
use rustc_hir::*;
851850

852-
let def_id = def_id.expect_local();
853851
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
854852
let Node::Item(item) = tcx.hir().get(hir_id) else {
855853
bug!();
@@ -908,8 +906,8 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtDef<'_> {
908906
tcx.mk_adt_def(def_id.to_def_id(), kind, variants, repr)
909907
}
910908

911-
fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
912-
let item = tcx.hir().expect_item(def_id.expect_local());
909+
fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
910+
let item = tcx.hir().expect_item(def_id);
913911

914912
let (is_auto, unsafety, items) = match item.kind {
915913
hir::ItemKind::Trait(is_auto, unsafety, .., items) => {
@@ -1036,7 +1034,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
10361034
});
10371035

10381036
ty::TraitDef {
1039-
def_id,
1037+
def_id: def_id.to_def_id(),
10401038
unsafety,
10411039
paren_sugar,
10421040
has_auto_impl: is_auto,
@@ -1091,11 +1089,10 @@ pub fn get_infer_ret_ty<'hir>(output: &'hir hir::FnRetTy<'hir>) -> Option<&'hir
10911089
}
10921090

10931091
#[instrument(level = "debug", skip(tcx))]
1094-
fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<ty::PolyFnSig<'_>> {
1092+
fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<ty::PolyFnSig<'_>> {
10951093
use rustc_hir::Node::*;
10961094
use rustc_hir::*;
10971095

1098-
let def_id = def_id.expect_local();
10991096
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
11001097

11011098
let icx = ItemCtxt::new(tcx, def_id.to_def_id());
@@ -1338,9 +1335,12 @@ fn suggest_impl_trait<'tcx>(
13381335
None
13391336
}
13401337

1341-
fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::EarlyBinder<ty::TraitRef<'_>>> {
1342-
let icx = ItemCtxt::new(tcx, def_id);
1343-
let impl_ = tcx.hir().expect_item(def_id.expect_local()).expect_impl();
1338+
fn impl_trait_ref(
1339+
tcx: TyCtxt<'_>,
1340+
def_id: LocalDefId,
1341+
) -> Option<ty::EarlyBinder<ty::TraitRef<'_>>> {
1342+
let icx = ItemCtxt::new(tcx, def_id.to_def_id());
1343+
let impl_ = tcx.hir().expect_item(def_id).expect_impl();
13441344
impl_
13451345
.of_trait
13461346
.as_ref()
@@ -1380,9 +1380,9 @@ fn check_impl_constness(
13801380
}
13811381
}
13821382

1383-
fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity {
1383+
fn impl_polarity(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplPolarity {
13841384
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
1385-
let item = tcx.hir().expect_item(def_id.expect_local());
1385+
let item = tcx.hir().expect_item(def_id);
13861386
match &item.kind {
13871387
hir::ItemKind::Impl(hir::Impl {
13881388
polarity: hir::ImplPolarity::Negative(span),
@@ -1515,31 +1515,28 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
15151515
fty
15161516
}
15171517

1518-
fn is_foreign_item(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
1519-
match tcx.hir().get_if_local(def_id) {
1520-
Some(Node::ForeignItem(..)) => true,
1521-
Some(_) => false,
1522-
_ => bug!("is_foreign_item applied to non-local def-id {:?}", def_id),
1518+
fn is_foreign_item(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
1519+
match tcx.hir().get_by_def_id(def_id) {
1520+
Node::ForeignItem(..) => true,
1521+
_ => false,
15231522
}
15241523
}
15251524

1526-
fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind> {
1527-
match tcx.hir().get_if_local(def_id) {
1528-
Some(Node::Expr(&rustc_hir::Expr {
1525+
fn generator_kind(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<hir::GeneratorKind> {
1526+
match tcx.hir().get_by_def_id(def_id) {
1527+
Node::Expr(&rustc_hir::Expr {
15291528
kind: rustc_hir::ExprKind::Closure(&rustc_hir::Closure { body, .. }),
15301529
..
1531-
})) => tcx.hir().body(body).generator_kind(),
1532-
Some(_) => None,
1533-
_ => bug!("generator_kind applied to non-local def-id {:?}", def_id),
1530+
}) => tcx.hir().body(body).generator_kind(),
1531+
_ => None,
15341532
}
15351533
}
15361534

1537-
fn is_type_alias_impl_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
1538-
match tcx.hir().get_if_local(def_id) {
1539-
Some(Node::Item(hir::Item { kind: hir::ItemKind::OpaqueTy(opaque), .. })) => {
1535+
fn is_type_alias_impl_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
1536+
match tcx.hir().get_by_def_id(def_id) {
1537+
Node::Item(hir::Item { kind: hir::ItemKind::OpaqueTy(opaque), .. }) => {
15401538
matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias)
15411539
}
1542-
Some(_) => bug!("tried getting opaque_ty_origin for non-opaque: {:?}", def_id),
1543-
_ => bug!("tried getting opaque_ty_origin for non-local def-id {:?}", def_id),
1540+
_ => bug!("tried getting opaque_ty_origin for non-opaque: {:?}", def_id),
15441541
}
15451542
}

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ use hir::{
55
};
66
use rustc_hir as hir;
77
use rustc_hir::def::DefKind;
8-
use rustc_hir::def_id::DefId;
8+
use rustc_hir::def_id::LocalDefId;
99
use rustc_middle::ty::{self, TyCtxt};
1010
use rustc_session::lint;
1111
use rustc_span::symbol::{kw, Symbol};
1212
use rustc_span::Span;
1313

14-
pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
14+
pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
1515
use rustc_hir::*;
1616

17-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
17+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1818

1919
let node = tcx.hir().get(hir_id);
2020
let parent_def_id = match node {
@@ -121,7 +121,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
121121
Some(parent_def_id.to_def_id())
122122
}
123123
Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) => {
124-
Some(tcx.typeck_root_def_id(def_id))
124+
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
125125
}
126126
// Exclude `GlobalAsm` here which cannot have generics.
127127
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
@@ -140,7 +140,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
140140
}
141141
}
142142
Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
143-
Some(tcx.typeck_root_def_id(def_id))
143+
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
144144
}
145145
Node::Item(item) => match item.kind {
146146
ItemKind::OpaqueTy(hir::OpaqueTy {
@@ -189,7 +189,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
189189
let opt_self = Some(ty::GenericParamDef {
190190
index: 0,
191191
name: kw::SelfUpper,
192-
def_id,
192+
def_id: def_id.to_def_id(),
193193
pure_wrt_drop: false,
194194
kind: ty::GenericParamDefKind::Type {
195195
has_default: false,
@@ -326,7 +326,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
326326
params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {
327327
index: next_index(),
328328
name: Symbol::intern(arg),
329-
def_id,
329+
def_id: def_id.to_def_id(),
330330
pure_wrt_drop: false,
331331
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
332332
}));
@@ -339,7 +339,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
339339
params.push(ty::GenericParamDef {
340340
index: next_index(),
341341
name: Symbol::intern("<const_ty>"),
342-
def_id,
342+
def_id: def_id.to_def_id(),
343343
pure_wrt_drop: false,
344344
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
345345
});

0 commit comments

Comments
 (0)