diff options
| author | Michael Howell <michael@notriddle.com> | 2022-06-08 19:26:51 -0700 |
|---|---|---|
| committer | Michael Howell <michael@notriddle.com> | 2022-06-08 19:26:51 -0700 |
| commit | 6950f144cf83d10bc4a304b48488f9f5368cfaae (patch) | |
| tree | cdb01e93a5aa5912f6ef07deef7ba8c622027f67 /src/librustdoc | |
| parent | 7a935670055d87e17c381542f4eaab481e8bf17b (diff) | |
| download | rust-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/librustdoc')
| -rw-r--r-- | src/librustdoc/clean/inline.rs | 6 | ||||
| -rw-r--r-- | src/librustdoc/clean/mod.rs | 6 | ||||
| -rw-r--r-- | src/librustdoc/clean/types.rs | 5 | ||||
| -rw-r--r-- | src/librustdoc/html/format.rs | 20 | ||||
| -rw-r--r-- | src/librustdoc/json/conversions.rs | 2 |
5 files changed, 33 insertions, 6 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)), }; |
