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

This commit is contained in:
Michael Goulet 2024-08-02 11:07:38 -04:00
parent 5367673014
commit abada5fdca

View File

@ -2667,6 +2667,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();
@ -2674,7 +2677,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.
@ -2754,7 +2758,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);
@ -2764,7 +2770,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.
@ -2781,6 +2789,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);