rustdoc: clean up tuple <-> primitive conversion docs
This adds a minor missing feature to `fake_variadic`, so that it can render `impl From<(T,)> for [T; 1]` correctly.
This commit is contained in:
parent
f167efad2f
commit
6df0ccf49e
4 changed files with 122 additions and 55 deletions
|
@ -951,6 +951,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
||||||
bare_fn_ty.decl.inputs.len() == 1
|
bare_fn_ty.decl.inputs.len() == 1
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
}
|
||||||
|
|| if let Some(&[hir::GenericArg::Type(ty)]) = i
|
||||||
|
.of_trait
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|trait_ref| trait_ref.path.segments.last())
|
||||||
|
.map(|last_segment| last_segment.args().args)
|
||||||
|
{
|
||||||
|
matches!(&ty.kind, hir::TyKind::Tup([_]))
|
||||||
|
} else {
|
||||||
|
false
|
||||||
};
|
};
|
||||||
if !is_valid {
|
if !is_valid {
|
||||||
self.dcx().emit_err(errors::DocFakeVariadicNotValid { span: meta.span() });
|
self.dcx().emit_err(errors::DocFakeVariadicNotValid { span: meta.span() });
|
||||||
|
|
|
@ -122,6 +122,8 @@ macro_rules! tuple_impls {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
maybe_tuple_doc! {
|
||||||
|
$($T)+ @
|
||||||
#[stable(feature = "array_tuple_conv", since = "1.71.0")]
|
#[stable(feature = "array_tuple_conv", since = "1.71.0")]
|
||||||
impl<T> From<[T; ${count($T)}]> for ($(${ignore($T)} T,)+) {
|
impl<T> From<[T; ${count($T)}]> for ($(${ignore($T)} T,)+) {
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -131,7 +133,10 @@ macro_rules! tuple_impls {
|
||||||
($($T,)+)
|
($($T,)+)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
maybe_tuple_doc! {
|
||||||
|
$($T)+ @
|
||||||
#[stable(feature = "array_tuple_conv", since = "1.71.0")]
|
#[stable(feature = "array_tuple_conv", since = "1.71.0")]
|
||||||
impl<T> From<($(${ignore($T)} T,)+)> for [T; ${count($T)}] {
|
impl<T> From<($(${ignore($T)} T,)+)> for [T; ${count($T)}] {
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -142,13 +147,14 @@ macro_rules! tuple_impls {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this is a unary tuple, it adds a doc comment.
|
// If this is a unary tuple, it adds a doc comment.
|
||||||
// Otherwise, it hides the docs entirely.
|
// Otherwise, it hides the docs entirely.
|
||||||
macro_rules! maybe_tuple_doc {
|
macro_rules! maybe_tuple_doc {
|
||||||
($a:ident @ #[$meta:meta] $item:item) => {
|
($a:ident @ #[$meta:meta] $item:item) => {
|
||||||
#[doc(fake_variadic)]
|
#[cfg_attr(not(bootstrap), doc(fake_variadic))]
|
||||||
#[doc = "This trait is implemented for tuples up to twelve items long."]
|
#[doc = "This trait is implemented for tuples up to twelve items long."]
|
||||||
#[$meta]
|
#[$meta]
|
||||||
$item
|
$item
|
||||||
|
|
|
@ -1288,11 +1288,43 @@ impl clean::Impl {
|
||||||
if self.is_negative_trait_impl() {
|
if self.is_negative_trait_impl() {
|
||||||
write!(f, "!")?;
|
write!(f, "!")?;
|
||||||
}
|
}
|
||||||
|
if self.kind.is_fake_variadic()
|
||||||
|
&& let generics = ty.generics()
|
||||||
|
&& let &[inner_type] = generics.as_ref().map_or(&[][..], |v| &v[..])
|
||||||
|
{
|
||||||
|
let last = ty.last();
|
||||||
|
if f.alternate() {
|
||||||
|
write!(f, "{}<", last)?;
|
||||||
|
self.print_type(inner_type, f, use_absolute, cx)?;
|
||||||
|
write!(f, ">")?;
|
||||||
|
} else {
|
||||||
|
write!(f, "{}<", anchor(ty.def_id(), last, cx).to_string())?;
|
||||||
|
self.print_type(inner_type, f, use_absolute, cx)?;
|
||||||
|
write!(f, ">")?;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
ty.print(cx).fmt(f)?;
|
ty.print(cx).fmt(f)?;
|
||||||
|
}
|
||||||
write!(f, " for ")?;
|
write!(f, " for ")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let clean::Type::Tuple(types) = &self.for_
|
if let Some(ty) = self.kind.as_blanket_ty() {
|
||||||
|
fmt_type(ty, f, use_absolute, cx)?;
|
||||||
|
} else {
|
||||||
|
self.print_type(&self.for_, f, use_absolute, cx)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
print_where_clause(&self.generics, cx, 0, Ending::Newline).fmt(f)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fn print_type<'a, 'tcx: 'a>(
|
||||||
|
&self,
|
||||||
|
type_: &clean::Type,
|
||||||
|
f: &mut fmt::Formatter<'_>,
|
||||||
|
use_absolute: bool,
|
||||||
|
cx: &'a Context<'tcx>,
|
||||||
|
) -> Result<(), fmt::Error> {
|
||||||
|
if let clean::Type::Tuple(types) = type_
|
||||||
&& let [clean::Type::Generic(name)] = &types[..]
|
&& let [clean::Type::Generic(name)] = &types[..]
|
||||||
&& (self.kind.is_fake_variadic() || self.kind.is_auto())
|
&& (self.kind.is_fake_variadic() || self.kind.is_auto())
|
||||||
{
|
{
|
||||||
|
@ -1305,7 +1337,13 @@ impl clean::Impl {
|
||||||
"#trait-implementations-1",
|
"#trait-implementations-1",
|
||||||
cx,
|
cx,
|
||||||
)?;
|
)?;
|
||||||
} else if let clean::BareFunction(bare_fn) = &self.for_
|
} else if let clean::Type::Array(ty, len) = type_
|
||||||
|
&& let clean::Type::Generic(name) = &**ty
|
||||||
|
&& &len[..] == "1"
|
||||||
|
&& (self.kind.is_fake_variadic() || self.kind.is_auto())
|
||||||
|
{
|
||||||
|
primitive_link(f, PrimitiveType::Array, format_args!("[{name}; N]"), cx)?;
|
||||||
|
} else if let clean::BareFunction(bare_fn) = &type_
|
||||||
&& let [clean::Argument { type_: clean::Type::Generic(name), .. }] =
|
&& let [clean::Argument { type_: clean::Type::Generic(name), .. }] =
|
||||||
&bare_fn.decl.inputs.values[..]
|
&bare_fn.decl.inputs.values[..]
|
||||||
&& (self.kind.is_fake_variadic() || self.kind.is_auto())
|
&& (self.kind.is_fake_variadic() || self.kind.is_auto())
|
||||||
|
@ -1329,14 +1367,10 @@ impl clean::Impl {
|
||||||
write!(f, " -> ")?;
|
write!(f, " -> ")?;
|
||||||
fmt_type(&bare_fn.decl.output, f, use_absolute, cx)?;
|
fmt_type(&bare_fn.decl.output, f, use_absolute, cx)?;
|
||||||
}
|
}
|
||||||
} else if let Some(ty) = self.kind.as_blanket_ty() {
|
|
||||||
fmt_type(ty, f, use_absolute, cx)?;
|
|
||||||
} else {
|
} else {
|
||||||
fmt_type(&self.for_, f, use_absolute, cx)?;
|
fmt_type(&type_, f, use_absolute, cx)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
print_where_clause(&self.generics, cx, 0, Ending::Newline).fmt(f)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,3 +16,20 @@ pub trait Bar {}
|
||||||
//@ has - '//section[@id="impl-Bar-for-(U,)"]/h3' 'impl<U: Foo> Bar for (U₁, U₂, …, Uₙ)'
|
//@ has - '//section[@id="impl-Bar-for-(U,)"]/h3' 'impl<U: Foo> Bar for (U₁, U₂, …, Uₙ)'
|
||||||
#[doc(fake_variadic)]
|
#[doc(fake_variadic)]
|
||||||
impl<U: Foo> Bar for (U,) {}
|
impl<U: Foo> Bar for (U,) {}
|
||||||
|
|
||||||
|
pub trait Baz<T> { fn baz(&self) -> T { todo!() } }
|
||||||
|
|
||||||
|
//@ has foo/trait.Baz.html
|
||||||
|
//@ has - '//section[@id="impl-Baz%3C(T,)%3E-for-%5BT;+1%5D"]/h3' 'impl<T> Baz<(T₁, T₂, …, Tₙ)> for [T; N]'
|
||||||
|
#[doc(fake_variadic)]
|
||||||
|
impl<T> Baz<(T,)> for [T; 1] {}
|
||||||
|
|
||||||
|
//@ has foo/trait.Baz.html
|
||||||
|
//@ has - '//section[@id="impl-Baz%3C%5BT;+1%5D%3E-for-(T,)"]/h3' 'impl<T> Baz<[T; N]> for (T₁, T₂, …, Tₙ)'
|
||||||
|
#[doc(fake_variadic)]
|
||||||
|
impl<T> Baz<[T; 1]> for (T,) {}
|
||||||
|
|
||||||
|
//@ has foo/trait.Baz.html
|
||||||
|
//@ has - '//section[@id="impl-Baz%3CT%3E-for-(T,)"]/h3' 'impl<T> Baz<T> for (T₁, T₂, …, Tₙ)'
|
||||||
|
#[doc(fake_variadic)]
|
||||||
|
impl<T> Baz<T> for (T,) {}
|
||||||
|
|
Loading…
Add table
Reference in a new issue