Skip to content

Commit eff9120

Browse files
authored
Rollup merge of #128875 - bvanjoi:cleanup-import-used, r=petrochenkov
rm `import.used` By the way, `import_used_map` will only be used during `build_reduced_graph` and `finalize`, so it can be split from `Resolver` in the future. r? ``@petrochenkov``
2 parents 32e0fe1 + 217ee32 commit eff9120

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
374374
root_span,
375375
root_id,
376376
vis,
377-
used: Default::default(),
378377
});
379378

380379
self.r.indeterminate_imports.push(import);
@@ -890,8 +889,10 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
890889
span: item.span,
891890
module_path: Vec::new(),
892891
vis,
893-
used: Cell::new(used.then_some(Used::Other)),
894892
});
893+
if used {
894+
self.r.import_use_map.insert(import, Used::Other);
895+
}
895896
self.r.potentially_unused_imports.push(import);
896897
let imported_binding = self.r.import(binding, import);
897898
if parent == self.r.graph_root {
@@ -1091,7 +1092,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
10911092
span,
10921093
module_path: Vec::new(),
10931094
vis: ty::Visibility::Restricted(CRATE_DEF_ID),
1094-
used: Default::default(),
10951095
})
10961096
};
10971097

@@ -1256,8 +1256,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12561256
span,
12571257
module_path: Vec::new(),
12581258
vis,
1259-
used: Cell::new(Some(Used::Other)),
12601259
});
1260+
self.r.import_use_map.insert(import, Used::Other);
12611261
let import_binding = self.r.import(binding, import);
12621262
self.r.define(self.r.graph_root, ident, MacroNS, import_binding);
12631263
} else {

compiler/rustc_resolve/src/check_unused.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,9 @@ impl Resolver<'_, '_> {
381381

382382
for import in self.potentially_unused_imports.iter() {
383383
match import.kind {
384-
_ if import.used.get().is_some()
385-
|| import.vis.is_public()
386-
|| import.span.is_dummy() =>
384+
_ if import.vis.is_public()
385+
|| import.span.is_dummy()
386+
|| self.import_use_map.contains_key(import) =>
387387
{
388388
if let ImportKind::MacroUse { .. } = import.kind {
389389
if !import.span.is_dummy() {

compiler/rustc_resolve/src/imports.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ pub(crate) struct ImportData<'a> {
176176
/// The resolution of `module_path`.
177177
pub imported_module: Cell<Option<ModuleOrUniformRoot<'a>>>,
178178
pub vis: ty::Visibility,
179-
pub used: Cell<Option<Used>>,
180179
}
181180

182181
/// All imports are unique and allocated on a same arena,
@@ -484,7 +483,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
484483
});
485484
self.record_use(target, dummy_binding, Used::Other);
486485
} else if import.imported_module.get().is_none() {
487-
import.used.set(Some(Used::Other));
486+
self.import_use_map.insert(import, Used::Other);
488487
if let Some(id) = import.id() {
489488
self.used_imports.insert(id);
490489
}
@@ -1347,7 +1346,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13471346
// module defined by a block).
13481347
// Skip if the import is public or was used through non scope-based resolution,
13491348
// e.g. through a module-relative path.
1350-
if import.used.get() == Some(Used::Other)
1349+
if self.import_use_map.get(&import) == Some(&Used::Other)
13511350
|| self.effective_visibilities.is_exported(self.local_def_id(id))
13521351
{
13531352
return false;

compiler/rustc_resolve/src/lib.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,8 @@ pub struct Resolver<'a, 'tcx> {
10161016
partial_res_map: NodeMap<PartialRes>,
10171017
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
10181018
import_res_map: NodeMap<PerNS<Option<Res>>>,
1019+
/// An import will be inserted into this map if it has been used.
1020+
import_use_map: FxHashMap<Import<'a>, Used>,
10191021
/// Resolutions for labels (node IDs of their corresponding blocks or loops).
10201022
label_res_map: NodeMap<NodeId>,
10211023
/// Resolutions for lifetimes.
@@ -1422,6 +1424,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14221424
pat_span_map: Default::default(),
14231425
partial_res_map: Default::default(),
14241426
import_res_map: Default::default(),
1427+
import_use_map: Default::default(),
14251428
label_res_map: Default::default(),
14261429
lifetimes_res_map: Default::default(),
14271430
extra_lifetime_params_map: Default::default(),
@@ -1839,7 +1842,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18391842
}
18401843

18411844
/// Test if AmbiguityError ambi is any identical to any one inside ambiguity_errors
1842-
fn matches_previous_ambiguity_error(&mut self, ambi: &AmbiguityError<'_>) -> bool {
1845+
fn matches_previous_ambiguity_error(&self, ambi: &AmbiguityError<'_>) -> bool {
18431846
for ambiguity_error in &self.ambiguity_errors {
18441847
// if the span location and ident as well as its span are the same
18451848
if ambiguity_error.kind == ambi.kind
@@ -1900,10 +1903,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
19001903
}
19011904
}
19021905
}
1903-
let old_used = import.used.get();
1904-
let new_used = Some(used);
1905-
if new_used > old_used {
1906-
import.used.set(new_used);
1906+
let old_used = self.import_use_map.entry(import).or_insert(used);
1907+
if *old_used < used {
1908+
*old_used = used;
19071909
}
19081910
if let Some(id) = import.id() {
19091911
self.used_imports.insert(id);

0 commit comments

Comments
 (0)