From 1169832f2ff1ce740dc3d68de2df3745ec4e1aef Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 16 Jun 2022 09:50:57 -0700 Subject: [PATCH] rustdoc: extend `#[doc(tuple_variadic)]` to fn pointers The attribute is also renamed `fake_variadic`. --- compiler/rustc_ast_passes/src/feature_gate.rs | 4 ++-- .../locales/en-US/passes.ftl | 4 ++-- compiler/rustc_passes/src/check_attr.rs | 20 ++++++++++++------- compiler/rustc_passes/src/errors.rs | 4 ++-- compiler/rustc_span/src/symbol.rs | 2 +- library/core/src/fmt/mod.rs | 2 +- library/core/src/hash/mod.rs | 2 +- library/core/src/primitive_docs.rs | 4 ++-- library/core/src/tuple.rs | 2 +- library/std/src/primitive_docs.rs | 4 ++-- src/librustdoc/clean/inline.rs | 4 ++-- src/librustdoc/clean/mod.rs | 4 ++-- src/librustdoc/clean/types.rs | 6 +++--- src/librustdoc/html/format.rs | 15 +++++++++++++- src/librustdoc/json/conversions.rs | 2 +- src/test/rustdoc-ui/tuple-variadic-check.rs | 4 ++-- .../rustdoc-ui/tuple-variadic-check.stderr | 6 +++--- .../rustdoc/primitive-tuple-auto-trait.rs | 2 +- src/test/rustdoc/primitive-tuple-variadic.rs | 4 ++-- .../feature-gate-rustdoc_internals.rs | 2 +- .../feature-gate-rustdoc_internals.stderr | 6 +++--- 21 files changed, 61 insertions(+), 42 deletions(-) diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index ad0e0384acf..e7004ff4bc5 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -402,8 +402,8 @@ macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => { gate_feature_post!(self, rustdoc_internals, attr.span, msg); } - if nested_meta.has_name(sym::tuple_variadic) { - let msg = "`#[doc(tuple_variadic)]` is meant for internal use only"; + if nested_meta.has_name(sym::fake_variadic) { + let msg = "`#[doc(fake_variadic)]` is meant for internal use only"; gate_feature_post!(self, rustdoc_internals, attr.span, msg); } } diff --git a/compiler/rustc_error_messages/locales/en-US/passes.ftl b/compiler/rustc_error_messages/locales/en-US/passes.ftl index e4c9a4dad7b..04c67cf8ff7 100644 --- a/compiler/rustc_error_messages/locales/en-US/passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/passes.ftl @@ -81,8 +81,8 @@ passes-doc-keyword-not-mod = `#[doc(keyword = "...")]` should be used on modules passes-doc-keyword-invalid-ident = `{$doc_keyword}` is not a valid identifier -passes-doc-tuple-variadic-not-first = - `#[doc(tuple_variadic)]` must be used on the first of a set of tuple trait impls with varying arity +passes-doc-fake-variadic-not-valid = + `#[doc(fake_variadic)]` must be used on the first of a set of tuple or fn pointer trait impls with varying arity passes-doc-keyword-only-impl = `#[doc(keyword = "...")]` should be used on impl blocks diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index d96e7d3efe8..f88997f884a 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -706,14 +706,20 @@ fn check_doc_keyword(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool { true } - fn check_doc_tuple_variadic(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool { + fn check_doc_fake_variadic(&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.emit_err(errors::DocTupleVariadicNotFirst { span: meta.span() }); + let is_valid = matches!(&i.self_ty.kind, hir::TyKind::Tup([_])) + || if let hir::TyKind::BareFn(bare_fn_ty) = &i.self_ty.kind { + bare_fn_ty.decl.inputs.len() == 1 + } else { + false + }; + if !is_valid { + self.tcx.sess.emit_err(errors::DocFakeVariadicNotValid { span: meta.span() }); return false; } } @@ -887,9 +893,9 @@ fn check_doc_attrs( is_valid = false } - sym::tuple_variadic - if !self.check_attr_not_crate_level(meta, hir_id, "tuple_variadic") - || !self.check_doc_tuple_variadic(meta, hir_id) => + sym::fake_variadic + if !self.check_attr_not_crate_level(meta, hir_id, "fake_variadic") + || !self.check_doc_fake_variadic(meta, hir_id) => { is_valid = false } @@ -939,7 +945,7 @@ fn check_doc_attrs( | sym::notable_trait | sym::passes | sym::plugins - | sym::tuple_variadic => {} + | sym::fake_variadic => {} sym::test => { if !self.check_test_attr(meta, hir_id) { diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index f8e8720ab54..fcd1e9363b1 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -212,8 +212,8 @@ pub struct DocKeywordInvalidIdent { } #[derive(SessionDiagnostic)] -#[error(passes::doc_tuple_variadic_not_first)] -pub struct DocTupleVariadicNotFirst { +#[error(passes::doc_fake_variadic_not_valid)] +pub struct DocFakeVariadicNotValid { #[primary_span] pub span: Span, } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index d5e65c0b442..924bc2b6bce 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -685,6 +685,7 @@ fabsf32, fabsf64, fadd_fast, + fake_variadic, fdiv_fast, feature, fence, @@ -1460,7 +1461,6 @@ tuple, tuple_from_req, tuple_indexing, - tuple_variadic, two_phase, ty, type_alias_enum_variants, diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 6c3bb7229e5..372141e0933 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -2562,7 +2562,7 @@ fn fmt(&self, f: &mut Formatter<'_>) -> Result { macro_rules! maybe_tuple_doc { ($a:ident @ #[$meta:meta] $item:item) => { - #[doc(tuple_variadic)] + #[cfg_attr(not(bootstrap), doc(fake_variadic))] #[doc = "This trait is implemented for tuples up to twelve items long."] #[$meta] $item diff --git a/library/core/src/hash/mod.rs b/library/core/src/hash/mod.rs index cf428e0b1cf..5974562aced 100644 --- a/library/core/src/hash/mod.rs +++ b/library/core/src/hash/mod.rs @@ -900,7 +900,7 @@ fn hash(&self, state: &mut S) { macro_rules! maybe_tuple_doc { ($a:ident @ #[$meta:meta] $item:item) => { - #[doc(tuple_variadic)] + #[cfg_attr(not(bootstrap), doc(fake_variadic))] #[doc = "This trait is implemented for tuples up to twelve items long."] #[$meta] $item diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index 401c8159988..4f08b562685 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -996,7 +996,7 @@ impl (T,) {} // Fake impl that's only really used for docs. #[cfg(doc)] #[stable(feature = "rust1", since = "1.0.0")] -#[doc(tuple_variadic)] +#[cfg_attr(not(bootstrap), doc(fake_variadic))] /// This trait is implemented on arbitrary-length tuples. impl Clone for (T,) { fn clone(&self) -> Self { @@ -1007,7 +1007,7 @@ fn clone(&self) -> Self { // Fake impl that's only really used for docs. #[cfg(doc)] #[stable(feature = "rust1", since = "1.0.0")] -#[doc(tuple_variadic)] +#[cfg_attr(not(bootstrap), doc(fake_variadic))] /// This trait is implemented on arbitrary-length tuples. impl Copy for (T,) { // empty diff --git a/library/core/src/tuple.rs b/library/core/src/tuple.rs index b664b0bb866..d189e6400f1 100644 --- a/library/core/src/tuple.rs +++ b/library/core/src/tuple.rs @@ -107,7 +107,7 @@ fn default() -> ($($T,)+) { // Otherwise, it hides the docs entirely. macro_rules! maybe_tuple_doc { ($a:ident @ #[$meta:meta] $item:item) => { - #[doc(tuple_variadic)] + #[cfg_attr(not(bootstrap), doc(fake_variadic))] #[doc = "This trait is implemented for tuples up to twelve items long."] #[$meta] $item diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index 401c8159988..4f08b562685 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -996,7 +996,7 @@ impl (T,) {} // Fake impl that's only really used for docs. #[cfg(doc)] #[stable(feature = "rust1", since = "1.0.0")] -#[doc(tuple_variadic)] +#[cfg_attr(not(bootstrap), doc(fake_variadic))] /// This trait is implemented on arbitrary-length tuples. impl Clone for (T,) { fn clone(&self) -> Self { @@ -1007,7 +1007,7 @@ fn clone(&self) -> Self { // Fake impl that's only really used for docs. #[cfg(doc)] #[stable(feature = "rust1", since = "1.0.0")] -#[doc(tuple_variadic)] +#[cfg_attr(not(bootstrap), doc(fake_variadic))] /// This trait is implemented on arbitrary-length tuples. impl Copy for (T,) { // empty diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 23af7d2ef1e..ce10ca9aa3d 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -506,8 +506,8 @@ pub(crate) fn build_impl( for_, items: trait_items, polarity, - kind: if utils::has_doc_flag(tcx, did, sym::tuple_variadic) { - ImplKind::TupleVaradic + kind: if utils::has_doc_flag(tcx, did, sym::fake_variadic) { + ImplKind::FakeVaradic } else { ImplKind::Normal }, diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index d6260b8ca06..a5c5d1ac182 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2005,8 +2005,8 @@ fn clean_impl<'tcx>( for_, items, polarity: tcx.impl_polarity(def_id), - kind: if utils::has_doc_flag(tcx, def_id.to_def_id(), sym::tuple_variadic) { - ImplKind::TupleVaradic + kind: if utils::has_doc_flag(tcx, def_id.to_def_id(), sym::fake_variadic) { + ImplKind::FakeVaradic } else { ImplKind::Normal }, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index d29ba2dedaf..5ddf7ea8a85 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -2394,7 +2394,7 @@ pub(crate) fn provided_trait_methods(&self, tcx: TyCtxt<'_>) -> FxHashSet), } @@ -2407,8 +2407,8 @@ pub(crate) fn is_blanket(&self) -> bool { matches!(self, ImplKind::Blanket(_)) } - pub(crate) fn is_tuple_variadic(&self) -> bool { - matches!(self, ImplKind::TupleVaradic) + pub(crate) fn is_fake_variadic(&self) -> bool { + matches!(self, ImplKind::FakeVaradic) } pub(crate) fn as_blanket_ty(&self) -> Option<&Type> { diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 9f46ab54d3e..291e6bc2fe4 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1165,10 +1165,23 @@ pub(crate) fn print<'a, 'tcx: 'a>( if let clean::Type::Tuple(types) = &self.for_ && let [clean::Type::Generic(name)] = &types[..] && - (self.kind.is_tuple_variadic() || self.kind.is_auto()) { + (self.kind.is_fake_variadic() || self.kind.is_auto()) { // Hardcoded anchor library/core/src/primitive_docs.rs // Link should match `# Trait implementations` primitive_link_fragment(f, PrimitiveType::Tuple, &format!("({name}₁, {name}₂, …, {name}ₙ)"), "#trait-implementations-1", cx)?; + } else if let clean::Type::BareFunction(bare_fn) = &self.for_ && + let [clean::Argument { type_: clean::Type::Generic(name), .. }] = &bare_fn.decl.inputs.values[..] && + (self.kind.is_fake_variadic() || self.kind.is_auto()) { + // Hardcoded anchor library/core/src/primitive_docs.rs + // Link should match `# Trait implementations` + primitive_link_fragment(f, PrimitiveType::Tuple, &format!("fn ({name}₁, {name}₂, …, {name}ₙ)"), "#trait-implementations-1", cx)?; + // Not implemented. + assert!(!bare_fn.decl.c_variadic); + // Write output. + if let clean::FnRetTy::Return(ty) = &bare_fn.decl.output { + write!(f, " -> ")?; + fmt_type(ty, f, use_absolute, cx)?; + } } else if let Some(ty) = self.kind.as_blanket_ty() { fmt_type(ty, f, use_absolute, cx)?; } else { diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 2598b9b0b28..9000ab472d9 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -574,7 +574,7 @@ fn from_tcx(impl_: clean::Impl, tcx: TyCtxt<'_>) -> Self { 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 | clean::ImplKind::TupleVaradic => (false, None), + clean::ImplKind::Normal | clean::ImplKind::FakeVaradic => (false, None), clean::ImplKind::Auto => (true, None), clean::ImplKind::Blanket(ty) => (false, Some(*ty)), }; diff --git a/src/test/rustdoc-ui/tuple-variadic-check.rs b/src/test/rustdoc-ui/tuple-variadic-check.rs index 11ce2dbe280..505de53481f 100644 --- a/src/test/rustdoc-ui/tuple-variadic-check.rs +++ b/src/test/rustdoc-ui/tuple-variadic-check.rs @@ -3,13 +3,13 @@ trait Mine {} // This one is fine -#[doc(tuple_variadic)] +#[doc(fake_variadic)] impl Mine for (T,) {} trait Mine2 {} // This one is not -#[doc(tuple_variadic)] //~ ERROR +#[doc(fake_variadic)] //~ ERROR impl Mine for (T,U) {} fn main() {} diff --git a/src/test/rustdoc-ui/tuple-variadic-check.stderr b/src/test/rustdoc-ui/tuple-variadic-check.stderr index 358d06d6a42..d127fb858d1 100644 --- a/src/test/rustdoc-ui/tuple-variadic-check.stderr +++ b/src/test/rustdoc-ui/tuple-variadic-check.stderr @@ -1,8 +1,8 @@ -error: `#[doc(tuple_variadic)]` must be used on the first of a set of tuple trait impls with varying arity +error: `#[doc(fake_variadic)]` must be used on the first of a set of tuple or fn pointer trait impls with varying arity --> $DIR/tuple-variadic-check.rs:12:7 | -LL | #[doc(tuple_variadic)] - | ^^^^^^^^^^^^^^ +LL | #[doc(fake_variadic)] + | ^^^^^^^^^^^^^ error: aborting due to previous error diff --git a/src/test/rustdoc/primitive-tuple-auto-trait.rs b/src/test/rustdoc/primitive-tuple-auto-trait.rs index 71b0b077009..a2fbbf078d5 100644 --- a/src/test/rustdoc/primitive-tuple-auto-trait.rs +++ b/src/test/rustdoc/primitive-tuple-auto-trait.rs @@ -16,7 +16,7 @@ // @has - '//h2[@id="trait-implementations-1"]' 'Trait implementations' /// # Trait implementations /// -/// This header is hard-coded in the HTML format linking for `#[doc(tuple_variadics)]`. +/// This header is hard-coded in the HTML format linking for `#[doc(fake_variadics)]`. /// To make sure it gets linked correctly, we need to make sure the hardcoded anchor /// in the code matches what rustdoc generates for the header. mod tuple_prim {} diff --git a/src/test/rustdoc/primitive-tuple-variadic.rs b/src/test/rustdoc/primitive-tuple-variadic.rs index 4fd6254f674..db7cfd60c71 100644 --- a/src/test/rustdoc/primitive-tuple-variadic.rs +++ b/src/test/rustdoc/primitive-tuple-variadic.rs @@ -7,12 +7,12 @@ pub trait Foo {} // @has foo/trait.Foo.html // @has - '//section[@id="impl-Foo-for-(T%2C)"]/h3' 'impl Foo for (T₁, T₂, …, Tₙ)' -#[doc(tuple_variadic)] +#[doc(fake_variadic)] impl Foo for (T,) {} pub trait Bar {} // @has foo/trait.Bar.html // @has - '//section[@id="impl-Bar-for-(U%2C)"]/h3' 'impl Bar for (U₁, U₂, …, Uₙ)' -#[doc(tuple_variadic)] +#[doc(fake_variadic)] impl Bar for (U,) {} 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 6a144412d07..58306a4cfc9 100644 --- a/src/test/ui/feature-gates/feature-gate-rustdoc_internals.rs +++ b/src/test/ui/feature-gates/feature-gate-rustdoc_internals.rs @@ -4,7 +4,7 @@ mod foo {} trait Mine {} -#[doc(tuple_variadic)] //~ ERROR: `#[doc(tuple_variadic)]` is meant for internal use only +#[doc(fake_variadic)] //~ ERROR: `#[doc(fake_variadic)]` is meant for internal use only impl 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 9fe08afd4f0..c4272a2c04c 100644 --- a/src/test/ui/feature-gates/feature-gate-rustdoc_internals.stderr +++ b/src/test/ui/feature-gates/feature-gate-rustdoc_internals.stderr @@ -7,11 +7,11 @@ LL | #[doc(keyword = "match")] = note: see issue #90418 for more information = help: add `#![feature(rustdoc_internals)]` to the crate attributes to enable -error[E0658]: `#[doc(tuple_variadic)]` is meant for internal use only +error[E0658]: `#[doc(fake_variadic)]` is meant for internal use only --> $DIR/feature-gate-rustdoc_internals.rs:7:1 | -LL | #[doc(tuple_variadic)] - | ^^^^^^^^^^^^^^^^^^^^^^ +LL | #[doc(fake_variadic)] + | ^^^^^^^^^^^^^^^^^^^^^ | = note: see issue #90418 for more information = help: add `#![feature(rustdoc_internals)]` to the crate attributes to enable