Skip to content

Commit c75fc57

Browse files
Noratriebfee1-dead
authored andcommitted
Use {Local}ModDefId in many queries
1 parent 40de40e commit c75fc57

File tree

26 files changed

+177
-98
lines changed

26 files changed

+177
-98
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_attr as attr;
88
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan};
99
use rustc_hir as hir;
1010
use rustc_hir::def::{CtorKind, DefKind, Res};
11-
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
11+
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
1212
use rustc_hir::intravisit::Visitor;
1313
use rustc_hir::{ItemKind, Node, PathSegment};
1414
use rustc_infer::infer::opaque_types::ConstrainOpaqueTypeRegionVisitor;
@@ -1443,7 +1443,7 @@ pub(super) fn check_type_params_are_used<'tcx>(
14431443
}
14441444
}
14451445

1446-
pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
1446+
pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
14471447
let module = tcx.hir_module_items(module_def_id);
14481448
for id in module.items() {
14491449
check_item_type(tcx, id);

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast as ast;
55
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
66
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
77
use rustc_hir as hir;
8-
use rustc_hir::def_id::{DefId, LocalDefId};
8+
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
99
use rustc_hir::lang_items::LangItem;
1010
use rustc_hir::ItemKind;
1111
use rustc_infer::infer::outlives::env::{OutlivesEnvironment, RegionBoundPairs};
@@ -1854,7 +1854,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
18541854
}
18551855
}
18561856

1857-
fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalDefId) {
1857+
fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalModDefId) {
18581858
let items = tcx.hir_module_items(module);
18591859
items.par_items(|item| tcx.ensure().check_well_formed(item.owner_id));
18601860
items.par_impl_items(|item| tcx.ensure().check_well_formed(item.owner_id));

compiler/rustc_hir_analysis/src/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_data_structures::captures::Captures;
2222
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2323
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, StashKey};
2424
use rustc_hir as hir;
25-
use rustc_hir::def_id::{DefId, LocalDefId};
25+
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
2626
use rustc_hir::intravisit::{self, Visitor};
2727
use rustc_hir::{GenericParamKind, Node};
2828
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
@@ -48,7 +48,7 @@ mod type_of;
4848
///////////////////////////////////////////////////////////////////////////
4949
// Main entry point
5050

51-
fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
51+
fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
5252
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CollectItemTypesVisitor { tcx });
5353
}
5454

compiler/rustc_hir_analysis/src/impl_wf_check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use min_specialization::check_min_specialization;
1414
use rustc_data_structures::fx::FxHashSet;
1515
use rustc_errors::struct_span_err;
1616
use rustc_hir::def::DefKind;
17-
use rustc_hir::def_id::LocalDefId;
17+
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
1818
use rustc_middle::query::Providers;
1919
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
2020
use rustc_span::{Span, Symbol};
@@ -51,7 +51,7 @@ mod min_specialization;
5151
/// impl<'a> Trait<Foo> for Bar { type X = &'a i32; }
5252
/// // ^ 'a is unused and appears in assoc type, error
5353
/// ```
54-
fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
54+
fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
5555
let min_specialization = tcx.features().min_specialization;
5656
let module = tcx.hir_module_items(module_def_id);
5757
for id in module.items() {

compiler/rustc_lint/src/late.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_ast as ast;
1919
use rustc_data_structures::stack::ensure_sufficient_stack;
2020
use rustc_data_structures::sync::join;
2121
use rustc_hir as hir;
22-
use rustc_hir::def_id::LocalDefId;
22+
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
2323
use rustc_hir::intravisit as hir_visit;
2424
use rustc_hir::intravisit::Visitor;
2525
use rustc_middle::hir::nested_filter;
@@ -338,7 +338,7 @@ crate::late_lint_methods!(impl_late_lint_pass, []);
338338

339339
pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
340340
tcx: TyCtxt<'tcx>,
341-
module_def_id: LocalDefId,
341+
module_def_id: LocalModDefId,
342342
builtin_lints: T,
343343
) {
344344
let context = LateContext {
@@ -369,7 +369,7 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
369369

370370
fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>(
371371
tcx: TyCtxt<'tcx>,
372-
module_def_id: LocalDefId,
372+
module_def_id: LocalModDefId,
373373
context: LateContext<'tcx>,
374374
pass: T,
375375
) {

compiler/rustc_lint/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ use rustc_ast as ast;
9090
use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage};
9191
use rustc_fluent_macro::fluent_messages;
9292
use rustc_hir as hir;
93-
use rustc_hir::def_id::LocalDefId;
93+
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
9494
use rustc_middle::query::Providers;
9595
use rustc_middle::ty::TyCtxt;
9696
use rustc_session::lint::builtin::{
@@ -145,7 +145,7 @@ pub fn provide(providers: &mut Providers) {
145145
*providers = Providers { lint_mod, ..*providers };
146146
}
147147

148-
fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
148+
fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
149149
late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
150150
}
151151

compiler/rustc_middle/src/hir/map/mod.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
88
use rustc_data_structures::svh::Svh;
99
use rustc_data_structures::sync::{par_for_each_in, DynSend, DynSync};
1010
use rustc_hir::def::{DefKind, Res};
11-
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
11+
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, LOCAL_CRATE};
1212
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
1313
use rustc_hir::intravisit::{self, Visitor};
1414
use rustc_hir::*;
@@ -148,7 +148,7 @@ impl<'hir> Map<'hir> {
148148
}
149149

150150
#[inline]
151-
pub fn module_items(self, module: LocalDefId) -> impl Iterator<Item = ItemId> + 'hir {
151+
pub fn module_items(self, module: LocalModDefId) -> impl Iterator<Item = ItemId> + 'hir {
152152
self.tcx.hir_module_items(module).items()
153153
}
154154

@@ -169,8 +169,8 @@ impl<'hir> Map<'hir> {
169169
}
170170

171171
#[inline]
172-
pub fn local_def_id_to_hir_id(self, def_id: LocalDefId) -> HirId {
173-
self.tcx.local_def_id_to_hir_id(def_id)
172+
pub fn local_def_id_to_hir_id(self, def_id: impl Into<LocalDefId>) -> HirId {
173+
self.tcx.local_def_id_to_hir_id(def_id.into())
174174
}
175175

176176
/// Do not call this function directly. The query should be called.
@@ -529,8 +529,8 @@ impl<'hir> Map<'hir> {
529529
self.krate_attrs().iter().any(|attr| attr.has_name(sym::rustc_coherence_is_core))
530530
}
531531

532-
pub fn get_module(self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) {
533-
let hir_id = HirId::make_owner(module);
532+
pub fn get_module(self, module: LocalModDefId) -> (&'hir Mod<'hir>, Span, HirId) {
533+
let hir_id = HirId::make_owner(module.to_local_def_id());
534534
match self.tcx.hir_owner(hir_id.owner).map(|o| o.node) {
535535
Some(OwnerNode::Item(&Item { span, kind: ItemKind::Mod(ref m), .. })) => {
536536
(m, span, hir_id)
@@ -542,7 +542,7 @@ impl<'hir> Map<'hir> {
542542

543543
/// Walks the contents of the local crate. See also `visit_all_item_likes_in_crate`.
544544
pub fn walk_toplevel_module(self, visitor: &mut impl Visitor<'hir>) {
545-
let (top_mod, span, hir_id) = self.get_module(CRATE_DEF_ID);
545+
let (top_mod, span, hir_id) = self.get_module(LocalModDefId::CRATE_DEF_ID);
546546
visitor.visit_mod(top_mod, span, hir_id);
547547
}
548548

@@ -595,7 +595,7 @@ impl<'hir> Map<'hir> {
595595

596596
/// This method is the equivalent of `visit_all_item_likes_in_crate` but restricted to
597597
/// item-likes in a single module.
598-
pub fn visit_item_likes_in_module<V>(self, module: LocalDefId, visitor: &mut V)
598+
pub fn visit_item_likes_in_module<V>(self, module: LocalModDefId, visitor: &mut V)
599599
where
600600
V: Visitor<'hir>,
601601
{
@@ -618,17 +618,19 @@ impl<'hir> Map<'hir> {
618618
}
619619
}
620620

621-
pub fn for_each_module(self, mut f: impl FnMut(LocalDefId)) {
621+
pub fn for_each_module(self, mut f: impl FnMut(LocalModDefId)) {
622622
let crate_items = self.tcx.hir_crate_items(());
623623
for module in crate_items.submodules.iter() {
624-
f(module.def_id)
624+
f(LocalModDefId::new_unchecked(module.def_id))
625625
}
626626
}
627627

628628
#[inline]
629-
pub fn par_for_each_module(self, f: impl Fn(LocalDefId) + DynSend + DynSync) {
629+
pub fn par_for_each_module(self, f: impl Fn(LocalModDefId) + DynSend + DynSync) {
630630
let crate_items = self.tcx.hir_crate_items(());
631-
par_for_each_in(&crate_items.submodules[..], |module| f(module.def_id))
631+
par_for_each_in(&crate_items.submodules[..], |module| {
632+
f(LocalModDefId::new_unchecked(module.def_id))
633+
})
632634
}
633635

634636
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
@@ -1324,7 +1326,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
13241326
}
13251327
}
13261328

1327-
pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> ModuleItems {
1329+
pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> ModuleItems {
13281330
let mut collector = ItemCollector::new(tcx, false);
13291331

13301332
let (hir_mod, span, hir_id) = tcx.hir().get_module(module_id);

compiler/rustc_middle/src/hir/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::ty::{EarlyBinder, ImplSubject, TyCtxt};
1111
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1212
use rustc_data_structures::sync::{par_for_each_in, DynSend, DynSync};
1313
use rustc_hir::def::DefKind;
14-
use rustc_hir::def_id::{DefId, LocalDefId};
14+
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
1515
use rustc_hir::*;
1616
use rustc_query_system::ich::StableHashingContext;
1717
use rustc_span::{ExpnId, DUMMY_SP};
@@ -101,22 +101,22 @@ impl<'tcx> TyCtxt<'tcx> {
101101
map::Map { tcx: self }
102102
}
103103

104-
pub fn parent_module(self, id: HirId) -> LocalDefId {
104+
pub fn parent_module(self, id: HirId) -> LocalModDefId {
105105
if !id.is_owner() && self.def_kind(id.owner) == DefKind::Mod {
106-
id.owner.def_id
106+
LocalModDefId::new_unchecked(id.owner.def_id)
107107
} else {
108108
self.parent_module_from_def_id(id.owner.def_id)
109109
}
110110
}
111111

112-
pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalDefId {
112+
pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalModDefId {
113113
while let Some(parent) = self.opt_local_parent(id) {
114114
id = parent;
115115
if self.def_kind(id) == DefKind::Mod {
116116
break;
117117
}
118118
}
119-
id
119+
LocalModDefId::new_unchecked(id)
120120
}
121121

122122
pub fn impl_subject(self, def_id: DefId) -> EarlyBinder<ImplSubject<'tcx>> {

compiler/rustc_middle/src/query/erase.rs

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ trivial! {
235235
rustc_hir::def_id::DefId,
236236
rustc_hir::def_id::DefIndex,
237237
rustc_hir::def_id::LocalDefId,
238+
rustc_hir::def_id::LocalModDefId,
238239
rustc_hir::def::DefKind,
239240
rustc_hir::Defaultness,
240241
rustc_hir::definitions::DefKey,

compiler/rustc_middle/src/query/keys.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::ty::fast_reject::SimplifiedType;
88
use crate::ty::layout::{TyAndLayout, ValidityRequirement};
99
use crate::ty::{self, Ty, TyCtxt};
1010
use crate::ty::{GenericArg, GenericArgsRef};
11-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
11+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalModDefId, ModDefId, LOCAL_CRATE};
1212
use rustc_hir::hir_id::{HirId, OwnerId};
1313
use rustc_query_system::query::{DefaultCacheSelector, SingleCacheSelector, VecCacheSelector};
1414
use rustc_span::symbol::{Ident, Symbol};
@@ -175,6 +175,41 @@ impl AsLocalKey for DefId {
175175
}
176176
}
177177

178+
impl Key for LocalModDefId {
179+
type CacheSelector = DefaultCacheSelector<Self>;
180+
181+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
182+
tcx.def_span(*self)
183+
}
184+
185+
#[inline(always)]
186+
fn key_as_def_id(&self) -> Option<DefId> {
187+
Some(self.to_def_id())
188+
}
189+
}
190+
191+
impl Key for ModDefId {
192+
type CacheSelector = DefaultCacheSelector<Self>;
193+
194+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
195+
tcx.def_span(*self)
196+
}
197+
198+
#[inline(always)]
199+
fn key_as_def_id(&self) -> Option<DefId> {
200+
Some(self.to_def_id())
201+
}
202+
}
203+
204+
impl AsLocalKey for ModDefId {
205+
type LocalKey = LocalModDefId;
206+
207+
#[inline(always)]
208+
fn as_local_key(&self) -> Option<Self::LocalKey> {
209+
self.as_local()
210+
}
211+
}
212+
178213
impl Key for SimplifiedType {
179214
type CacheSelector = DefaultCacheSelector<Self>;
180215

compiler/rustc_middle/src/query/mod.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use rustc_errors::ErrorGuaranteed;
6767
use rustc_hir as hir;
6868
use rustc_hir::def::{DefKind, DocLinkResMap};
6969
use rustc_hir::def_id::{
70-
CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LocalDefIdMap, LocalDefIdSet,
70+
CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LocalDefIdMap, LocalDefIdSet, LocalModDefId,
7171
};
7272
use rustc_hir::lang_items::{LangItem, LanguageItems};
7373
use rustc_hir::{Crate, ItemLocalId, TraitCandidate};
@@ -167,7 +167,7 @@ rustc_queries! {
167167
///
168168
/// This can be conveniently accessed by `tcx.hir().visit_item_likes_in_module`.
169169
/// Avoid calling this query directly.
170-
query hir_module_items(key: LocalDefId) -> &'tcx rustc_middle::hir::ModuleItems {
170+
query hir_module_items(key: LocalModDefId) -> &'tcx rustc_middle::hir::ModuleItems {
171171
arena_cache
172172
desc { |tcx| "getting HIR module items in `{}`", tcx.def_path_str(key) }
173173
cache_on_disk_if { true }
@@ -896,7 +896,7 @@ rustc_queries! {
896896
}
897897

898898
/// Performs lint checking for the module.
899-
query lint_mod(key: LocalDefId) -> () {
899+
query lint_mod(key: LocalModDefId) -> () {
900900
desc { |tcx| "linting {}", describe_as_module(key, tcx) }
901901
}
902902

@@ -905,35 +905,35 @@ rustc_queries! {
905905
}
906906

907907
/// Checks the attributes in the module.
908-
query check_mod_attrs(key: LocalDefId) -> () {
908+
query check_mod_attrs(key: LocalModDefId) -> () {
909909
desc { |tcx| "checking attributes in {}", describe_as_module(key, tcx) }
910910
}
911911

912912
/// Checks for uses of unstable APIs in the module.
913-
query check_mod_unstable_api_usage(key: LocalDefId) -> () {
913+
query check_mod_unstable_api_usage(key: LocalModDefId) -> () {
914914
desc { |tcx| "checking for unstable API usage in {}", describe_as_module(key, tcx) }
915915
}
916916

917917
/// Checks the const bodies in the module for illegal operations (e.g. `if` or `loop`).
918-
query check_mod_const_bodies(key: LocalDefId) -> () {
918+
query check_mod_const_bodies(key: LocalModDefId) -> () {
919919
desc { |tcx| "checking consts in {}", describe_as_module(key, tcx) }
920920
}
921921

922922
/// Checks the loops in the module.
923-
query check_mod_loops(key: LocalDefId) -> () {
923+
query check_mod_loops(key: LocalModDefId) -> () {
924924
desc { |tcx| "checking loops in {}", describe_as_module(key, tcx) }
925925
}
926926

927-
query check_mod_naked_functions(key: LocalDefId) -> () {
927+
query check_mod_naked_functions(key: LocalModDefId) -> () {
928928
desc { |tcx| "checking naked functions in {}", describe_as_module(key, tcx) }
929929
}
930930

931-
query check_mod_item_types(key: LocalDefId) -> () {
931+
query check_mod_item_types(key: LocalModDefId) -> () {
932932
desc { |tcx| "checking item types in {}", describe_as_module(key, tcx) }
933933
}
934934

935-
query check_mod_privacy(key: LocalDefId) -> () {
936-
desc { |tcx| "checking privacy in {}", describe_as_module(key, tcx) }
935+
query check_mod_privacy(key: LocalModDefId) -> () {
936+
desc { |tcx| "checking privacy in {}", describe_as_module(key.to_local_def_id(), tcx) }
937937
}
938938

939939
query check_liveness(key: LocalDefId) {
@@ -952,19 +952,19 @@ rustc_queries! {
952952
desc { "finding live symbols in crate" }
953953
}
954954

955-
query check_mod_deathness(key: LocalDefId) -> () {
955+
query check_mod_deathness(key: LocalModDefId) -> () {
956956
desc { |tcx| "checking deathness of variables in {}", describe_as_module(key, tcx) }
957957
}
958958

959-
query check_mod_impl_wf(key: LocalDefId) -> () {
959+
query check_mod_impl_wf(key: LocalModDefId) -> () {
960960
desc { |tcx| "checking that impls are well-formed in {}", describe_as_module(key, tcx) }
961961
}
962962

963-
query check_mod_type_wf(key: LocalDefId) -> () {
963+
query check_mod_type_wf(key: LocalModDefId) -> () {
964964
desc { |tcx| "checking that types are well-formed in {}", describe_as_module(key, tcx) }
965965
}
966966

967-
query collect_mod_item_types(key: LocalDefId) -> () {
967+
query collect_mod_item_types(key: LocalModDefId) -> () {
968968
desc { |tcx| "collecting item types in {}", describe_as_module(key, tcx) }
969969
}
970970

0 commit comments

Comments
 (0)