From e6478a372497244653c87947e0d2ed53ed03f21b Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 9 Aug 2022 15:42:59 -0300 Subject: [PATCH 1/3] Add regression test for issue 100187 --- src/test/ui/impl-trait/issue-100187.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/test/ui/impl-trait/issue-100187.rs diff --git a/src/test/ui/impl-trait/issue-100187.rs b/src/test/ui/impl-trait/issue-100187.rs new file mode 100644 index 0000000000000..fc541c6962928 --- /dev/null +++ b/src/test/ui/impl-trait/issue-100187.rs @@ -0,0 +1,12 @@ +// check-pass + +trait Trait { + type Ty; +} +impl Trait<&u8> for () { + type Ty = (); +} + +fn test<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {} + +fn main() {} From 457ff7c56cedbf67e7716d88762595ed73d75077 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 9 Aug 2022 16:33:19 -0300 Subject: [PATCH 2/3] Iterate def_ids map backwards to try first the latest mappings (it's a stack) --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 0562f7b88a3b0..46f2a43566993 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -220,7 +220,7 @@ impl ResolverAstLoweringExt for ResolverAstLowering { } fn get_remapped_def_id(&self, mut local_def_id: LocalDefId) -> LocalDefId { - for map in &self.generics_def_id_map { + for map in self.generics_def_id_map.iter().rev() { if let Some(r) = map.get(&local_def_id) { debug!("def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`"); local_def_id = *r; From 750a04ea7fabd46b6156e32b1e910a06daf8c33c Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 9 Aug 2022 16:39:02 -0300 Subject: [PATCH 3/3] Add docs for get_remapped_def_id --- compiler/rustc_ast_lowering/src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 46f2a43566993..38d30d0ffdedb 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -220,6 +220,19 @@ impl ResolverAstLoweringExt for ResolverAstLowering { } fn get_remapped_def_id(&self, mut local_def_id: LocalDefId) -> LocalDefId { + // `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we + // push new mappings so we need to try first the latest mappings, hence `iter().rev()`. + // + // Consider: + // + // `fn test<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {}` + // + // We would end with a generics_def_id_map like: + // + // `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]` + // + // for the opaque type generated on `impl Sized + 'b`, We want the result to be: + // impl_sized#'b, so iterating forward is the wrong thing to do. for map in self.generics_def_id_map.iter().rev() { if let Some(r) = map.get(&local_def_id) { debug!("def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`");