1
Fork 0

Avoid explicitly handling res when is not needed

This commit is contained in:
Santiago Pastorino 2022-08-02 23:06:38 -03:00
parent cd3c388418
commit 40bcbed3c7
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
2 changed files with 46 additions and 31 deletions

View file

@ -1453,11 +1453,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
GenericParamKind::Lifetime => { GenericParamKind::Lifetime => {
let ident_span = self.lower_span(ident.span); let ident_span = self.lower_span(ident.span);
let ident = self.lower_ident(ident); let ident = self.lower_ident(ident);
let res = self.resolver.get_lifetime_res(id).unwrap_or_else(|| {
panic!("Missing resolution for lifetime {:?} at {:?}", id, ident.span)
});
let lt_id = self.next_node_id(); let lt_id = self.next_node_id();
let lifetime = self.new_named_lifetime_with_res(lt_id, ident_span, ident, res); let lifetime = self.new_named_lifetime(id, lt_id, ident_span, ident);
Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate { Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
lifetime, lifetime,
span, span,

View file

@ -1361,10 +1361,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
lctx.with_remapping(new_remapping, |lctx| { lctx.with_remapping(new_remapping, |lctx| {
let hir_bounds = lctx.lower_param_bounds(bounds, itctx); let hir_bounds = lctx.lower_param_bounds(bounds, itctx);
let lifetime_defs = let lifetime_defs = lctx.arena.alloc_from_iter(collected_lifetimes.iter().map(
lctx.arena.alloc_from_iter(collected_lifetimes.iter().map(|&(lifetime, _)| { |&(new_node_id, lifetime)| {
let hir_id = lctx.lower_node_id(lifetime.id); let hir_id = lctx.lower_node_id(new_node_id);
debug_assert_ne!(lctx.opt_local_def_id(lifetime.id), None); debug_assert_ne!(lctx.opt_local_def_id(new_node_id), None);
let (name, kind) = if lifetime.ident.name == kw::UnderscoreLifetime { let (name, kind) = if lifetime.ident.name == kw::UnderscoreLifetime {
(hir::ParamName::Fresh, hir::LifetimeParamKind::Elided) (hir::ParamName::Fresh, hir::LifetimeParamKind::Elided)
@ -1383,7 +1383,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
kind: hir::GenericParamKind::Lifetime { kind }, kind: hir::GenericParamKind::Lifetime { kind },
colon_span: None, colon_span: None,
} }
})); },
));
debug!("lower_opaque_impl_trait: lifetime_defs={:#?}", lifetime_defs); debug!("lower_opaque_impl_trait: lifetime_defs={:#?}", lifetime_defs);
@ -1405,7 +1406,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}); });
let lifetimes = let lifetimes =
self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(|(lifetime, res)| { self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(|(_, lifetime)| {
let id = self.next_node_id(); let id = self.next_node_id();
let span = lifetime.ident.span; let span = lifetime.ident.span;
@ -1415,7 +1416,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
lifetime.ident lifetime.ident
}; };
let l = self.new_named_lifetime_with_res(id, span, ident, res); let l = self.new_named_lifetime(lifetime.id, id, span, ident);
hir::GenericArg::Lifetime(l) hir::GenericArg::Lifetime(l)
})); }));
@ -1452,7 +1453,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
parent_def_id: LocalDefId, parent_def_id: LocalDefId,
lifetimes_in_bounds: &[Lifetime], lifetimes_in_bounds: &[Lifetime],
remapping: &mut FxHashMap<LocalDefId, LocalDefId>, remapping: &mut FxHashMap<LocalDefId, LocalDefId>,
) -> Vec<(Lifetime, LifetimeRes)> { ) -> Vec<(NodeId, Lifetime)> {
let mut result = Vec::new(); let mut result = Vec::new();
for lifetime in lifetimes_in_bounds { for lifetime in lifetimes_in_bounds {
@ -1471,8 +1472,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
); );
remapping.insert(old_def_id, new_def_id); remapping.insert(old_def_id, new_def_id);
let new_lifetime = Lifetime { id: node_id, ident: lifetime.ident }; result.push((node_id, *lifetime));
result.push((new_lifetime, res));
} }
} }
@ -1489,8 +1489,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
); );
remapping.insert(old_def_id, new_def_id); remapping.insert(old_def_id, new_def_id);
let new_lifetime = Lifetime { id: node_id, ident: lifetime.ident }; result.push((node_id, *lifetime));
result.push((new_lifetime, res));
} }
} }
@ -1732,8 +1731,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} }
}; };
let new_lifetime = Lifetime { id: inner_node_id, ident }; let lifetime = Lifetime { id: outer_node_id, ident };
captures.push((new_lifetime, inner_res)); captures.push((inner_node_id, lifetime, Some(inner_res)));
} }
debug!(?captures); debug!(?captures);
@ -1743,11 +1742,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
lifetime_collector::lifetimes_in_ret_ty(&this.resolver, output); lifetime_collector::lifetimes_in_ret_ty(&this.resolver, output);
debug!(?lifetimes_in_bounds); debug!(?lifetimes_in_bounds);
captures.extend(this.create_lifetime_defs( captures.extend(
opaque_ty_def_id, this.create_lifetime_defs(
&lifetimes_in_bounds, opaque_ty_def_id,
&mut new_remapping, &lifetimes_in_bounds,
)); &mut new_remapping,
)
.into_iter()
.map(|(new_node_id, lifetime)| (new_node_id, lifetime, None)),
);
this.with_remapping(new_remapping, |this| { this.with_remapping(new_remapping, |this| {
// We have to be careful to get elision right here. The // We have to be careful to get elision right here. The
@ -1761,10 +1764,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let future_bound = let future_bound =
this.lower_async_fn_output_type_to_future_bound(output, fn_def_id, span); this.lower_async_fn_output_type_to_future_bound(output, fn_def_id, span);
let generic_params = let generic_params = this.arena.alloc_from_iter(captures.iter().map(
this.arena.alloc_from_iter(captures.iter().map(|&(lifetime, _)| { |&(new_node_id, lifetime, _)| {
let hir_id = this.lower_node_id(lifetime.id); let hir_id = this.lower_node_id(new_node_id);
debug_assert_ne!(this.opt_local_def_id(lifetime.id), None); debug_assert_ne!(this.opt_local_def_id(new_node_id), None);
let (name, kind) = if lifetime.ident.name == kw::UnderscoreLifetime { let (name, kind) = if lifetime.ident.name == kw::UnderscoreLifetime {
(hir::ParamName::Fresh, hir::LifetimeParamKind::Elided) (hir::ParamName::Fresh, hir::LifetimeParamKind::Elided)
@ -1783,7 +1786,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
kind: hir::GenericParamKind::Lifetime { kind }, kind: hir::GenericParamKind::Lifetime { kind },
colon_span: None, colon_span: None,
} }
})); },
));
debug!("lower_async_fn_ret_ty: generic_params={:#?}", generic_params); debug!("lower_async_fn_ret_ty: generic_params={:#?}", generic_params);
let opaque_ty_item = hir::OpaqueTy { let opaque_ty_item = hir::OpaqueTy {
@ -1819,7 +1823,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// For the "output" lifetime parameters, we just want to // For the "output" lifetime parameters, we just want to
// generate `'_`. // generate `'_`.
let generic_args = let generic_args =
self.arena.alloc_from_iter(captures.into_iter().map(|(lifetime, res)| { self.arena.alloc_from_iter(captures.into_iter().map(|(_, lifetime, res)| {
let id = self.next_node_id(); let id = self.next_node_id();
let span = lifetime.ident.span; let span = lifetime.ident.span;
@ -1829,6 +1833,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
lifetime.ident lifetime.ident
}; };
let res = res.unwrap_or(
self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error),
);
let l = self.new_named_lifetime_with_res(id, span, ident, res); let l = self.new_named_lifetime_with_res(id, span, ident, res);
hir::GenericArg::Lifetime(l) hir::GenericArg::Lifetime(l)
})); }));
@ -1901,8 +1908,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime { fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
let span = self.lower_span(l.ident.span); let span = self.lower_span(l.ident.span);
let ident = self.lower_ident(l.ident); let ident = self.lower_ident(l.ident);
let res = self.resolver.get_lifetime_res(l.id).unwrap_or(LifetimeRes::Error); self.new_named_lifetime(l.id, l.id, span, ident)
self.new_named_lifetime_with_res(l.id, span, ident, res)
} }
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
@ -1936,6 +1942,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::Lifetime { hir_id: self.lower_node_id(id), span: self.lower_span(span), name } hir::Lifetime { hir_id: self.lower_node_id(id), span: self.lower_span(span), name }
} }
#[tracing::instrument(level = "debug", skip(self))]
fn new_named_lifetime(
&mut self,
id: NodeId,
new_id: NodeId,
span: Span,
ident: Ident,
) -> hir::Lifetime {
let res = self.resolver.get_lifetime_res(id).unwrap_or(LifetimeRes::Error);
self.new_named_lifetime_with_res(new_id, span, ident, res)
}
fn lower_generic_params_mut<'s>( fn lower_generic_params_mut<'s>(
&'s mut self, &'s mut self,
params: &'s [GenericParam], params: &'s [GenericParam],