Skip to content

Commit 0749ec6

Browse files
committed
resolve: Do not build expensive suggestions if they are not actually used
Also remove a redundant parameter from `fn resolve_path(_with_ribs)`, `crate_lint: CrateLint` is a more detailed version of `record_used: bool` with `CrateLint::No` meaning `false` and anything else meaning `true`.
1 parent 63b8f01 commit 0749ec6

22 files changed

+234
-445
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
296296
&segments,
297297
Some(TypeNS),
298298
parent_scope,
299-
!speculative,
300299
path.span,
301-
CrateLint::SimplePath(id),
300+
if speculative { CrateLint::No } else { CrateLint::SimplePath(id) },
302301
) {
303302
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
304303
let res = module.res().expect("visibility resolved to unnamed block");

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14261426
) -> Option<(Vec<Segment>, Vec<String>)> {
14271427
// Replace first ident with `self` and check if that is valid.
14281428
path[0].ident.name = kw::SelfLower;
1429-
let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No);
1429+
let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No);
14301430
debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result);
14311431
if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None }
14321432
}
@@ -1446,7 +1446,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14461446
) -> Option<(Vec<Segment>, Vec<String>)> {
14471447
// Replace first ident with `crate` and check if that is valid.
14481448
path[0].ident.name = kw::Crate;
1449-
let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No);
1449+
let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No);
14501450
debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result);
14511451
if let PathResult::Module(..) = result {
14521452
Some((
@@ -1478,7 +1478,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14781478
) -> Option<(Vec<Segment>, Vec<String>)> {
14791479
// Replace first ident with `crate` and check if that is valid.
14801480
path[0].ident.name = kw::Super;
1481-
let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No);
1481+
let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No);
14821482
debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result);
14831483
if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None }
14841484
}
@@ -1513,7 +1513,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
15131513
for name in extern_crate_names.into_iter() {
15141514
// Replace first ident with a crate name and check if that is valid.
15151515
path[0].ident.name = name;
1516-
let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No);
1516+
let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No);
15171517
debug!(
15181518
"make_external_crate_suggestion: name={:?} path={:?} result={:?}",
15191519
name, path, result

compiler/rustc_resolve/src/imports.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ impl<'a> Import<'a> {
123123
_ => false,
124124
}
125125
}
126-
127-
crate fn crate_lint(&self) -> CrateLint {
128-
CrateLint::UsePath { root_id: self.root_id, root_span: self.root_span }
129-
}
130126
}
131127

132128
#[derive(Clone, Default, Debug)]
@@ -791,9 +787,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
791787
&import.module_path,
792788
None,
793789
&import.parent_scope,
794-
false,
795790
import.span,
796-
import.crate_lint(),
791+
CrateLint::No,
797792
);
798793
import.vis.set(orig_vis);
799794

@@ -887,13 +882,14 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
887882
_ => None,
888883
};
889884
let prev_ambiguity_errors_len = self.r.ambiguity_errors.len();
885+
let crate_lint =
886+
CrateLint::UsePath { root_id: import.root_id, root_span: import.root_span };
890887
let path_res = self.r.resolve_path(
891888
&import.module_path,
892889
None,
893890
&import.parent_scope,
894-
true,
895891
import.span,
896-
import.crate_lint(),
892+
crate_lint,
897893
);
898894
let no_ambiguity = self.r.ambiguity_errors.len() == prev_ambiguity_errors_len;
899895
if let Some(orig_unusable_binding) = orig_unusable_binding {
@@ -982,7 +978,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
982978
let mut full_path = import.module_path.clone();
983979
full_path.push(Segment::from_ident(Ident::empty()));
984980
self.r.lint_if_path_starts_with_module(
985-
import.crate_lint(),
981+
crate_lint,
986982
&full_path,
987983
import.span,
988984
None,
@@ -1254,7 +1250,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
12541250
self.r.per_ns(|this, ns| {
12551251
if let Ok(binding) = source_bindings[ns].get() {
12561252
this.lint_if_path_starts_with_module(
1257-
import.crate_lint(),
1253+
crate_lint,
12581254
&full_path,
12591255
import.span,
12601256
Some(binding),

compiler/rustc_resolve/src/late.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -768,15 +768,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
768768
&mut self,
769769
path: &[Segment],
770770
opt_ns: Option<Namespace>, // `None` indicates a module path in import
771-
record_used: bool,
772771
path_span: Span,
773772
crate_lint: CrateLint,
774773
) -> PathResult<'a> {
775774
self.r.resolve_path_with_ribs(
776775
path,
777776
opt_ns,
778777
&self.parent_scope,
779-
record_used,
780778
path_span,
781779
crate_lint,
782780
Some(&self.ribs),
@@ -1253,18 +1251,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
12531251
PathSource::Trait(AliasPossibility::No),
12541252
CrateLint::SimplePath(trait_ref.ref_id),
12551253
);
1256-
let res = res.base_res();
1257-
if res != Res::Err {
1258-
if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = self.resolve_path(
1259-
&path,
1260-
Some(TypeNS),
1261-
true,
1262-
trait_ref.path.span,
1263-
CrateLint::SimplePath(trait_ref.ref_id),
1264-
) {
1265-
new_id = Some(res.def_id());
1266-
new_val = Some((module, trait_ref.clone()));
1267-
}
1254+
if let Some(def_id) = res.base_res().opt_def_id() {
1255+
new_id = Some(def_id);
1256+
new_val = Some((self.r.expect_module(def_id), trait_ref.clone()));
12681257
}
12691258
}
12701259
let original_trait_ref = replace(&mut self.current_trait_ref, new_val);
@@ -2026,6 +2015,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
20262015
None
20272016
};
20282017

2018+
assert_ne!(crate_lint, CrateLint::No);
20292019
let partial_res = match self.resolve_qpath_anywhere(
20302020
id,
20312021
qself,
@@ -2060,7 +2050,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
20602050
std_path.push(Segment::from_ident(Ident::with_dummy_span(sym::std)));
20612051
std_path.extend(path);
20622052
if let PathResult::Module(_) | PathResult::NonModule(_) =
2063-
self.resolve_path(&std_path, Some(ns), false, span, CrateLint::No)
2053+
self.resolve_path(&std_path, Some(ns), span, CrateLint::No)
20642054
{
20652055
// Check if we wrote `str::from_utf8` instead of `std::str::from_utf8`
20662056
let item_span =
@@ -2228,7 +2218,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
22282218
)));
22292219
}
22302220

2231-
let result = match self.resolve_path(&path, Some(ns), true, span, crate_lint) {
2221+
let result = match self.resolve_path(&path, Some(ns), span, crate_lint) {
22322222
PathResult::NonModule(path_res) => path_res,
22332223
PathResult::Module(ModuleOrUniformRoot::Module(module)) if !module.is_normal() => {
22342224
PartialRes::new(module.res().unwrap())
@@ -2268,13 +2258,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
22682258
&& path[0].ident.name != kw::DollarCrate
22692259
{
22702260
let unqualified_result = {
2271-
match self.resolve_path(
2272-
&[*path.last().unwrap()],
2273-
Some(ns),
2274-
false,
2275-
span,
2276-
CrateLint::No,
2277-
) {
2261+
match self.resolve_path(&[*path.last().unwrap()], Some(ns), span, CrateLint::No) {
22782262
PathResult::NonModule(path_res) => path_res.base_res(),
22792263
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
22802264
module.res().unwrap()

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
188188
} else {
189189
let mod_path = &path[..path.len() - 1];
190190
let mod_prefix =
191-
match self.resolve_path(mod_path, Some(TypeNS), false, span, CrateLint::No) {
191+
match self.resolve_path(mod_path, Some(TypeNS), span, CrateLint::No) {
192192
PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(),
193193
_ => None,
194194
}
@@ -648,7 +648,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
648648
if let crate::PathSource::TraitItem(_) = source {
649649
let mod_path = &path[..path.len() - 1];
650650
if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
651-
self.resolve_path(mod_path, None, false, span, CrateLint::No)
651+
self.resolve_path(mod_path, None, span, CrateLint::No)
652652
{
653653
let resolutions = self.r.resolutions(module).borrow();
654654
let targets: Vec<_> =
@@ -1384,7 +1384,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
13841384
// Search in module.
13851385
let mod_path = &path[..path.len() - 1];
13861386
if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
1387-
self.resolve_path(mod_path, Some(TypeNS), false, span, CrateLint::No)
1387+
self.resolve_path(mod_path, Some(TypeNS), span, CrateLint::No)
13881388
{
13891389
self.r.add_module_candidates(module, &mut names, &filter_fn);
13901390
}

0 commit comments

Comments
 (0)