From af5f7b3b6b2c7412d6215e7f2921708a86000146 Mon Sep 17 00:00:00 2001
From: Camille GILLOT <gillot.camille@gmail.com>
Date: Sat, 23 Dec 2023 18:13:41 +0000
Subject: [PATCH] Do not fetch HIR in inferred_outlives_of.

---
 .../rustc_hir_analysis/src/outlives/mod.rs    | 64 ++++++++-----------
 1 file changed, 25 insertions(+), 39 deletions(-)

diff --git a/compiler/rustc_hir_analysis/src/outlives/mod.rs b/compiler/rustc_hir_analysis/src/outlives/mod.rs
index 5d2aea7441b..72511bfa01f 100644
--- a/compiler/rustc_hir_analysis/src/outlives/mod.rs
+++ b/compiler/rustc_hir_analysis/src/outlives/mod.rs
@@ -1,5 +1,4 @@
-use hir::Node;
-use rustc_hir as hir;
+use rustc_hir::def::DefKind;
 use rustc_hir::def_id::LocalDefId;
 use rustc_middle::query::Providers;
 use rustc_middle::ty::GenericArgKind;
@@ -17,45 +16,32 @@ pub fn provide(providers: &mut Providers) {
 }
 
 fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clause<'_>, Span)] {
-    let id = tcx.local_def_id_to_hir_id(item_def_id);
-
-    if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst)
-        && tcx.features().generic_const_exprs
-    {
-        if tcx.hir().opt_const_param_default_param_def_id(id).is_some() {
-            // In `generics_of` we set the generics' parent to be our parent's parent which means that
-            // we lose out on the predicates of our actual parent if we dont return those predicates here.
-            // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
-            //
-            // struct Foo<'a, 'b, const N: usize = { ... }>(&'a &'b ());
-            //        ^^^                          ^^^^^^^ the def id we are calling
-            //        ^^^                                  inferred_outlives_of on
-            //        parent item we dont have set as the
-            //        parent of generics returned by `generics_of`
-            //
-            // In the above code we want the anon const to have predicates in its param env for `'b: 'a`
-            let item_def_id = tcx.hir().get_parent_item(id);
-            // In the above code example we would be calling `inferred_outlives_of(Foo)` here
-            return tcx.inferred_outlives_of(item_def_id);
+    match tcx.def_kind(item_def_id) {
+        DefKind::Struct | DefKind::Enum | DefKind::Union => {
+            let crate_map = tcx.inferred_outlives_crate(());
+            crate_map.predicates.get(&item_def_id.to_def_id()).copied().unwrap_or(&[])
         }
-    }
-
-    match tcx.hir_node(id) {
-        Node::Item(item) => match item.kind {
-            hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..) => {
-                let crate_map = tcx.inferred_outlives_crate(());
-
-                let predicates =
-                    crate_map.predicates.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);
-
-                debug!("inferred_outlives_of({:?}) = {:?}", item_def_id, predicates);
-
-                predicates
+        DefKind::AnonConst if tcx.features().generic_const_exprs => {
+            let id = tcx.local_def_id_to_hir_id(item_def_id);
+            if tcx.hir().opt_const_param_default_param_def_id(id).is_some() {
+                // In `generics_of` we set the generics' parent to be our parent's parent which means that
+                // we lose out on the predicates of our actual parent if we dont return those predicates here.
+                // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
+                //
+                // struct Foo<'a, 'b, const N: usize = { ... }>(&'a &'b ());
+                //        ^^^                          ^^^^^^^ the def id we are calling
+                //        ^^^                                  inferred_outlives_of on
+                //        parent item we dont have set as the
+                //        parent of generics returned by `generics_of`
+                //
+                // In the above code we want the anon const to have predicates in its param env for `'b: 'a`
+                let item_def_id = tcx.hir().get_parent_item(id);
+                // In the above code example we would be calling `inferred_outlives_of(Foo)` here
+                tcx.inferred_outlives_of(item_def_id)
+            } else {
+                &[]
             }
-
-            _ => &[],
-        },
-
+        }
         _ => &[],
     }
 }