Skip to content

Commit 77d50a8

Browse files
committed
Auto merge of rust-lang#109092 - compiler-errors:local-key, r=cjgillot
Make local query providers receive local keys When a query is marked `separate_provide_extern`, we can map a query key to a "local" form of the key, e.g. `DefId` -> `LocalDefId`. This simplifies a ton of code which either has to assert or use something like `expect_local` to assert that the query key is local.
2 parents 6667682 + b1a957b commit 77d50a8

File tree

78 files changed

+438
-632
lines changed

Some content is hidden

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

78 files changed

+438
-632
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
284284
// hidden type is well formed even without those bounds.
285285
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(definition_ty.into()));
286286

287-
let id_substs = InternalSubsts::identity_for_item(self.tcx, def_id.to_def_id());
287+
let id_substs = InternalSubsts::identity_for_item(self.tcx, def_id);
288288

289289
// Require that the hidden type actually fulfills all the bounds of the opaque type, even without
290290
// the bounds that the function supplies.

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1010
use rustc_middle::middle::exported_symbols::{
1111
metadata_symbol_name, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
1212
};
13+
use rustc_middle::query::LocalCrate;
1314
use rustc_middle::ty::query::{ExternProviders, Providers};
1415
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
1516
use rustc_middle::ty::Instance;
@@ -41,9 +42,7 @@ pub fn crates_export_threshold(crate_types: &[CrateType]) -> SymbolExportLevel {
4142
}
4243
}
4344

44-
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<SymbolExportInfo> {
45-
assert_eq!(cnum, LOCAL_CRATE);
46-
45+
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<SymbolExportInfo> {
4746
if !tcx.sess.opts.output_types.should_codegen() {
4847
return Default::default();
4948
}
@@ -154,10 +153,10 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
154153
reachable_non_generics
155154
}
156155

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

160-
if let Some(&info) = tcx.reachable_non_generics(def_id.krate).get(&def_id) {
159+
if let Some(&info) = tcx.reachable_non_generics(LOCAL_CRATE).get(&def_id.to_def_id()) {
161160
info.level.is_below_threshold(export_threshold)
162161
} else {
163162
false
@@ -170,10 +169,8 @@ fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> b
170169

171170
fn exported_symbols_provider_local(
172171
tcx: TyCtxt<'_>,
173-
cnum: CrateNum,
172+
_: LocalCrate,
174173
) -> &[(ExportedSymbol<'_>, SymbolExportInfo)] {
175-
assert_eq!(cnum, LOCAL_CRATE);
176-
177174
if !tcx.sess.opts.output_types.should_codegen() {
178175
return &[];
179176
}

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/astconv/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub trait AstConv<'tcx> {
7575
fn get_type_parameter_bounds(
7676
&self,
7777
span: Span,
78-
def_id: DefId,
78+
def_id: LocalDefId,
7979
assoc_name: Ident,
8080
) -> ty::GenericPredicates<'tcx>;
8181

@@ -1773,9 +1773,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
17731773
ty_param_def_id, assoc_name, span,
17741774
);
17751775

1776-
let predicates = &self
1777-
.get_type_parameter_bounds(span, ty_param_def_id.to_def_id(), assoc_name)
1778-
.predicates;
1776+
let predicates =
1777+
&self.get_type_parameter_bounds(span, ty_param_def_id, assoc_name).predicates;
17791778

17801779
debug!("find_bound_for_assoc_item: predicates={:#?}", predicates);
17811780

compiler/rustc_hir_analysis/src/check/check.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ fn check_opaque(tcx: TyCtxt<'_>, id: hir::ItemId) {
211211
return;
212212
}
213213

214-
let substs = InternalSubsts::identity_for_item(tcx, item.owner_id.to_def_id());
214+
let substs = InternalSubsts::identity_for_item(tcx, item.owner_id);
215215
let span = tcx.def_span(item.owner_id.def_id);
216216

217217
if !tcx.features().impl_trait_projections {
@@ -304,7 +304,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes(
304304
..
305305
}) = item.kind
306306
{
307-
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
307+
let substs = InternalSubsts::identity_for_item(tcx, def_id);
308308
let opaque_identity_ty = if in_trait {
309309
tcx.mk_projection(def_id.to_def_id(), substs)
310310
} else {
@@ -535,7 +535,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
535535
}
536536
ty::AssocKind::Type if assoc_item.defaultness(tcx).has_value() => {
537537
let trait_substs =
538-
InternalSubsts::identity_for_item(tcx, id.owner_id.to_def_id());
538+
InternalSubsts::identity_for_item(tcx, id.owner_id);
539539
let _: Result<_, rustc_errors::ErrorGuaranteed> = check_type_bounds(
540540
tcx,
541541
assoc_item,
@@ -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/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ fn check_variances_for_type_defn<'tcx>(
17941794

17951795
// Lazily calculated because it is only needed in case of an error.
17961796
let explicitly_bounded_params = LazyCell::new(|| {
1797-
let icx = crate::collect::ItemCtxt::new(tcx, item.owner_id.to_def_id());
1797+
let icx = crate::collect::ItemCtxt::new(tcx, item.owner_id.def_id);
17981798
hir_generics
17991799
.predicates
18001800
.iter()

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[..],

0 commit comments

Comments
 (0)