1
Fork 0

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.
This commit is contained in:
Michael Howell 2022-06-08 19:26:51 -07:00
parent 7a93567005
commit 6950f144cf
15 changed files with 111 additions and 44 deletions

View file

@ -804,6 +804,37 @@ impl CheckAttrVisitor<'_> {
true
}
fn check_doc_tuple_varadic(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
match self.tcx.hir().find(hir_id).and_then(|node| match node {
hir::Node::Item(item) => Some(&item.kind),
_ => None,
}) {
Some(ItemKind::Impl(ref i)) => {
if !matches!(&i.self_ty.kind, hir::TyKind::Tup([_])) {
self.tcx
.sess
.struct_span_err(
meta.span(),
"`#[doc(tuple_varadic)]` can only be used on unary tuples",
)
.emit();
return false;
}
}
_ => {
self.tcx
.sess
.struct_span_err(
meta.span(),
"`#[doc(keyword = \"...\")]` can only be used on impl blocks",
)
.emit();
return false;
}
}
true
}
/// Checks `#[doc(inline)]`/`#[doc(no_inline)]` attributes. Returns `true` if valid.
///
/// A doc inlining attribute is invalid if it is applied to a non-`use` item, or
@ -1064,6 +1095,13 @@ impl CheckAttrVisitor<'_> {
is_valid = false
}
sym::tuple_varadic
if !self.check_attr_not_crate_level(meta, hir_id, "tuple_varadic")
|| !self.check_doc_tuple_varadic(meta, hir_id) =>
{
is_valid = false
}
sym::html_favicon_url
| sym::html_logo_url
| sym::html_playground_url
@ -1117,7 +1155,8 @@ impl CheckAttrVisitor<'_> {
| sym::no_inline
| sym::notable_trait
| sym::passes
| sym::plugins => {}
| sym::plugins
| sym::tuple_varadic => {}
sym::test => {
if !self.check_test_attr(meta, hir_id) {