Skip to content

Commit 903427b

Browse files
committed
Auto merge of #95255 - petrochenkov:suggresolve, r=michaelwoerister
resolve: Do not build expensive suggestions if they are not actually used And remove a bunch of (conditionally) unused parameters from path resolution functions. This helps with performance issues in #94857, and should be helpful in general even without that.
2 parents e70e211 + 1ad64a2 commit 903427b

25 files changed

+457
-824
lines changed

Diff for: compiler/rustc_resolve/src/build_reduced_graph.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::def_collector::collect_definitions;
99
use crate::imports::{Import, ImportKind};
1010
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
1111
use crate::Namespace::{self, MacroNS, TypeNS, ValueNS};
12-
use crate::{CrateLint, Determinacy, ExternPreludeEntry, Module, ModuleKind, ModuleOrUniformRoot};
12+
use crate::{Determinacy, ExternPreludeEntry, Finalize, Module, ModuleKind, ModuleOrUniformRoot};
1313
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError};
1414
use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError};
1515

@@ -235,16 +235,16 @@ impl<'a> AsMut<Resolver<'a>> for BuildReducedGraphVisitor<'a, '_> {
235235

236236
impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
237237
fn resolve_visibility(&mut self, vis: &ast::Visibility) -> ty::Visibility {
238-
self.resolve_visibility_speculative(vis, false).unwrap_or_else(|err| {
238+
self.try_resolve_visibility(vis, true).unwrap_or_else(|err| {
239239
self.r.report_vis_error(err);
240240
ty::Visibility::Public
241241
})
242242
}
243243

244-
fn resolve_visibility_speculative<'ast>(
244+
fn try_resolve_visibility<'ast>(
245245
&mut self,
246246
vis: &'ast ast::Visibility,
247-
speculative: bool,
247+
finalize: bool,
248248
) -> Result<ty::Visibility, VisResolutionError<'ast>> {
249249
let parent_scope = &self.parent_scope;
250250
match vis.kind {
@@ -296,13 +296,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
296296
&segments,
297297
Some(TypeNS),
298298
parent_scope,
299-
!speculative,
300-
path.span,
301-
CrateLint::SimplePath(id),
299+
if finalize { Finalize::SimplePath(id, path.span) } else { Finalize::No },
302300
) {
303301
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
304302
let res = module.res().expect("visibility resolved to unnamed block");
305-
if !speculative {
303+
if finalize {
306304
self.r.record_partial_res(id, PartialRes::new(res));
307305
}
308306
if module.is_normal() {
@@ -772,7 +770,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
772770
// correct visibilities for unnamed field placeholders specifically, so the
773771
// constructor visibility should still be determined correctly.
774772
let field_vis = self
775-
.resolve_visibility_speculative(&field.vis, true)
773+
.try_resolve_visibility(&field.vis, false)
776774
.unwrap_or(ty::Visibility::Public);
777775
if ctor_vis.is_at_least(field_vis, &*self.r) {
778776
ctor_vis = field_vis;
@@ -1131,8 +1129,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11311129
ident,
11321130
MacroNS,
11331131
&self.parent_scope,
1134-
false,
1135-
ident.span,
1132+
None,
11361133
);
11371134
if let Ok(binding) = result {
11381135
let import = macro_use_import(self, ident.span);
@@ -1272,9 +1269,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
12721269
let vis = match item.kind {
12731270
// Visibilities must not be resolved non-speculatively twice
12741271
// and we already resolved this one as a `fn` item visibility.
1275-
ItemKind::Fn(..) => self
1276-
.resolve_visibility_speculative(&item.vis, true)
1277-
.unwrap_or(ty::Visibility::Public),
1272+
ItemKind::Fn(..) => {
1273+
self.try_resolve_visibility(&item.vis, false).unwrap_or(ty::Visibility::Public)
1274+
}
12781275
_ => self.resolve_visibility(&item.vis),
12791276
};
12801277
if vis != ty::Visibility::Public {

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ use tracing::debug;
2424
use crate::imports::{Import, ImportKind, ImportResolver};
2525
use crate::path_names_to_string;
2626
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind};
27-
use crate::{
28-
BindingError, CrateLint, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot,
29-
};
30-
use crate::{NameBinding, NameBindingKind, PrivacyError, VisResolutionError};
27+
use crate::{BindingError, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot};
28+
use crate::{Finalize, NameBinding, NameBindingKind, PrivacyError, VisResolutionError};
3129
use crate::{ParentScope, PathResult, ResolutionError, Resolver, Scope, ScopeSet, Segment};
3230

3331
type Res = def::Res<ast::NodeId>;
@@ -1076,9 +1074,8 @@ impl<'a> Resolver<'a> {
10761074
ident,
10771075
ScopeSet::All(ns, false),
10781076
&parent_scope,
1077+
None,
10791078
false,
1080-
false,
1081-
ident.span,
10821079
) {
10831080
let desc = match binding.res() {
10841081
Res::Def(DefKind::Macro(MacroKind::Bang), _) => {
@@ -1405,10 +1402,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14051402
_ => return None,
14061403
}
14071404

1408-
self.make_missing_self_suggestion(span, path.clone(), parent_scope)
1409-
.or_else(|| self.make_missing_crate_suggestion(span, path.clone(), parent_scope))
1410-
.or_else(|| self.make_missing_super_suggestion(span, path.clone(), parent_scope))
1411-
.or_else(|| self.make_external_crate_suggestion(span, path, parent_scope))
1405+
self.make_missing_self_suggestion(path.clone(), parent_scope)
1406+
.or_else(|| self.make_missing_crate_suggestion(path.clone(), parent_scope))
1407+
.or_else(|| self.make_missing_super_suggestion(path.clone(), parent_scope))
1408+
.or_else(|| self.make_external_crate_suggestion(path, parent_scope))
14121409
}
14131410

14141411
/// Suggest a missing `self::` if that resolves to an correct module.
@@ -1420,13 +1417,12 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14201417
/// ```
14211418
fn make_missing_self_suggestion(
14221419
&mut self,
1423-
span: Span,
14241420
mut path: Vec<Segment>,
14251421
parent_scope: &ParentScope<'b>,
14261422
) -> Option<(Vec<Segment>, Vec<String>)> {
14271423
// Replace first ident with `self` and check if that is valid.
14281424
path[0].ident.name = kw::SelfLower;
1429-
let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No);
1425+
let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No);
14301426
debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result);
14311427
if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None }
14321428
}
@@ -1440,13 +1436,12 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14401436
/// ```
14411437
fn make_missing_crate_suggestion(
14421438
&mut self,
1443-
span: Span,
14441439
mut path: Vec<Segment>,
14451440
parent_scope: &ParentScope<'b>,
14461441
) -> Option<(Vec<Segment>, Vec<String>)> {
14471442
// Replace first ident with `crate` and check if that is valid.
14481443
path[0].ident.name = kw::Crate;
1449-
let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No);
1444+
let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No);
14501445
debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result);
14511446
if let PathResult::Module(..) = result {
14521447
Some((
@@ -1472,13 +1467,12 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14721467
/// ```
14731468
fn make_missing_super_suggestion(
14741469
&mut self,
1475-
span: Span,
14761470
mut path: Vec<Segment>,
14771471
parent_scope: &ParentScope<'b>,
14781472
) -> Option<(Vec<Segment>, Vec<String>)> {
14791473
// Replace first ident with `crate` and check if that is valid.
14801474
path[0].ident.name = kw::Super;
1481-
let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No);
1475+
let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No);
14821476
debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result);
14831477
if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None }
14841478
}
@@ -1495,7 +1489,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14951489
/// name as the first part of path.
14961490
fn make_external_crate_suggestion(
14971491
&mut self,
1498-
span: Span,
14991492
mut path: Vec<Segment>,
15001493
parent_scope: &ParentScope<'b>,
15011494
) -> Option<(Vec<Segment>, Vec<String>)> {
@@ -1513,7 +1506,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
15131506
for name in extern_crate_names.into_iter() {
15141507
// Replace first ident with a crate name and check if that is valid.
15151508
path[0].ident.name = name;
1516-
let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No);
1509+
let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No);
15171510
debug!(
15181511
"make_external_crate_suggestion: name={:?} path={:?} result={:?}",
15191512
name, path, result

0 commit comments

Comments
 (0)