From f528414e4092815e96690275c527717871ff411d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 12 Oct 2022 11:32:00 +0200 Subject: [PATCH 1/2] Add missing checks for `doc(cfg_hide(...))` attribute --- .../locales/en-US/passes.ftl | 3 +++ compiler/rustc_passes/src/check_attr.rs | 23 +++++++++++++++++++ compiler/rustc_passes/src/errors.rs | 4 ++++ 3 files changed, 30 insertions(+) diff --git a/compiler/rustc_error_messages/locales/en-US/passes.ftl b/compiler/rustc_error_messages/locales/en-US/passes.ftl index 1f1c9c29d665a..4bc6bd9fb2207 100644 --- a/compiler/rustc_error_messages/locales/en-US/passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/passes.ftl @@ -145,6 +145,9 @@ passes_doc_test_takes_list = passes_doc_primitive = `doc(primitive)` should never have been stable +passes_doc_cfg_hide_takes_list = + `#[doc(cfg_hide(...)]` takes a list of attributes + passes_doc_test_unknown_any = unknown `doc` attribute `{$path}` diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 73fb89bbc385e..c5ba4eb68fac0 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -934,6 +934,22 @@ impl CheckAttrVisitor<'_> { is_valid } + /// Check that the `#![doc(cfg_hide(...))]` attribute only contains a list of attributes. + /// Returns `true` if valid. + fn check_doc_cfg_hide(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool { + if meta.meta_item_list().is_some() { + true + } else { + self.tcx.emit_spanned_lint( + INVALID_DOC_ATTRIBUTES, + hir_id, + meta.span(), + errors::DocCfgHideTakesList, + ); + false + } + } + /// Runs various checks on `#[doc]` attributes. Returns `true` if valid. /// /// `specified_inline` should be initialized to `None` and kept for the scope @@ -987,6 +1003,13 @@ impl CheckAttrVisitor<'_> { is_valid = false; } + sym::cfg_hide + if !self.check_attr_crate_level(attr, meta, hir_id) + || !self.check_doc_cfg_hide(meta, hir_id) => + { + is_valid = false; + } + sym::inline | sym::no_inline if !self.check_doc_inline( attr, diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 1cc81a9ab9884..ed54834134453 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -271,6 +271,10 @@ pub struct DocTestUnknown { #[diag(passes::doc_test_takes_list)] pub struct DocTestTakesList; +#[derive(LintDiagnostic)] +#[diag(passes::doc_cfg_hide_takes_list)] +pub struct DocCfgHideTakesList; + #[derive(LintDiagnostic)] #[diag(passes::doc_primitive)] pub struct DocPrimitive; From 6f0c2470dbc3eeff7aa243965ec8c3dc7b64e254 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 12 Oct 2022 11:32:26 +0200 Subject: [PATCH 2/2] Add UI test for invalid `doc(cfg_hide(...))` attributes --- src/test/rustdoc-ui/doc_cfg_hide.rs | 11 +++++++ src/test/rustdoc-ui/doc_cfg_hide.stderr | 40 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/test/rustdoc-ui/doc_cfg_hide.rs create mode 100644 src/test/rustdoc-ui/doc_cfg_hide.stderr diff --git a/src/test/rustdoc-ui/doc_cfg_hide.rs b/src/test/rustdoc-ui/doc_cfg_hide.rs new file mode 100644 index 0000000000000..5d8791748a012 --- /dev/null +++ b/src/test/rustdoc-ui/doc_cfg_hide.rs @@ -0,0 +1,11 @@ +#![feature(doc_cfg_hide)] +#![deny(warnings)] + +#![doc(cfg_hide = "test")] //~ ERROR +//~^ WARN +#![doc(cfg_hide)] //~ ERROR +//~^ WARN + +#[doc(cfg_hide(doc))] //~ ERROR +//~^ WARN +pub fn foo() {} diff --git a/src/test/rustdoc-ui/doc_cfg_hide.stderr b/src/test/rustdoc-ui/doc_cfg_hide.stderr new file mode 100644 index 0000000000000..03623368cd047 --- /dev/null +++ b/src/test/rustdoc-ui/doc_cfg_hide.stderr @@ -0,0 +1,40 @@ +error: this attribute can only be applied at the crate level + --> $DIR/doc_cfg_hide.rs:9:7 + | +LL | #[doc(cfg_hide(doc))] + | ^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + = note: read for more information +note: the lint level is defined here + --> $DIR/doc_cfg_hide.rs:2:9 + | +LL | #![deny(warnings)] + | ^^^^^^^^ + = note: `#[deny(invalid_doc_attributes)]` implied by `#[deny(warnings)]` +help: to apply to the crate, use an inner attribute + | +LL | #![doc(cfg_hide(doc))] + | ~~~~~~~~~~~~~~~~~~~~~~ + +error: `#[doc(cfg_hide(...)]` takes a list of attributes + --> $DIR/doc_cfg_hide.rs:4:8 + | +LL | #![doc(cfg_hide = "test")] + | ^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: `#[doc(cfg_hide(...)]` takes a list of attributes + --> $DIR/doc_cfg_hide.rs:6:8 + | +LL | #![doc(cfg_hide)] + | ^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: aborting due to 3 previous errors +