about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2022-06-08 19:26:51 -0700
committerMichael Howell <michael@notriddle.com>2022-06-08 19:26:51 -0700
commit6950f144cf83d10bc4a304b48488f9f5368cfaae (patch)
treecdb01e93a5aa5912f6ef07deef7ba8c622027f67 /src
parent7a935670055d87e17c381542f4eaab481e8bf17b (diff)
downloadrust-6950f144cf83d10bc4a304b48488f9f5368cfaae.tar.gz
rust-6950f144cf83d10bc4a304b48488f9f5368cfaae.zip
rustdoc: show tuple impls as `impl Trait for (T, ...)`
This commit adds a new unstable attribute, `#[doc(tuple_varadic)]`, that
shows a 1-tuple as `(T, ...)` instead of just `(T,)`, and links to a section
in the tuple primitive docs that talks about these.
Diffstat (limited to 'src')
-rw-r--r--src/librustdoc/clean/inline.rs6
-rw-r--r--src/librustdoc/clean/mod.rs6
-rw-r--r--src/librustdoc/clean/types.rs5
-rw-r--r--src/librustdoc/html/format.rs20
-rw-r--r--src/librustdoc/json/conversions.rs2
-rw-r--r--src/test/ui/feature-gates/feature-gate-rustdoc_internals.rs5
-rw-r--r--src/test/ui/feature-gates/feature-gate-rustdoc_internals.stderr11
7 files changed, 48 insertions, 7 deletions
diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs
index a82abe66926..d8f6a9c3ff0 100644
--- a/src/librustdoc/clean/inline.rs
+++ b/src/librustdoc/clean/inline.rs
@@ -500,7 +500,11 @@ pub(crate) fn build_impl(
             for_,
             items: trait_items,
             polarity,
-            kind: ImplKind::Normal,
+            kind: if utils::has_doc_flag(tcx, did, sym::tuple_varadic) {
+                ImplKind::TupleVaradic
+            } else {
+                ImplKind::Normal
+            },
         }),
         box merged_attrs,
         cx,
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index f3070fb35f1..b15ef424cb6 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -1999,7 +1999,11 @@ fn clean_impl<'tcx>(
             for_,
             items,
             polarity: tcx.impl_polarity(def_id),
-            kind: ImplKind::Normal,
+            kind: if utils::has_doc_flag(tcx, def_id.to_def_id(), sym::tuple_varadic) {
+                ImplKind::TupleVaradic
+            } else {
+                ImplKind::Normal
+            },
         });
         Item::from_hir_id_and_parts(hir_id, None, kind, cx)
     };
diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs
index 4605793d0df..95c84e68b83 100644
--- a/src/librustdoc/clean/types.rs
+++ b/src/librustdoc/clean/types.rs
@@ -2394,6 +2394,7 @@ impl Impl {
 pub(crate) enum ImplKind {
     Normal,
     Auto,
+    TupleVaradic,
     Blanket(Box<Type>),
 }
 
@@ -2406,6 +2407,10 @@ impl ImplKind {
         matches!(self, ImplKind::Blanket(_))
     }
 
+    pub(crate) fn is_tuple_varadic(&self) -> bool {
+        matches!(self, ImplKind::TupleVaradic)
+    }
+
     pub(crate) fn as_blanket_ty(&self) -> Option<&Type> {
         match self {
             ImplKind::Blanket(ty) => Some(ty),
diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index b7789493df6..2f433c2313b 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -714,6 +714,16 @@ fn primitive_link(
     name: &str,
     cx: &Context<'_>,
 ) -> fmt::Result {
+    primitive_link_fragment(f, prim, name, "", cx)
+}
+
+fn primitive_link_fragment(
+    f: &mut fmt::Formatter<'_>,
+    prim: clean::PrimitiveType,
+    name: &str,
+    fragment: &str,
+    cx: &Context<'_>,
+) -> fmt::Result {
     let m = &cx.cache();
     let mut needs_termination = false;
     if !f.alternate() {
@@ -723,7 +733,7 @@ fn primitive_link(
                 let len = if len == 0 { 0 } else { len - 1 };
                 write!(
                     f,
-                    "<a class=\"primitive\" href=\"{}primitive.{}.html\">",
+                    "<a class=\"primitive\" href=\"{}primitive.{}.html{fragment}\">",
                     "../".repeat(len),
                     prim.as_sym()
                 )?;
@@ -754,7 +764,7 @@ fn primitive_link(
                 };
                 if let Some(mut loc) = loc {
                     loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
-                    write!(f, "<a class=\"primitive\" href=\"{}\">", loc.finish())?;
+                    write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?;
                     needs_termination = true;
                 }
             }
@@ -1064,7 +1074,11 @@ impl clean::Impl {
                 write!(f, " for ")?;
             }
 
-            if let Some(ty) = self.kind.as_blanket_ty() {
+            if let clean::Type::Tuple(types) = &self.for_ &&
+                let [clean::Type::Generic(name)] = &types[..] &&
+                (self.kind.is_tuple_varadic() || self.kind.is_auto()) {
+                primitive_link_fragment(f, PrimitiveType::Tuple, &format!("({name}, ...)"), "#trait-implementations-1", cx)?;
+            } else if let Some(ty) = self.kind.as_blanket_ty() {
                 fmt_type(ty, f, use_absolute, cx)?;
             } else {
                 fmt_type(&self.for_, f, use_absolute, cx)?;
diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs
index 51a2abc50bc..4fde63c99d4 100644
--- a/src/librustdoc/json/conversions.rs
+++ b/src/librustdoc/json/conversions.rs
@@ -552,7 +552,7 @@ impl FromWithTcx<clean::Impl> for Impl {
         let trait_ = trait_.map(|path| clean::Type::Path { path }.into_tcx(tcx));
         // FIXME: use something like ImplKind in JSON?
         let (synthetic, blanket_impl) = match kind {
-            clean::ImplKind::Normal => (false, None),
+            clean::ImplKind::Normal | clean::ImplKind::TupleVaradic => (false, None),
             clean::ImplKind::Auto => (true, None),
             clean::ImplKind::Blanket(ty) => (false, Some(*ty)),
         };
diff --git a/src/test/ui/feature-gates/feature-gate-rustdoc_internals.rs b/src/test/ui/feature-gates/feature-gate-rustdoc_internals.rs
index d2ff4f62009..e40a3044b94 100644
--- a/src/test/ui/feature-gates/feature-gate-rustdoc_internals.rs
+++ b/src/test/ui/feature-gates/feature-gate-rustdoc_internals.rs
@@ -2,4 +2,9 @@
 /// wonderful
 mod foo {}
 
+trait Mine {}
+
+#[doc(tuple_varadic)]  //~ ERROR: `#[doc(tuple_varadic)]` is meant for internal use only
+impl<T> Mine for (T,) {}
+
 fn main() {}
diff --git a/src/test/ui/feature-gates/feature-gate-rustdoc_internals.stderr b/src/test/ui/feature-gates/feature-gate-rustdoc_internals.stderr
index e96461ac38a..00dab359e73 100644
--- a/src/test/ui/feature-gates/feature-gate-rustdoc_internals.stderr
+++ b/src/test/ui/feature-gates/feature-gate-rustdoc_internals.stderr
@@ -7,6 +7,15 @@ LL | #[doc(keyword = "match")]
    = note: see issue #90418 <https://github.com/rust-lang/rust/issues/90418> for more information
    = help: add `#![feature(rustdoc_internals)]` to the crate attributes to enable
 
-error: aborting due to previous error
+error[E0658]: `#[doc(tuple_varadic)]` is meant for internal use only
+  --> $DIR/feature-gate-rustdoc_internals.rs:7:1
+   |
+LL | #[doc(tuple_varadic)]
+   | ^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: see issue #90418 <https://github.com/rust-lang/rust/issues/90418> for more information
+   = help: add `#![feature(rustdoc_internals)]` to the crate attributes to enable
+
+error: aborting due to 2 previous errors
 
 For more information about this error, try `rustc --explain E0658`.