about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorStuart Cook <Zalathar@users.noreply.github.com>2025-09-18 11:48:50 +1000
committerGitHub <noreply@github.com>2025-09-18 11:48:50 +1000
commit34a805b0f5860471efd5b3acbae02146d97b37f7 (patch)
tree699117f0516ce1e577bcb066439cef02d493c061 /src
parentd5aa8d5e5eb013707a177c27d0856963df715ec7 (diff)
parent1d8971cf4e5f954c7fc9a2611dcf359a832f4c7a (diff)
downloadrust-34a805b0f5860471efd5b3acbae02146d97b37f7.tar.gz
rust-34a805b0f5860471efd5b3acbae02146d97b37f7.zip
Rollup merge of #146645 - yotamofek:pr/rustdoc/fndecl_inner_full_print, r=fmease
Cleanup `FnDecl::inner_full_print`

`inner_full_print` was pretty hard to follow IMHO.
Hopefully this cleans it up a little bit.

Also, it was checking whether `self.inputs` is empty twice, and then handling an unreachable match arm:
https://github.com/yotamofek/rust/blob/f836ae4e663b6e8938096b8559e094d18361be55/src/librustdoc/html/format.rs#L1368C1-L1368C33
`last_input_index` could only be `None` if the fn has no parameters, in which case the loop body would never run.

r? ``@GuillaumeGomez`` if you have the capacity :)

BTW, can we rename `FnDecl::inputs` to `parameters` or something? And `output` to `return_ty`?
Diffstat (limited to 'src')
-rw-r--r--src/librustdoc/display.rs6
-rw-r--r--src/librustdoc/html/format.rs107
2 files changed, 62 insertions, 51 deletions
diff --git a/src/librustdoc/display.rs b/src/librustdoc/display.rs
index aa0fad26520..db868c5c9a8 100644
--- a/src/librustdoc/display.rs
+++ b/src/librustdoc/display.rs
@@ -10,7 +10,7 @@ pub(crate) trait Joined: IntoIterator {
     ///
     /// The performance of `joined` is slightly better than `format`, since it doesn't need to use a `Cell` to keep track of whether [`fmt`](Display::fmt)
     /// was already called (`joined`'s API doesn't allow it be called more than once).
-    fn joined(self, sep: &str, f: &mut Formatter<'_>) -> fmt::Result;
+    fn joined(self, sep: impl Display, f: &mut Formatter<'_>) -> fmt::Result;
 }
 
 impl<I, T> Joined for I
@@ -18,12 +18,12 @@ where
     I: IntoIterator<Item = T>,
     T: Display,
 {
-    fn joined(self, sep: &str, f: &mut Formatter<'_>) -> fmt::Result {
+    fn joined(self, sep: impl Display, f: &mut Formatter<'_>) -> fmt::Result {
         let mut iter = self.into_iter();
         let Some(first) = iter.next() else { return Ok(()) };
         first.fmt(f)?;
         for item in iter {
-            f.write_str(sep)?;
+            sep.fmt(f)?;
             item.fmt(f)?;
         }
         Ok(())
diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index 493fdc6fb1b..8c75f301841 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -1264,6 +1264,7 @@ impl std::fmt::Write for WriteCounter {
 }
 
 // Implements Display by emitting the given number of spaces.
+#[derive(Clone, Copy)]
 struct Indent(usize);
 
 impl Display for Indent {
@@ -1275,6 +1276,37 @@ impl Display for Indent {
     }
 }
 
+impl clean::Parameter {
+    fn print(&self, cx: &Context<'_>) -> impl fmt::Display {
+        fmt::from_fn(move |f| {
+            if let Some(self_ty) = self.to_receiver() {
+                match self_ty {
+                    clean::SelfTy => f.write_str("self"),
+                    clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => {
+                        f.write_str(if f.alternate() { "&" } else { "&amp;" })?;
+                        if let Some(lt) = lifetime {
+                            write!(f, "{lt} ", lt = lt.print())?;
+                        }
+                        write!(f, "{mutability}self", mutability = mutability.print_with_space())
+                    }
+                    _ => {
+                        f.write_str("self: ")?;
+                        self_ty.print(cx).fmt(f)
+                    }
+                }
+            } else {
+                if self.is_const {
+                    write!(f, "const ")?;
+                }
+                if let Some(name) = self.name {
+                    write!(f, "{name}: ")?;
+                }
+                self.type_.print(cx).fmt(f)
+            }
+        })
+    }
+}
+
 impl clean::FnDecl {
     pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
         fmt::from_fn(move |f| {
@@ -1333,63 +1365,42 @@ impl clean::FnDecl {
         f: &mut fmt::Formatter<'_>,
         cx: &Context<'_>,
     ) -> fmt::Result {
-        let amp = if f.alternate() { "&" } else { "&amp;" };
+        f.write_char('(')?;
 
-        write!(f, "(")?;
-        if let Some(n) = line_wrapping_indent
-            && !self.inputs.is_empty()
-        {
-            write!(f, "\n{}", Indent(n + 4))?;
-        }
+        if !self.inputs.is_empty() {
+            let line_wrapping_indent = line_wrapping_indent.map(|n| Indent(n + 4));
 
-        let last_input_index = self.inputs.len().checked_sub(1);
-        for (i, param) in self.inputs.iter().enumerate() {
-            if let Some(selfty) = param.to_receiver() {
-                match selfty {
-                    clean::SelfTy => {
-                        write!(f, "self")?;
-                    }
-                    clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => {
-                        write!(f, "{amp}")?;
-                        if let Some(lt) = lifetime {
-                            write!(f, "{lt} ", lt = lt.print())?;
-                        }
-                        write!(f, "{mutability}self", mutability = mutability.print_with_space())?;
-                    }
-                    _ => {
-                        write!(f, "self: ")?;
-                        selfty.print(cx).fmt(f)?;
-                    }
-                }
-            } else {
-                if param.is_const {
-                    write!(f, "const ")?;
-                }
-                if let Some(name) = param.name {
-                    write!(f, "{name}: ")?;
+            if let Some(indent) = line_wrapping_indent {
+                write!(f, "\n{indent}")?;
+            }
+
+            let sep = fmt::from_fn(|f| {
+                if let Some(indent) = line_wrapping_indent {
+                    write!(f, ",\n{indent}")
+                } else {
+                    f.write_str(", ")
                 }
-                param.type_.print(cx).fmt(f)?;
+            });
+
+            self.inputs.iter().map(|param| param.print(cx)).joined(sep, f)?;
+
+            if line_wrapping_indent.is_some() {
+                writeln!(f, ",")?
             }
-            match (line_wrapping_indent, last_input_index) {
-                (_, None) => (),
-                (None, Some(last_i)) if i != last_i => write!(f, ", ")?,
-                (None, Some(_)) => (),
-                (Some(n), Some(last_i)) if i != last_i => write!(f, ",\n{}", Indent(n + 4))?,
-                (Some(_), Some(_)) => writeln!(f, ",")?,
+
+            if self.c_variadic {
+                match line_wrapping_indent {
+                    None => write!(f, ", ...")?,
+                    Some(indent) => writeln!(f, "{indent}...")?,
+                };
             }
         }
 
-        if self.c_variadic {
-            match line_wrapping_indent {
-                None => write!(f, ", ...")?,
-                Some(n) => writeln!(f, "{}...", Indent(n + 4))?,
-            };
+        if let Some(n) = line_wrapping_indent {
+            write!(f, "{}", Indent(n))?
         }
 
-        match line_wrapping_indent {
-            None => write!(f, ")")?,
-            Some(n) => write!(f, "{})", Indent(n))?,
-        };
+        f.write_char(')')?;
 
         self.print_output(cx).fmt(f)
     }