about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSantiago Pastorino <spastorino@gmail.com>2022-08-03 23:24:13 -0300
committerSantiago Pastorino <spastorino@gmail.com>2022-08-04 11:27:00 -0300
commit12fa3393a587f17dcea70d62ca1ee66140fb3068 (patch)
treeab4d94a1818855aa9cc3700999fb16fed8b63dfe
parent4b9b5838ac219aaca2cc7dbc93cda61555130006 (diff)
downloadrust-12fa3393a587f17dcea70d62ca1ee66140fb3068.tar.gz
rust-12fa3393a587f17dcea70d62ca1ee66140fb3068.zip
Move lifetimes_in_bounds call to outside with_hir_id_owner block in lower_async_fn_ret_ty
-rw-r--r--compiler/rustc_ast_lowering/src/lib.rs19
1 files changed, 14 insertions, 5 deletions
diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs
index 901284f3c19..a82225850c4 100644
--- a/compiler/rustc_ast_lowering/src/lib.rs
+++ b/compiler/rustc_ast_lowering/src/lib.rs
@@ -1809,21 +1809,30 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
 
         debug!(?captures);
 
-        self.with_hir_id_owner(opaque_ty_node_id, |this| {
-            let lifetimes_in_bounds =
-                lifetime_collector::lifetimes_in_ret_ty(&this.resolver, output);
-            debug!(?lifetimes_in_bounds);
+        // We only want to capture the lifetimes that appear in the bounds. So visit the bounds to
+        // find out exactly which ones those are.
+        // in fn return position, like the `fn test<'a>() -> impl Debug + 'a` example,
+        // we only keep the lifetimes that appear in the `impl Debug` itself:
+        let lifetimes_to_remap = lifetime_collector::lifetimes_in_ret_ty(&self.resolver, output);
+        debug!(?lifetimes_to_remap);
 
+        self.with_hir_id_owner(opaque_ty_node_id, |this| {
+            // If this opaque type is only capturing a subset of the lifetimes (those that appear
+            // in bounds), then create the new lifetime parameters required and create a mapping
+            // from the old `'a` (on the function) to the new `'a` (on the opaque type).
             captures.extend(
                 this.create_lifetime_defs(
                     opaque_ty_def_id,
-                    &lifetimes_in_bounds,
+                    &lifetimes_to_remap,
                     &mut new_remapping,
                 )
                 .into_iter()
                 .map(|(new_node_id, lifetime)| (new_node_id, lifetime, None)),
             );
+            debug!(?captures);
+            debug!(?new_remapping);
 
+            // Install the remapping from old to new (if any):
             this.with_remapping(new_remapping, |this| {
                 // We have to be careful to get elision right here. The
                 // idea is that we create a lifetime parameter for each