Skip to content

Commit 48ea37b

Browse files
authored
Rollup merge of rust-lang#72636 - marmeladema:resolver-outputs-def-id, r=petrochenkov
Cleanup `Resolver::<clone|into>_outputs` methods Follow-up cleanup work of rust-lang#72402 First commit has been split out from rust-lang#72552 r? @ecstatic-morse
2 parents 235f382 + 81f8ee4 commit 48ea37b

File tree

10 files changed

+44
-79
lines changed

10 files changed

+44
-79
lines changed

src/librustc_hir/hir.rs

-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub use rustc_ast::ast::{CaptureBy, Movability, Mutability};
1010
use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece};
1111
use rustc_ast::node_id::NodeMap;
1212
use rustc_ast::util::parser::ExprPrecedence;
13-
use rustc_data_structures::fx::FxHashSet;
1413
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
1514
use rustc_macros::HashStable_Generic;
1615
use rustc_span::source_map::{SourceMap, Spanned};
@@ -2664,10 +2663,6 @@ impl<ID> TraitCandidate<ID> {
26642663
// Trait method resolution
26652664
pub type TraitMap<ID = HirId> = NodeMap<Vec<TraitCandidate<ID>>>;
26662665

2667-
// Map from the NodeId of a glob import to a list of items which are actually
2668-
// imported.
2669-
pub type GlobMap = NodeMap<FxHashSet<Symbol>>;
2670-
26712666
#[derive(Copy, Clone, Debug, HashStable_Generic)]
26722667
pub enum Node<'hir> {
26732668
Param(&'hir Param<'hir>),

src/librustc_middle/hir/map/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ impl<'hir> Map<'hir> {
169169
})
170170
}
171171

172-
// FIXME(eddyb) this function can and should return `LocalDefId`.
173172
#[inline]
174173
pub fn local_def_id(&self, hir_id: HirId) -> LocalDefId {
175174
self.opt_local_def_id(hir_id).unwrap_or_else(|| {
@@ -192,11 +191,6 @@ impl<'hir> Map<'hir> {
192191
self.tcx.definitions.opt_local_def_id(node)
193192
}
194193

195-
#[inline]
196-
pub fn as_local_node_id(&self, def_id: DefId) -> Option<NodeId> {
197-
self.tcx.definitions.as_local_node_id(def_id)
198-
}
199-
200194
#[inline]
201195
pub fn as_local_hir_id(&self, def_id: LocalDefId) -> HirId {
202196
self.tcx.definitions.as_local_hir_id(def_id)

src/librustc_middle/query/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,9 @@ rustc_queries! {
973973
desc { "fetching what a crate is named" }
974974
}
975975
query item_children(_: DefId) -> &'tcx [Export<hir::HirId>] {}
976-
query extern_mod_stmt_cnum(_: DefId) -> Option<CrateNum> {}
976+
query extern_mod_stmt_cnum(_: LocalDefId) -> Option<CrateNum> {
977+
desc { "fetching extern module statement" }
978+
}
977979

978980
query get_lib_features(_: CrateNum) -> LibFeatures {
979981
storage(ArenaCacheSelector<'tcx>)
@@ -1040,7 +1042,7 @@ rustc_queries! {
10401042
desc { |tcx| "maybe_unused_trait_import for `{}`", tcx.def_path_str(def_id.to_def_id()) }
10411043
}
10421044
query maybe_unused_extern_crates(_: CrateNum)
1043-
-> &'tcx [(DefId, Span)] {
1045+
-> &'tcx [(LocalDefId, Span)] {
10441046
eval_always
10451047
desc { "looking up all possibly unused extern crates" }
10461048
}

src/librustc_middle/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ pub struct GlobalCtxt<'tcx> {
925925
pub consts: CommonConsts<'tcx>,
926926

927927
/// Resolutions of `extern crate` items produced by resolver.
928-
extern_crate_map: FxHashMap<DefId, CrateNum>,
928+
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
929929

930930
/// Map indicating what traits are in scope for places where this
931931
/// is relevant; generated by resolve.
@@ -944,7 +944,7 @@ pub struct GlobalCtxt<'tcx> {
944944
pub queries: query::Queries<'tcx>,
945945

946946
maybe_unused_trait_imports: FxHashSet<LocalDefId>,
947-
maybe_unused_extern_crates: Vec<(DefId, Span)>,
947+
maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
948948
/// A map of glob use to a set of names it actually imports. Currently only
949949
/// used in save-analysis.
950950
glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,

src/librustc_middle/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ mod sty;
120120
pub struct ResolverOutputs {
121121
pub definitions: rustc_hir::definitions::Definitions,
122122
pub cstore: Box<CrateStoreDyn>,
123-
pub extern_crate_map: FxHashMap<DefId, CrateNum>,
123+
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
124124
pub trait_map: FxHashMap<hir::HirId, Vec<hir::TraitCandidate<hir::HirId>>>,
125125
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
126-
pub maybe_unused_extern_crates: Vec<(DefId, Span)>,
126+
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
127127
pub export_map: ExportMap<hir::HirId>,
128128
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
129129
/// Extern prelude entries. The value is `true` if the entry was introduced

src/librustc_resolve/build_reduced_graph.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'a> Resolver<'a> {
130130
Some(def_id) => def_id,
131131
None => return self.ast_transform_scopes.get(&expn_id).unwrap_or(&self.graph_root),
132132
};
133-
if let Some(id) = self.definitions.as_local_node_id(def_id) {
133+
if let Some(id) = def_id.as_local() {
134134
self.local_macro_def_scopes[&id]
135135
} else {
136136
let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
@@ -640,9 +640,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
640640
} else if orig_name == Some(kw::SelfLower) {
641641
self.r.graph_root
642642
} else {
643+
let def_id = self.r.definitions.local_def_id(item.id);
643644
let crate_id =
644645
self.r.crate_loader.process_extern_crate(item, &self.r.definitions);
645-
self.r.extern_crate_map.insert(item.id, crate_id);
646+
self.r.extern_crate_map.insert(def_id, crate_id);
646647
self.r.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX })
647648
};
648649

@@ -1173,10 +1174,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11731174
_ => unreachable!(),
11741175
};
11751176

1176-
let def_id = self.r.definitions.local_def_id(item.id).to_def_id();
1177-
let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id);
1178-
self.r.macro_map.insert(def_id, ext);
1179-
self.r.local_macro_def_scopes.insert(item.id, parent_scope.module);
1177+
let def_id = self.r.definitions.local_def_id(item.id);
1178+
let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id.to_def_id());
1179+
self.r.macro_map.insert(def_id.to_def_id(), ext);
1180+
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
11801181

11811182
if macro_rules {
11821183
let ident = ident.normalize_to_macros_2_0();

src/librustc_resolve/check_unused.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,17 @@ impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> {
6464
fn check_import(&mut self, id: ast::NodeId) {
6565
let mut used = false;
6666
self.r.per_ns(|this, ns| used |= this.used_imports.contains(&(id, ns)));
67+
let def_id = self.r.definitions.local_def_id(id);
6768
if !used {
68-
if self.r.maybe_unused_trait_imports.contains(&id) {
69+
if self.r.maybe_unused_trait_imports.contains(&def_id) {
6970
// Check later.
7071
return;
7172
}
7273
self.unused_import(self.base_id).add(id);
7374
} else {
7475
// This trait import is definitely used, in a way other than
7576
// method resolution.
76-
self.r.maybe_unused_trait_imports.remove(&id);
77+
self.r.maybe_unused_trait_imports.remove(&def_id);
7778
if let Some(i) = self.unused_imports.get_mut(&self.base_id) {
7879
i.unused.remove(&id);
7980
}
@@ -245,7 +246,8 @@ impl Resolver<'_> {
245246
}
246247
}
247248
ImportKind::ExternCrate { .. } => {
248-
self.maybe_unused_extern_crates.push((import.id, import.span));
249+
let def_id = self.definitions.local_def_id(import.id);
250+
self.maybe_unused_extern_crates.push((def_id, import.span));
249251
}
250252
ImportKind::MacroUse => {
251253
let msg = "unused `#[macro_use]` import";

src/librustc_resolve/late.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2209,7 +2209,8 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
22092209
) -> SmallVec<[NodeId; 1]> {
22102210
let mut import_ids = smallvec![];
22112211
while let NameBindingKind::Import { import, binding, .. } = kind {
2212-
self.r.maybe_unused_trait_imports.insert(import.id);
2212+
let id = self.r.definitions.local_def_id(import.id);
2213+
self.r.maybe_unused_trait_imports.insert(id);
22132214
self.r.add_to_glob_map(&import, trait_name);
22142215
import_ids.push(import.id);
22152216
kind = &binding.kind;

src/librustc_resolve/lib.rs

+16-47
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
3737
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
3838
use rustc_hir::definitions::{DefKey, Definitions};
3939
use rustc_hir::PrimTy::{self, Bool, Char, Float, Int, Str, Uint};
40-
use rustc_hir::{GlobMap, TraitMap};
40+
use rustc_hir::TraitMap;
4141
use rustc_metadata::creader::{CStore, CrateLoader};
4242
use rustc_middle::hir::exports::ExportMap;
4343
use rustc_middle::middle::cstore::{CrateStore, MetadataLoaderDyn};
@@ -866,7 +866,7 @@ pub struct Resolver<'a> {
866866
label_res_map: NodeMap<NodeId>,
867867

868868
/// `CrateNum` resolutions of `extern crate` items.
869-
extern_crate_map: NodeMap<CrateNum>,
869+
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
870870
export_map: ExportMap<NodeId>,
871871
trait_map: TraitMap<NodeId>,
872872

@@ -895,11 +895,11 @@ pub struct Resolver<'a> {
895895
underscore_disambiguator: u32,
896896

897897
/// Maps glob imports to the names of items actually imported.
898-
glob_map: GlobMap,
898+
glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
899899

900900
used_imports: FxHashSet<(NodeId, Namespace)>,
901-
maybe_unused_trait_imports: NodeSet,
902-
maybe_unused_extern_crates: Vec<(NodeId, Span)>,
901+
maybe_unused_trait_imports: FxHashSet<LocalDefId>,
902+
maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
903903

904904
/// Privacy errors are delayed until the end in order to deduplicate them.
905905
privacy_errors: Vec<PrivacyError<'a>>,
@@ -924,7 +924,7 @@ pub struct Resolver<'a> {
924924
dummy_ext_bang: Lrc<SyntaxExtension>,
925925
dummy_ext_derive: Lrc<SyntaxExtension>,
926926
non_macro_attrs: [Lrc<SyntaxExtension>; 2],
927-
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
927+
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'a>>,
928928
ast_transform_scopes: FxHashMap<ExpnId, Module<'a>>,
929929
unused_macros: NodeMap<Span>,
930930
proc_macro_stubs: NodeSet,
@@ -1269,11 +1269,7 @@ impl<'a> Resolver<'a> {
12691269

12701270
pub fn into_outputs(self) -> ResolverOutputs {
12711271
let definitions = self.definitions;
1272-
let extern_crate_map = self
1273-
.extern_crate_map
1274-
.into_iter()
1275-
.map(|(k, v)| (definitions.local_def_id(k).to_def_id(), v))
1276-
.collect();
1272+
let extern_crate_map = self.extern_crate_map;
12771273
let export_map = self
12781274
.export_map
12791275
.into_iter()
@@ -1298,21 +1294,9 @@ impl<'a> Resolver<'a> {
12981294
)
12991295
})
13001296
.collect();
1301-
let maybe_unused_trait_imports = self
1302-
.maybe_unused_trait_imports
1303-
.into_iter()
1304-
.map(|id| definitions.local_def_id(id))
1305-
.collect();
1306-
let maybe_unused_extern_crates = self
1307-
.maybe_unused_extern_crates
1308-
.into_iter()
1309-
.map(|(id, sp)| (definitions.local_def_id(id).to_def_id(), sp))
1310-
.collect();
1311-
let glob_map = self
1312-
.glob_map
1313-
.into_iter()
1314-
.map(|(id, names)| (definitions.local_def_id(id), names))
1315-
.collect();
1297+
let maybe_unused_trait_imports = self.maybe_unused_trait_imports;
1298+
let maybe_unused_extern_crates = self.maybe_unused_extern_crates;
1299+
let glob_map = self.glob_map;
13161300
ResolverOutputs {
13171301
definitions: definitions,
13181302
cstore: Box::new(self.crate_loader.into_cstore()),
@@ -1334,11 +1318,7 @@ impl<'a> Resolver<'a> {
13341318
ResolverOutputs {
13351319
definitions: self.definitions.clone(),
13361320
cstore: Box::new(self.cstore().clone()),
1337-
extern_crate_map: self
1338-
.extern_crate_map
1339-
.iter()
1340-
.map(|(&k, &v)| (self.definitions.local_def_id(k).to_def_id(), v))
1341-
.collect(),
1321+
extern_crate_map: self.extern_crate_map.clone(),
13421322
export_map: self
13431323
.export_map
13441324
.iter()
@@ -1366,21 +1346,9 @@ impl<'a> Resolver<'a> {
13661346
)
13671347
})
13681348
.collect(),
1369-
glob_map: self
1370-
.glob_map
1371-
.iter()
1372-
.map(|(&id, names)| (self.definitions.local_def_id(id), names.clone()))
1373-
.collect(),
1374-
maybe_unused_trait_imports: self
1375-
.maybe_unused_trait_imports
1376-
.iter()
1377-
.map(|&id| self.definitions.local_def_id(id))
1378-
.collect(),
1379-
maybe_unused_extern_crates: self
1380-
.maybe_unused_extern_crates
1381-
.iter()
1382-
.map(|&(id, sp)| (self.definitions.local_def_id(id).to_def_id(), sp))
1383-
.collect(),
1349+
glob_map: self.glob_map.clone(),
1350+
maybe_unused_trait_imports: self.maybe_unused_trait_imports.clone(),
1351+
maybe_unused_extern_crates: self.maybe_unused_extern_crates.clone(),
13841352
extern_prelude: self
13851353
.extern_prelude
13861354
.iter()
@@ -1522,7 +1490,8 @@ impl<'a> Resolver<'a> {
15221490
#[inline]
15231491
fn add_to_glob_map(&mut self, import: &Import<'_>, ident: Ident) {
15241492
if import.is_glob() {
1525-
self.glob_map.entry(import.id).or_default().insert(ident.name);
1493+
let def_id = self.definitions.local_def_id(import.id);
1494+
self.glob_map.entry(def_id).or_default().insert(ident.name);
15261495
}
15271496
}
15281497

src/librustc_typeck/check_unused.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_data_structures::fx::FxHashMap;
22
use rustc_errors::Applicability;
33
use rustc_hir as hir;
4-
use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE};
4+
use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LOCAL_CRATE};
55
use rustc_hir::itemlikevisit::ItemLikeVisitor;
66
use rustc_middle::ty::TyCtxt;
77
use rustc_session::lint;
@@ -70,7 +70,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
7070
// Collect first the crates that are completely unused. These we
7171
// can always suggest removing (no matter which edition we are
7272
// in).
73-
let unused_extern_crates: FxHashMap<DefId, Span> = tcx
73+
let unused_extern_crates: FxHashMap<LocalDefId, Span> = tcx
7474
.maybe_unused_extern_crates(LOCAL_CRATE)
7575
.iter()
7676
.filter(|&&(def_id, _)| {
@@ -88,7 +88,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
8888
// Note that if we carry through to the `extern_mod_stmt_cnum` query
8989
// below it'll cause a panic because `def_id` is actually bogus at this
9090
// point in time otherwise.
91-
if tcx.hir().find(tcx.hir().as_local_hir_id(def_id.expect_local())).is_none() {
91+
if tcx.hir().find(tcx.hir().as_local_hir_id(def_id)).is_none() {
9292
return false;
9393
}
9494
true
@@ -112,13 +112,14 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
112112
});
113113

114114
for extern_crate in &crates_to_lint {
115-
let id = tcx.hir().as_local_hir_id(extern_crate.def_id.expect_local());
115+
let def_id = extern_crate.def_id.expect_local();
116+
let id = tcx.hir().as_local_hir_id(def_id);
116117
let item = tcx.hir().expect_item(id);
117118

118119
// If the crate is fully unused, we suggest removing it altogether.
119120
// We do this in any edition.
120121
if extern_crate.warn_if_unused {
121-
if let Some(&span) = unused_extern_crates.get(&extern_crate.def_id) {
122+
if let Some(&span) = unused_extern_crates.get(&def_id) {
122123
tcx.struct_span_lint_hir(lint, id, span, |lint| {
123124
// Removal suggestion span needs to include attributes (Issue #54400)
124125
let span_with_attrs = tcx

0 commit comments

Comments
 (0)