Skip lifetimes in binders when visiting

This commit is contained in:
Santiago Pastorino 2022-08-01 16:03:49 -03:00
parent 4f334f2b97
commit 05b989e16e
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
3 changed files with 71 additions and 73 deletions

View File

@ -1353,12 +1353,11 @@ fn lower_opaque_impl_trait(
}),
);
let (lifetimes_in_bounds, binders_to_ignore) =
lifetime_collector::lifetimes_in_bounds(bounds);
let lifetimes_in_bounds =
lifetime_collector::lifetimes_in_bounds(&lctx.resolver, bounds);
debug!(?lifetimes_in_bounds);
debug!(?binders_to_ignore);
lctx.create_and_capture_lifetime_defs(&lifetimes_in_bounds, &binders_to_ignore);
lctx.create_and_capture_lifetime_defs(&lifetimes_in_bounds);
let ret = lctx.lower_param_bounds(bounds, itctx);
@ -1447,11 +1446,7 @@ fn generate_opaque_type(
hir::OwnerNode::Item(self.arena.alloc(opaque_ty_item))
}
fn create_and_capture_lifetime_defs(
&mut self,
lifetimes_in_bounds: &[&Lifetime],
binders_to_ignore: &FxHashMap<NodeId, Vec<NodeId>>,
) {
fn create_and_capture_lifetime_defs(&mut self, lifetimes_in_bounds: &[&Lifetime]) {
for lifetime in lifetimes_in_bounds {
let ident = lifetime.ident;
let span = ident.span;
@ -1461,53 +1456,41 @@ fn create_and_capture_lifetime_defs(
if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
match res {
LifetimeRes::Param { param, binder } => {
if !binders_to_ignore
.get(&lifetime.id)
.unwrap_or(&Vec::new())
.contains(&binder)
{
match captured_lifetimes.captures.entry(param) {
Entry::Occupied(_) => {}
Entry::Vacant(v) => {
let node_id = self.next_node_id();
let name = ParamName::Plain(ident);
LifetimeRes::Param { param, binder: _ } => {
match captured_lifetimes.captures.entry(param) {
Entry::Occupied(_) => {}
Entry::Vacant(v) => {
let node_id = self.next_node_id();
let name = ParamName::Plain(ident);
self.create_def(
captured_lifetimes.parent_def_id,
node_id,
DefPathData::LifetimeNs(name.ident().name),
);
self.create_def(
captured_lifetimes.parent_def_id,
node_id,
DefPathData::LifetimeNs(name.ident().name),
);
v.insert((span, node_id, name, res));
}
v.insert((span, node_id, name, res));
}
}
}
LifetimeRes::Fresh { param, binder } => {
LifetimeRes::Fresh { param, binder: _ } => {
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
if !binders_to_ignore
.get(&lifetime.id)
.unwrap_or(&Vec::new())
.contains(&binder)
{
let param = self.local_def_id(param);
match captured_lifetimes.captures.entry(param) {
Entry::Occupied(_) => {}
Entry::Vacant(v) => {
let node_id = self.next_node_id();
let param = self.local_def_id(param);
match captured_lifetimes.captures.entry(param) {
Entry::Occupied(_) => {}
Entry::Vacant(v) => {
let node_id = self.next_node_id();
let name = ParamName::Fresh;
let name = ParamName::Fresh;
self.create_def(
captured_lifetimes.parent_def_id,
node_id,
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
);
self.create_def(
captured_lifetimes.parent_def_id,
node_id,
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
);
v.insert((span, node_id, name, res));
}
v.insert((span, node_id, name, res));
}
}
}
@ -1758,12 +1741,11 @@ fn lower_async_fn_ret_ty(
}),
);
let (lifetimes_in_bounds, binders_to_ignore) =
lifetime_collector::lifetimes_in_ret_ty(output);
let lifetimes_in_bounds =
lifetime_collector::lifetimes_in_ret_ty(&this.resolver, output);
debug!(?lifetimes_in_bounds);
debug!(?binders_to_ignore);
this.create_and_capture_lifetime_defs(&lifetimes_in_bounds, &binders_to_ignore);
this.create_and_capture_lifetime_defs(&lifetimes_in_bounds);
// We have to be careful to get elision right here. The
// idea is that we create a lifetime parameter for each

View File

@ -1,21 +1,32 @@
use super::ResolverAstLoweringExt;
use rustc_ast::visit::{self, BoundKind, LifetimeCtxt, Visitor};
use rustc_ast::{
FnRetTy, GenericBounds, Lifetime, NodeId, PolyTraitRef, TraitBoundModifier, Ty, TyKind,
};
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::LifetimeRes;
use rustc_middle::ty::ResolverAstLowering;
struct LifetimeCollectVisitor<'ast> {
struct LifetimeCollectVisitor<'this, 'ast: 'this> {
resolver: &'this ResolverAstLowering,
current_binders: Vec<NodeId>,
binders_to_ignore: FxHashMap<NodeId, Vec<NodeId>>,
collected_lifetimes: Vec<&'ast Lifetime>,
}
impl<'ast> Visitor<'ast> for LifetimeCollectVisitor<'ast> {
impl<'this, 'ast: 'this> LifetimeCollectVisitor<'this, 'ast> {
fn new(resolver: &'this ResolverAstLowering) -> Self {
Self { resolver, current_binders: Vec::new(), collected_lifetimes: Vec::new() }
}
}
impl<'this, 'ast: 'this> Visitor<'ast> for LifetimeCollectVisitor<'this, 'ast> {
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) {
if !self.collected_lifetimes.contains(&lifetime) {
self.collected_lifetimes.push(lifetime);
let res = self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error);
if res.binder().map_or(true, |b| !self.current_binders.contains(&b)) {
if !self.collected_lifetimes.contains(&lifetime) {
self.collected_lifetimes.push(lifetime);
}
}
self.binders_to_ignore.insert(lifetime.id, self.current_binders.clone());
}
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
@ -37,26 +48,22 @@ fn visit_ty(&mut self, t: &'ast Ty) {
}
}
pub fn lifetimes_in_ret_ty(ret_ty: &FnRetTy) -> (Vec<&Lifetime>, FxHashMap<NodeId, Vec<NodeId>>) {
let mut visitor = LifetimeCollectVisitor {
current_binders: Vec::new(),
binders_to_ignore: FxHashMap::default(),
collected_lifetimes: Vec::new(),
};
pub fn lifetimes_in_ret_ty<'this, 'ast: 'this>(
resolver: &'this ResolverAstLowering,
ret_ty: &'ast FnRetTy,
) -> Vec<&'ast Lifetime> {
let mut visitor = LifetimeCollectVisitor::new(resolver);
visitor.visit_fn_ret_ty(ret_ty);
(visitor.collected_lifetimes, visitor.binders_to_ignore)
visitor.collected_lifetimes
}
pub fn lifetimes_in_bounds(
bounds: &GenericBounds,
) -> (Vec<&Lifetime>, FxHashMap<NodeId, Vec<NodeId>>) {
let mut visitor = LifetimeCollectVisitor {
current_binders: Vec::new(),
binders_to_ignore: FxHashMap::default(),
collected_lifetimes: Vec::new(),
};
pub fn lifetimes_in_bounds<'this, 'ast: 'this>(
resolver: &'this ResolverAstLowering,
bounds: &'ast GenericBounds,
) -> Vec<&'ast Lifetime> {
let mut visitor = LifetimeCollectVisitor::new(resolver);
for bound in bounds {
visitor.visit_param_bound(bound, BoundKind::Bound);
}
(visitor.collected_lifetimes, visitor.binders_to_ignore)
visitor.collected_lifetimes
}

View File

@ -747,3 +747,12 @@ pub enum LifetimeRes {
/// HACK: This is used to recover the NodeId of an elided lifetime.
ElidedAnchor { start: NodeId, end: NodeId },
}
impl LifetimeRes {
pub fn binder(&self) -> Option<NodeId> {
match self {
LifetimeRes::Param { binder, .. } | LifetimeRes::Fresh { binder, .. } => Some(*binder),
_ => None,
}
}
}