]> git.lizzy.rs Git - rust.git/commitdiff
Avoid explicitly handling res when is not needed
authorSantiago Pastorino <spastorino@gmail.com>
Wed, 3 Aug 2022 02:06:38 +0000 (23:06 -0300)
committerSantiago Pastorino <spastorino@gmail.com>
Thu, 4 Aug 2022 14:26:58 +0000 (11:26 -0300)
compiler/rustc_ast_lowering/src/item.rs
compiler/rustc_ast_lowering/src/lib.rs

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