Auto merge of #128550 - compiler-errors:shadowed-params-perf, r=petrochenkov

Only walk ribs to collect possibly shadowed params if we are adding params in our new rib

No need to collect params from parent ribs if we literally have no params to declare in this new rib.

Attempt to win back some of the perf in https://github.com/rust-lang/rust/pull/128357#issuecomment-2262677031.

Please review with whitespace *off*, the diff should be like 2 lines.

r? petrochenkov
This commit is contained in:
bors 2024-08-08 01:46:21 +00:00
commit 0d65e5a180
1 changed files with 109 additions and 100 deletions

View File

@ -2668,6 +2668,9 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
let mut function_type_rib = Rib::new(kind);
let mut function_value_rib = Rib::new(kind);
let mut function_lifetime_rib = LifetimeRib::new(lifetime_kind);
// Only check for shadowed bindings if we're declaring new params.
if !params.is_empty() {
let mut seen_bindings = FxHashMap::default();
// Store all seen lifetimes names from outer scopes.
let mut seen_lifetimes = FxHashSet::default();
@ -2675,7 +2678,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
// We also can't shadow bindings from associated parent items.
for ns in [ValueNS, TypeNS] {
for parent_rib in self.ribs[ns].iter().rev() {
seen_bindings.extend(parent_rib.bindings.keys().map(|ident| (*ident, ident.span)));
seen_bindings
.extend(parent_rib.bindings.keys().map(|ident| (*ident, ident.span)));
// Break at mod level, to account for nested items which are
// allowed to shadow generic param names.
@ -2755,7 +2759,9 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
// Plain insert (no renaming).
let (rib, def_kind) = match param.kind {
GenericParamKind::Type { .. } => (&mut function_type_rib, DefKind::TyParam),
GenericParamKind::Const { .. } => (&mut function_value_rib, DefKind::ConstParam),
GenericParamKind::Const { .. } => {
(&mut function_value_rib, DefKind::ConstParam)
}
GenericParamKind::Lifetime => {
let res = LifetimeRes::Param { param: def_id, binder };
self.record_lifetime_param(param.id, res);
@ -2765,7 +2771,9 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
};
let res = match kind {
RibKind::Item(..) | RibKind::AssocItem => Res::Def(def_kind, def_id.to_def_id()),
RibKind::Item(..) | RibKind::AssocItem => {
Res::Def(def_kind, def_id.to_def_id())
}
RibKind::Normal => {
// FIXME(non_lifetime_binders): Stop special-casing
// const params to error out here.
@ -2782,6 +2790,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
self.r.record_partial_res(param.id, PartialRes::new(res));
rib.bindings.insert(ident, res);
}
}
self.lifetime_ribs.push(function_lifetime_rib);
self.ribs[ValueNS].push(function_value_rib);