Skip to content

Commit 1169832

Browse files
committed
rustdoc: extend #[doc(tuple_variadic)] to fn pointers
The attribute is also renamed `fake_variadic`.
1 parent 263edd4 commit 1169832

File tree

21 files changed

+61
-42
lines changed

21 files changed

+61
-42
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
402402
gate_feature_post!(self, rustdoc_internals, attr.span, msg);
403403
}
404404

405-
if nested_meta.has_name(sym::tuple_variadic) {
406-
let msg = "`#[doc(tuple_variadic)]` is meant for internal use only";
405+
if nested_meta.has_name(sym::fake_variadic) {
406+
let msg = "`#[doc(fake_variadic)]` is meant for internal use only";
407407
gate_feature_post!(self, rustdoc_internals, attr.span, msg);
408408
}
409409
}

compiler/rustc_error_messages/locales/en-US/passes.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ passes-doc-keyword-not-mod = `#[doc(keyword = "...")]` should be used on modules
8181
8282
passes-doc-keyword-invalid-ident = `{$doc_keyword}` is not a valid identifier
8383
84-
passes-doc-tuple-variadic-not-first =
85-
`#[doc(tuple_variadic)]` must be used on the first of a set of tuple trait impls with varying arity
84+
passes-doc-fake-variadic-not-valid =
85+
`#[doc(fake_variadic)]` must be used on the first of a set of tuple or fn pointer trait impls with varying arity
8686
8787
passes-doc-keyword-only-impl = `#[doc(keyword = "...")]` should be used on impl blocks
8888

compiler/rustc_passes/src/check_attr.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -706,14 +706,20 @@ impl CheckAttrVisitor<'_> {
706706
true
707707
}
708708

709-
fn check_doc_tuple_variadic(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
709+
fn check_doc_fake_variadic(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
710710
match self.tcx.hir().find(hir_id).and_then(|node| match node {
711711
hir::Node::Item(item) => Some(&item.kind),
712712
_ => None,
713713
}) {
714714
Some(ItemKind::Impl(ref i)) => {
715-
if !matches!(&i.self_ty.kind, hir::TyKind::Tup([_])) {
716-
self.tcx.sess.emit_err(errors::DocTupleVariadicNotFirst { span: meta.span() });
715+
let is_valid = matches!(&i.self_ty.kind, hir::TyKind::Tup([_]))
716+
|| if let hir::TyKind::BareFn(bare_fn_ty) = &i.self_ty.kind {
717+
bare_fn_ty.decl.inputs.len() == 1
718+
} else {
719+
false
720+
};
721+
if !is_valid {
722+
self.tcx.sess.emit_err(errors::DocFakeVariadicNotValid { span: meta.span() });
717723
return false;
718724
}
719725
}
@@ -887,9 +893,9 @@ impl CheckAttrVisitor<'_> {
887893
is_valid = false
888894
}
889895

890-
sym::tuple_variadic
891-
if !self.check_attr_not_crate_level(meta, hir_id, "tuple_variadic")
892-
|| !self.check_doc_tuple_variadic(meta, hir_id) =>
896+
sym::fake_variadic
897+
if !self.check_attr_not_crate_level(meta, hir_id, "fake_variadic")
898+
|| !self.check_doc_fake_variadic(meta, hir_id) =>
893899
{
894900
is_valid = false
895901
}
@@ -939,7 +945,7 @@ impl CheckAttrVisitor<'_> {
939945
| sym::notable_trait
940946
| sym::passes
941947
| sym::plugins
942-
| sym::tuple_variadic => {}
948+
| sym::fake_variadic => {}
943949

944950
sym::test => {
945951
if !self.check_test_attr(meta, hir_id) {

compiler/rustc_passes/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ pub struct DocKeywordInvalidIdent {
212212
}
213213

214214
#[derive(SessionDiagnostic)]
215-
#[error(passes::doc_tuple_variadic_not_first)]
216-
pub struct DocTupleVariadicNotFirst {
215+
#[error(passes::doc_fake_variadic_not_valid)]
216+
pub struct DocFakeVariadicNotValid {
217217
#[primary_span]
218218
pub span: Span,
219219
}

compiler/rustc_span/src/symbol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ symbols! {
685685
fabsf32,
686686
fabsf64,
687687
fadd_fast,
688+
fake_variadic,
688689
fdiv_fast,
689690
feature,
690691
fence,
@@ -1460,7 +1461,6 @@ symbols! {
14601461
tuple,
14611462
tuple_from_req,
14621463
tuple_indexing,
1463-
tuple_variadic,
14641464
two_phase,
14651465
ty,
14661466
type_alias_enum_variants,

library/core/src/fmt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2562,7 +2562,7 @@ macro_rules! tuple {
25622562

25632563
macro_rules! maybe_tuple_doc {
25642564
($a:ident @ #[$meta:meta] $item:item) => {
2565-
#[doc(tuple_variadic)]
2565+
#[cfg_attr(not(bootstrap), doc(fake_variadic))]
25662566
#[doc = "This trait is implemented for tuples up to twelve items long."]
25672567
#[$meta]
25682568
$item

library/core/src/hash/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ mod impls {
900900

901901
macro_rules! maybe_tuple_doc {
902902
($a:ident @ #[$meta:meta] $item:item) => {
903-
#[doc(tuple_variadic)]
903+
#[cfg_attr(not(bootstrap), doc(fake_variadic))]
904904
#[doc = "This trait is implemented for tuples up to twelve items long."]
905905
#[$meta]
906906
$item

library/core/src/primitive_docs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ impl<T> (T,) {}
996996
// Fake impl that's only really used for docs.
997997
#[cfg(doc)]
998998
#[stable(feature = "rust1", since = "1.0.0")]
999-
#[doc(tuple_variadic)]
999+
#[cfg_attr(not(bootstrap), doc(fake_variadic))]
10001000
/// This trait is implemented on arbitrary-length tuples.
10011001
impl<T: Clone> Clone for (T,) {
10021002
fn clone(&self) -> Self {
@@ -1007,7 +1007,7 @@ impl<T: Clone> Clone for (T,) {
10071007
// Fake impl that's only really used for docs.
10081008
#[cfg(doc)]
10091009
#[stable(feature = "rust1", since = "1.0.0")]
1010-
#[doc(tuple_variadic)]
1010+
#[cfg_attr(not(bootstrap), doc(fake_variadic))]
10111011
/// This trait is implemented on arbitrary-length tuples.
10121012
impl<T: Copy> Copy for (T,) {
10131013
// empty

library/core/src/tuple.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ macro_rules! tuple_impls {
107107
// Otherwise, it hides the docs entirely.
108108
macro_rules! maybe_tuple_doc {
109109
($a:ident @ #[$meta:meta] $item:item) => {
110-
#[doc(tuple_variadic)]
110+
#[cfg_attr(not(bootstrap), doc(fake_variadic))]
111111
#[doc = "This trait is implemented for tuples up to twelve items long."]
112112
#[$meta]
113113
$item

library/std/src/primitive_docs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ impl<T> (T,) {}
996996
// Fake impl that's only really used for docs.
997997
#[cfg(doc)]
998998
#[stable(feature = "rust1", since = "1.0.0")]
999-
#[doc(tuple_variadic)]
999+
#[cfg_attr(not(bootstrap), doc(fake_variadic))]
10001000
/// This trait is implemented on arbitrary-length tuples.
10011001
impl<T: Clone> Clone for (T,) {
10021002
fn clone(&self) -> Self {
@@ -1007,7 +1007,7 @@ impl<T: Clone> Clone for (T,) {
10071007
// Fake impl that's only really used for docs.
10081008
#[cfg(doc)]
10091009
#[stable(feature = "rust1", since = "1.0.0")]
1010-
#[doc(tuple_variadic)]
1010+
#[cfg_attr(not(bootstrap), doc(fake_variadic))]
10111011
/// This trait is implemented on arbitrary-length tuples.
10121012
impl<T: Copy> Copy for (T,) {
10131013
// empty

src/librustdoc/clean/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,8 @@ pub(crate) fn build_impl(
506506
for_,
507507
items: trait_items,
508508
polarity,
509-
kind: if utils::has_doc_flag(tcx, did, sym::tuple_variadic) {
510-
ImplKind::TupleVaradic
509+
kind: if utils::has_doc_flag(tcx, did, sym::fake_variadic) {
510+
ImplKind::FakeVaradic
511511
} else {
512512
ImplKind::Normal
513513
},

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2005,8 +2005,8 @@ fn clean_impl<'tcx>(
20052005
for_,
20062006
items,
20072007
polarity: tcx.impl_polarity(def_id),
2008-
kind: if utils::has_doc_flag(tcx, def_id.to_def_id(), sym::tuple_variadic) {
2009-
ImplKind::TupleVaradic
2008+
kind: if utils::has_doc_flag(tcx, def_id.to_def_id(), sym::fake_variadic) {
2009+
ImplKind::FakeVaradic
20102010
} else {
20112011
ImplKind::Normal
20122012
},

src/librustdoc/clean/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,7 @@ impl Impl {
23942394
pub(crate) enum ImplKind {
23952395
Normal,
23962396
Auto,
2397-
TupleVaradic,
2397+
FakeVaradic,
23982398
Blanket(Box<Type>),
23992399
}
24002400

@@ -2407,8 +2407,8 @@ impl ImplKind {
24072407
matches!(self, ImplKind::Blanket(_))
24082408
}
24092409

2410-
pub(crate) fn is_tuple_variadic(&self) -> bool {
2411-
matches!(self, ImplKind::TupleVaradic)
2410+
pub(crate) fn is_fake_variadic(&self) -> bool {
2411+
matches!(self, ImplKind::FakeVaradic)
24122412
}
24132413

24142414
pub(crate) fn as_blanket_ty(&self) -> Option<&Type> {

src/librustdoc/html/format.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1165,10 +1165,23 @@ impl clean::Impl {
11651165

11661166
if let clean::Type::Tuple(types) = &self.for_ &&
11671167
let [clean::Type::Generic(name)] = &types[..] &&
1168-
(self.kind.is_tuple_variadic() || self.kind.is_auto()) {
1168+
(self.kind.is_fake_variadic() || self.kind.is_auto()) {
11691169
// Hardcoded anchor library/core/src/primitive_docs.rs
11701170
// Link should match `# Trait implementations`
11711171
primitive_link_fragment(f, PrimitiveType::Tuple, &format!("({name}₁, {name}₂, …, {name}ₙ)"), "#trait-implementations-1", cx)?;
1172+
} else if let clean::Type::BareFunction(bare_fn) = &self.for_ &&
1173+
let [clean::Argument { type_: clean::Type::Generic(name), .. }] = &bare_fn.decl.inputs.values[..] &&
1174+
(self.kind.is_fake_variadic() || self.kind.is_auto()) {
1175+
// Hardcoded anchor library/core/src/primitive_docs.rs
1176+
// Link should match `# Trait implementations`
1177+
primitive_link_fragment(f, PrimitiveType::Tuple, &format!("fn ({name}₁, {name}₂, …, {name}ₙ)"), "#trait-implementations-1", cx)?;
1178+
// Not implemented.
1179+
assert!(!bare_fn.decl.c_variadic);
1180+
// Write output.
1181+
if let clean::FnRetTy::Return(ty) = &bare_fn.decl.output {
1182+
write!(f, " -> ")?;
1183+
fmt_type(ty, f, use_absolute, cx)?;
1184+
}
11721185
} else if let Some(ty) = self.kind.as_blanket_ty() {
11731186
fmt_type(ty, f, use_absolute, cx)?;
11741187
} else {

src/librustdoc/json/conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ impl FromWithTcx<clean::Impl> for Impl {
574574
let trait_ = trait_.map(|path| clean::Type::Path { path }.into_tcx(tcx));
575575
// FIXME: use something like ImplKind in JSON?
576576
let (synthetic, blanket_impl) = match kind {
577-
clean::ImplKind::Normal | clean::ImplKind::TupleVaradic => (false, None),
577+
clean::ImplKind::Normal | clean::ImplKind::FakeVaradic => (false, None),
578578
clean::ImplKind::Auto => (true, None),
579579
clean::ImplKind::Blanket(ty) => (false, Some(*ty)),
580580
};

src/test/rustdoc-ui/tuple-variadic-check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
trait Mine {}
44

55
// This one is fine
6-
#[doc(tuple_variadic)]
6+
#[doc(fake_variadic)]
77
impl<T> Mine for (T,) {}
88

99
trait Mine2 {}
1010

1111
// This one is not
12-
#[doc(tuple_variadic)] //~ ERROR
12+
#[doc(fake_variadic)] //~ ERROR
1313
impl<T, U> Mine for (T,U) {}
1414

1515
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: `#[doc(tuple_variadic)]` must be used on the first of a set of tuple trait impls with varying arity
1+
error: `#[doc(fake_variadic)]` must be used on the first of a set of tuple or fn pointer trait impls with varying arity
22
--> $DIR/tuple-variadic-check.rs:12:7
33
|
4-
LL | #[doc(tuple_variadic)]
5-
| ^^^^^^^^^^^^^^
4+
LL | #[doc(fake_variadic)]
5+
| ^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

src/test/rustdoc/primitive-tuple-auto-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// @has - '//h2[@id="trait-implementations-1"]' 'Trait implementations'
1717
/// # Trait implementations
1818
///
19-
/// This header is hard-coded in the HTML format linking for `#[doc(tuple_variadics)]`.
19+
/// This header is hard-coded in the HTML format linking for `#[doc(fake_variadics)]`.
2020
/// To make sure it gets linked correctly, we need to make sure the hardcoded anchor
2121
/// in the code matches what rustdoc generates for the header.
2222
mod tuple_prim {}

src/test/rustdoc/primitive-tuple-variadic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ pub trait Foo {}
77

88
// @has foo/trait.Foo.html
99
// @has - '//section[@id="impl-Foo-for-(T%2C)"]/h3' 'impl<T> Foo for (T₁, T₂, …, Tₙ)'
10-
#[doc(tuple_variadic)]
10+
#[doc(fake_variadic)]
1111
impl<T> Foo for (T,) {}
1212

1313
pub trait Bar {}
1414

1515
// @has foo/trait.Bar.html
1616
// @has - '//section[@id="impl-Bar-for-(U%2C)"]/h3' 'impl<U: Foo> Bar for (U₁, U₂, …, Uₙ)'
17-
#[doc(tuple_variadic)]
17+
#[doc(fake_variadic)]
1818
impl<U: Foo> Bar for (U,) {}

src/test/ui/feature-gates/feature-gate-rustdoc_internals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod foo {}
44

55
trait Mine {}
66

7-
#[doc(tuple_variadic)] //~ ERROR: `#[doc(tuple_variadic)]` is meant for internal use only
7+
#[doc(fake_variadic)] //~ ERROR: `#[doc(fake_variadic)]` is meant for internal use only
88
impl<T> Mine for (T,) {}
99

1010
fn main() {}

src/test/ui/feature-gates/feature-gate-rustdoc_internals.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | #[doc(keyword = "match")]
77
= note: see issue #90418 <https://github.com/rust-lang/rust/issues/90418> for more information
88
= help: add `#![feature(rustdoc_internals)]` to the crate attributes to enable
99

10-
error[E0658]: `#[doc(tuple_variadic)]` is meant for internal use only
10+
error[E0658]: `#[doc(fake_variadic)]` is meant for internal use only
1111
--> $DIR/feature-gate-rustdoc_internals.rs:7:1
1212
|
13-
LL | #[doc(tuple_variadic)]
14-
| ^^^^^^^^^^^^^^^^^^^^^^
13+
LL | #[doc(fake_variadic)]
14+
| ^^^^^^^^^^^^^^^^^^^^^
1515
|
1616
= note: see issue #90418 <https://github.com/rust-lang/rust/issues/90418> for more information
1717
= help: add `#![feature(rustdoc_internals)]` to the crate attributes to enable

0 commit comments

Comments
 (0)