Skip to content

Commit 5dbca91

Browse files
spastorinocuviper
authored andcommitted
Do anonymous lifetimes remapping correctly for nested rpits
(cherry picked from commit 49ce8a2)
1 parent 2396c20 commit 5dbca91

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
533533

534534
/// Get the previously recorded `to` local def id given the `from` local def id, obtained using
535535
/// `generics_def_id_map` field.
536-
fn get_remapped_def_id(&self, mut local_def_id: LocalDefId) -> LocalDefId {
536+
fn get_remapped_def_id(&self, local_def_id: LocalDefId) -> LocalDefId {
537537
// `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we
538-
// push new mappings so we need to try first the latest mappings, hence `iter().rev()`.
538+
// push new mappings, so we first need to get the latest (innermost) mappings, hence `iter().rev()`.
539539
//
540540
// Consider:
541541
//
@@ -545,18 +545,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
545545
//
546546
// `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]`
547547
//
548-
// for the opaque type generated on `impl Sized + 'b`, We want the result to be:
549-
// impl_sized#'b, so iterating forward is the wrong thing to do.
550-
for map in self.generics_def_id_map.iter().rev() {
551-
if let Some(r) = map.get(&local_def_id) {
552-
debug!("def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`");
553-
local_def_id = *r;
554-
} else {
555-
debug!("def_id_remapper: no remapping for `{local_def_id:?}` found in map");
556-
}
557-
}
558-
559-
local_def_id
548+
// for the opaque type generated on `impl Sized + 'b`, we want the result to be: impl_sized#'b.
549+
// So, if we were trying to find first from the start (outermost) would give the wrong result, impl_trait#'b.
550+
self.generics_def_id_map
551+
.iter()
552+
.rev()
553+
.find_map(|map| map.get(&local_def_id).map(|local_def_id| *local_def_id))
554+
.unwrap_or(local_def_id)
560555
}
561556

562557
/// Freshen the `LoweringContext` and ready it to lower a nested item.
@@ -1615,7 +1610,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16151610

16161611
LifetimeRes::Fresh { param, binder: _ } => {
16171612
debug_assert_eq!(lifetime.ident.name, kw::UnderscoreLifetime);
1618-
if let Some(old_def_id) = self.opt_local_def_id(param) && remapping.get(&old_def_id).is_none() {
1613+
if let Some(old_def_id) = self.orig_opt_local_def_id(param) && remapping.get(&old_def_id).is_none() {
16191614
let node_id = self.next_node_id();
16201615

16211616
let new_def_id = self.create_def(
@@ -1884,7 +1879,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18841879
let extra_lifetime_params = self.resolver.take_extra_lifetime_params(opaque_ty_node_id);
18851880
debug!(?extra_lifetime_params);
18861881
for (ident, outer_node_id, outer_res) in extra_lifetime_params {
1887-
let outer_def_id = self.local_def_id(outer_node_id);
1882+
let outer_def_id = self.orig_local_def_id(outer_node_id);
18881883
let inner_node_id = self.next_node_id();
18891884

18901885
// Add a definition for the in scope lifetime def.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// check-pass
2+
3+
pub struct VecNumber<'s> {
4+
pub vec_number: Vec<Number<'s>>,
5+
pub auxiliary_object: &'s Vec<usize>,
6+
}
7+
8+
pub struct Number<'s> {
9+
pub number: &'s usize,
10+
}
11+
12+
impl<'s> VecNumber<'s> {
13+
pub fn vec_number_iterable_per_item_in_auxiliary_object(
14+
&self,
15+
) -> impl Iterator<Item = (&'s usize, impl Iterator<Item = &Number<'s>>)> {
16+
self.auxiliary_object.iter().map(move |n| {
17+
let iter_number = self.vec_number.iter();
18+
(n, iter_number)
19+
})
20+
}
21+
}
22+
23+
fn main() {}

0 commit comments

Comments
 (0)