about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTrinity Pointard <trinity.pointard@gmail.com>2021-06-15 11:47:17 +0200
committerTrinity Pointard <trinity.pointard@gmail.com>2021-06-15 11:47:17 +0200
commitaee50f417f38ce57f2491ee86fb386e7d32f241c (patch)
tree532c5c2422f1d0de6440402dd177cf4b8114606d
parent9089771daf6b1f1824446cca3306d7c18084eae0 (diff)
downloadrust-aee50f417f38ce57f2491ee86fb386e7d32f241c.tar.gz
rust-aee50f417f38ce57f2491ee86fb386e7d32f241c.zip
fix rustdoc stack overflow on mutually recursive Deref
fix #85095
-rw-r--r--src/librustdoc/html/render/mod.rs20
-rw-r--r--src/test/rustdoc/issue-85095.rs22
2 files changed, 39 insertions, 3 deletions
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index ebb4cfb7d48..7e53cf231a1 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -1960,13 +1960,19 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
                 .filter(|i| i.inner_impl().trait_.is_some())
                 .find(|i| i.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_trait_did)
             {
-                sidebar_deref_methods(cx, out, impl_, v);
+                sidebar_deref_methods(cx, out, impl_, v, FxHashSet::default());
             }
         }
     }
 }
 
-fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &Vec<Impl>) {
+fn sidebar_deref_methods(
+    cx: &Context<'_>,
+    out: &mut Buffer,
+    impl_: &Impl,
+    v: &Vec<Impl>,
+    mut already_seen: FxHashSet<DefId>,
+) {
     let c = cx.cache();
 
     debug!("found Deref: {:?}", impl_);
@@ -2032,7 +2038,15 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
                     .filter(|i| i.inner_impl().trait_.is_some())
                     .find(|i| i.inner_impl().trait_.def_id_full(c) == c.deref_trait_did)
                 {
-                    sidebar_deref_methods(cx, out, target_deref_impl, target_impls);
+                    if already_seen.insert(target_did.clone()) {
+                        sidebar_deref_methods(
+                            cx,
+                            out,
+                            target_deref_impl,
+                            target_impls,
+                            already_seen,
+                        );
+                    }
                 }
             }
         }
diff --git a/src/test/rustdoc/issue-85095.rs b/src/test/rustdoc/issue-85095.rs
new file mode 100644
index 00000000000..5c4a1da9e59
--- /dev/null
+++ b/src/test/rustdoc/issue-85095.rs
@@ -0,0 +1,22 @@
+use std::ops::Deref;
+
+pub struct A;
+pub struct B;
+
+// @has issue_85095/struct.A.html '//code' 'impl Deref for A'
+impl Deref for A {
+    type Target = B;
+
+    fn deref(&self) -> &Self::Target {
+        panic!()
+    }
+}
+
+// @has issue_85095/struct.B.html '//code' 'impl Deref for B'
+impl Deref for B {
+    type Target = A;
+
+    fn deref(&self) -> &Self::Target {
+        panic!()
+    }
+}