about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2023-06-29 07:15:27 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2023-07-07 13:17:00 +0000
commitaf9dcf70be6a7d14619275c9b2d8beebd0743b29 (patch)
treefdb90ac178f0e28879241c2a9c801263dfc84ffc
parenta3ca139defd6f4d93d8f19e7c73a5c94965cca4f (diff)
downloadrust-af9dcf70be6a7d14619275c9b2d8beebd0743b29.tar.gz
rust-af9dcf70be6a7d14619275c9b2d8beebd0743b29.zip
Remove one layer of nested matching on the same thing
-rw-r--r--compiler/rustc_ty_utils/src/opaque_types.rs94
1 files changed, 39 insertions, 55 deletions
diff --git a/compiler/rustc_ty_utils/src/opaque_types.rs b/compiler/rustc_ty_utils/src/opaque_types.rs
index 919cbcfc951..245352bfd3b 100644
--- a/compiler/rustc_ty_utils/src/opaque_types.rs
+++ b/compiler/rustc_ty_utils/src/opaque_types.rs
@@ -239,73 +239,56 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
 fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [LocalDefId] {
     let kind = tcx.def_kind(item);
     trace!(?kind);
+    let mut collector = OpaqueTypeCollector::new(tcx, item);
     match kind {
         // We're also doing this for `AssocTy` for the wf checks in `check_opaque_meets_bounds`
         DefKind::Static(_)
         | DefKind::Const
-        | DefKind::TyAlias
-        | DefKind::Fn
-        | DefKind::OpaqueTy
+        | DefKind::AssocConst
+        | DefKind::AssocFn
         | DefKind::AnonConst
         | DefKind::InlineConst
-        | DefKind::AssocFn
-        | DefKind::AssocTy
-        | DefKind::AssocConst => {
-            let mut collector = OpaqueTypeCollector::new(tcx, item);
+        | DefKind::Fn => {
             match kind {
+                // Walk over the signature of the function-like to find the opaques.
+                DefKind::AssocFn | DefKind::Fn => {
+                    let ty_sig = tcx.fn_sig(item).subst_identity();
+                    let hir_sig = tcx.hir().get_by_def_id(item).fn_sig().unwrap();
+                    // Walk over the inputs and outputs manually in order to get good spans for them.
+                    collector.visit_spanned(hir_sig.decl.output.span(), ty_sig.output());
+                    for (hir, ty) in hir_sig.decl.inputs.iter().zip(ty_sig.inputs().iter()) {
+                        collector.visit_spanned(hir.span, ty.map_bound(|x| *x));
+                    }
+                }
+                // Walk over the type of the item to find opaques.
                 DefKind::Static(_)
                 | DefKind::Const
                 | DefKind::AssocConst
-                | DefKind::AssocFn
                 | DefKind::AnonConst
-                | DefKind::InlineConst
-                | DefKind::Fn => {
-                    match kind {
-                        // Walk over the signature of the function-like to find the opaques.
-                        DefKind::AssocFn | DefKind::Fn => {
-                            let ty_sig = tcx.fn_sig(item).subst_identity();
-                            let hir_sig = tcx.hir().get_by_def_id(item).fn_sig().unwrap();
-                            // Walk over the inputs and outputs manually in order to get good spans for them.
-                            collector.visit_spanned(hir_sig.decl.output.span(), ty_sig.output());
-                            for (hir, ty) in hir_sig.decl.inputs.iter().zip(ty_sig.inputs().iter())
-                            {
-                                collector.visit_spanned(hir.span, ty.map_bound(|x| *x));
-                            }
-                        }
-                        // Walk over the type of the item to find opaques.
-                        DefKind::Static(_)
-                        | DefKind::Const
-                        | DefKind::AssocConst
-                        | DefKind::AnonConst
-                        | DefKind::InlineConst => {
-                            let span = match tcx.hir().get_by_def_id(item).ty() {
-                                Some(ty) => ty.span,
-                                _ => tcx.def_span(item),
-                            };
-                            collector.visit_spanned(span, tcx.type_of(item).subst_identity());
-                        }
-                        _ => unreachable!(),
-                    }
-                    // Look at all where bounds.
-                    tcx.predicates_of(item).instantiate_identity(tcx).visit_with(&mut collector);
-                    // An item is allowed to constrain opaques declared within its own body (but not nested within
-                    // nested functions).
-                    for id in find_taits_declared_in_body(tcx, item) {
-                        collector.opaques.extend(tcx.opaque_types_defined_by(id))
-                    }
-                }
-                DefKind::TyAlias | DefKind::AssocTy => {
-                    tcx.type_of(item).subst_identity().visit_with(&mut collector);
-                }
-                DefKind::OpaqueTy => {
-                    for (pred, span) in tcx.explicit_item_bounds(item).subst_identity_iter_copied()
-                    {
-                        collector.visit_spanned(span, pred);
-                    }
+                | DefKind::InlineConst => {
+                    let span = match tcx.hir().get_by_def_id(item).ty() {
+                        Some(ty) => ty.span,
+                        _ => tcx.def_span(item),
+                    };
+                    collector.visit_spanned(span, tcx.type_of(item).subst_identity());
                 }
                 _ => unreachable!(),
             }
-            tcx.arena.alloc_from_iter(collector.opaques)
+            // Look at all where bounds.
+            tcx.predicates_of(item).instantiate_identity(tcx).visit_with(&mut collector);
+            // An item is allowed to constrain opaques declared within its own body (but not nested within
+            // nested functions).
+            for id in find_taits_declared_in_body(tcx, item) {
+                collector.opaques.extend(tcx.opaque_types_defined_by(id))
+            }
+        }
+        DefKind::TyAlias | DefKind::AssocTy => {
+            tcx.type_of(item).subst_identity().visit_with(&mut collector);
+        }
+        DefKind::OpaqueTy => {
+            for (pred, span) in tcx.explicit_item_bounds(item).subst_identity_iter_copied() {
+                collector.visit_spanned(span, pred);
+            }
         }
         DefKind::Mod
         | DefKind::Struct
@@ -326,11 +309,12 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
         | DefKind::Field
         | DefKind::LifetimeParam
         | DefKind::GlobalAsm
-        | DefKind::Impl { .. } => &[],
+        | DefKind::Impl { .. } => {}
         DefKind::Closure | DefKind::Generator => {
-            tcx.opaque_types_defined_by(tcx.local_parent(item))
+            return tcx.opaque_types_defined_by(tcx.local_parent(item));
         }
     }
+    tcx.arena.alloc_from_iter(collector.opaques)
 }
 
 #[instrument(level = "trace", skip(tcx), ret)]