From cbfe8749c2bef9b4a0b9c1c8637e3ee578caf038 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 3 Aug 2021 14:04:27 +0200 Subject: [PATCH 1/3] Add check for doc(test(...)) attribute --- compiler/rustc_passes/src/check_attr.rs | 45 +++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 1bb6b899875ff..3fea75954b958 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -717,6 +717,42 @@ impl CheckAttrVisitor<'tcx> { true } + /// Checks that `doc(test(...))` attribute contains only valid attributes. Returns `true` if + /// valid. + fn check_test_attr(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool { + let mut is_valid = true; + if let Some(metas) = meta.meta_item_list() { + for i_meta in metas { + match i_meta.name_or_empty() { + sym::attr | sym::no_crate_inject => {} + _ => { + self.tcx.struct_span_lint_hir( + INVALID_DOC_ATTRIBUTES, + hir_id, + i_meta.span(), + |lint| { + lint.build(&format!( + "unknown `doc(test)` attribute `{}`", + rustc_ast_pretty::pprust::path_to_string( + &i_meta.meta_item().unwrap().path + ), + )) + .emit(); + }, + ); + is_valid = false; + } + } + } + } else { + self.tcx.struct_span_lint_hir(INVALID_DOC_ATTRIBUTES, hir_id, meta.span(), |lint| { + lint.build("`#[doc(test(...)]` takes a list of attributes").emit(); + }); + is_valid = false; + } + is_valid + } + /// Runs various checks on `#[doc]` attributes. Returns `true` if valid. /// /// `specified_inline` should be initialized to `None` and kept for the scope @@ -793,8 +829,13 @@ impl CheckAttrVisitor<'tcx> { | sym::no_inline | sym::notable_trait | sym::passes - | sym::plugins - | sym::test => {} + | sym::plugins => {} + + sym::test => { + if !self.check_test_attr(&meta, hir_id) { + is_valid = false; + } + } sym::primitive => { if !self.tcx.features().doc_primitive { From 4ba54ff60b39dc3ca27a81385f0db002fae415e7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 3 Aug 2021 14:04:55 +0200 Subject: [PATCH 2/3] Add UI tests for doc(test(...)) attribute checks --- src/test/rustdoc-ui/doc-test-attr.rs | 16 +++++++++++ src/test/rustdoc-ui/doc-test-attr.stderr | 34 ++++++++++++++++++++++++ src/test/ui/rustdoc/doc-test-attr.rs | 16 +++++++++++ src/test/ui/rustdoc/doc-test-attr.stderr | 34 ++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 src/test/rustdoc-ui/doc-test-attr.rs create mode 100644 src/test/rustdoc-ui/doc-test-attr.stderr create mode 100644 src/test/ui/rustdoc/doc-test-attr.rs create mode 100644 src/test/ui/rustdoc/doc-test-attr.stderr diff --git a/src/test/rustdoc-ui/doc-test-attr.rs b/src/test/rustdoc-ui/doc-test-attr.rs new file mode 100644 index 0000000000000..69288d3adb4f8 --- /dev/null +++ b/src/test/rustdoc-ui/doc-test-attr.rs @@ -0,0 +1,16 @@ +#![crate_type = "lib"] +#![deny(invalid_doc_attributes)] + +#![doc(test)] +//~^ ERROR `#[doc(test(...)]` takes a list of attributes +//~^^ WARN this was previously accepted by the compiler +#![doc(test = "hello")] +//~^ ERROR `#[doc(test(...)]` takes a list of attributes +//~^^ WARN this was previously accepted by the compiler +#![doc(test(a))] +//~^ ERROR unknown `doc(test)` attribute `a` +//~^^ WARN this was previously accepted by the compiler +#![doc(test(no_crate_inject))] +#![doc(test(attr(deny(warnings))))] + +pub fn foo() {} diff --git a/src/test/rustdoc-ui/doc-test-attr.stderr b/src/test/rustdoc-ui/doc-test-attr.stderr new file mode 100644 index 0000000000000..7f5e2d6bc70d9 --- /dev/null +++ b/src/test/rustdoc-ui/doc-test-attr.stderr @@ -0,0 +1,34 @@ +error: `#[doc(test(...)]` takes a list of attributes + --> $DIR/doc-test-attr.rs:4:8 + | +LL | #![doc(test)] + | ^^^^ + | +note: the lint level is defined here + --> $DIR/doc-test-attr.rs:2:9 + | +LL | #![deny(invalid_doc_attributes)] + | ^^^^^^^^^^^^^^^^^^^^^^ + = 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(test(...)]` takes a list of attributes + --> $DIR/doc-test-attr.rs:7:8 + | +LL | #![doc(test = "hello")] + | ^^^^^^^^^^^^^^ + | + = 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: unknown `doc(test)` attribute `a` + --> $DIR/doc-test-attr.rs:10:13 + | +LL | #![doc(test(a))] + | ^ + | + = 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 + diff --git a/src/test/ui/rustdoc/doc-test-attr.rs b/src/test/ui/rustdoc/doc-test-attr.rs new file mode 100644 index 0000000000000..69288d3adb4f8 --- /dev/null +++ b/src/test/ui/rustdoc/doc-test-attr.rs @@ -0,0 +1,16 @@ +#![crate_type = "lib"] +#![deny(invalid_doc_attributes)] + +#![doc(test)] +//~^ ERROR `#[doc(test(...)]` takes a list of attributes +//~^^ WARN this was previously accepted by the compiler +#![doc(test = "hello")] +//~^ ERROR `#[doc(test(...)]` takes a list of attributes +//~^^ WARN this was previously accepted by the compiler +#![doc(test(a))] +//~^ ERROR unknown `doc(test)` attribute `a` +//~^^ WARN this was previously accepted by the compiler +#![doc(test(no_crate_inject))] +#![doc(test(attr(deny(warnings))))] + +pub fn foo() {} diff --git a/src/test/ui/rustdoc/doc-test-attr.stderr b/src/test/ui/rustdoc/doc-test-attr.stderr new file mode 100644 index 0000000000000..7f5e2d6bc70d9 --- /dev/null +++ b/src/test/ui/rustdoc/doc-test-attr.stderr @@ -0,0 +1,34 @@ +error: `#[doc(test(...)]` takes a list of attributes + --> $DIR/doc-test-attr.rs:4:8 + | +LL | #![doc(test)] + | ^^^^ + | +note: the lint level is defined here + --> $DIR/doc-test-attr.rs:2:9 + | +LL | #![deny(invalid_doc_attributes)] + | ^^^^^^^^^^^^^^^^^^^^^^ + = 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(test(...)]` takes a list of attributes + --> $DIR/doc-test-attr.rs:7:8 + | +LL | #![doc(test = "hello")] + | ^^^^^^^^^^^^^^ + | + = 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: unknown `doc(test)` attribute `a` + --> $DIR/doc-test-attr.rs:10:13 + | +LL | #![doc(test(a))] + | ^ + | + = 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 + From d4293ffcb34b17af42815b1669aeec50e9cf50b8 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 7 Aug 2021 14:49:34 +0200 Subject: [PATCH 3/3] Move working checks into separate test files --- src/test/rustdoc-ui/doc-test-attr-pass.rs | 8 ++++++++ src/test/rustdoc-ui/doc-test-attr.rs | 2 -- src/test/ui/rustdoc/doc-test-attr-pass.rs | 9 +++++++++ src/test/ui/rustdoc/doc-test-attr.rs | 2 -- 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 src/test/rustdoc-ui/doc-test-attr-pass.rs create mode 100644 src/test/ui/rustdoc/doc-test-attr-pass.rs diff --git a/src/test/rustdoc-ui/doc-test-attr-pass.rs b/src/test/rustdoc-ui/doc-test-attr-pass.rs new file mode 100644 index 0000000000000..12608f2445075 --- /dev/null +++ b/src/test/rustdoc-ui/doc-test-attr-pass.rs @@ -0,0 +1,8 @@ +// check-pass + +#![crate_type = "lib"] +#![deny(invalid_doc_attributes)] +#![doc(test(no_crate_inject))] +#![doc(test(attr(deny(warnings))))] + +pub fn foo() {} diff --git a/src/test/rustdoc-ui/doc-test-attr.rs b/src/test/rustdoc-ui/doc-test-attr.rs index 69288d3adb4f8..46178ad865a4c 100644 --- a/src/test/rustdoc-ui/doc-test-attr.rs +++ b/src/test/rustdoc-ui/doc-test-attr.rs @@ -10,7 +10,5 @@ #![doc(test(a))] //~^ ERROR unknown `doc(test)` attribute `a` //~^^ WARN this was previously accepted by the compiler -#![doc(test(no_crate_inject))] -#![doc(test(attr(deny(warnings))))] pub fn foo() {} diff --git a/src/test/ui/rustdoc/doc-test-attr-pass.rs b/src/test/ui/rustdoc/doc-test-attr-pass.rs new file mode 100644 index 0000000000000..7884addd15fe7 --- /dev/null +++ b/src/test/ui/rustdoc/doc-test-attr-pass.rs @@ -0,0 +1,9 @@ +// check-pass + +#![crate_type = "lib"] +#![deny(invalid_doc_attributes)] +#![doc(test(no_crate_inject))] +#![doc(test(attr(deny(warnings))))] +#![doc(test())] + +pub fn foo() {} diff --git a/src/test/ui/rustdoc/doc-test-attr.rs b/src/test/ui/rustdoc/doc-test-attr.rs index 69288d3adb4f8..46178ad865a4c 100644 --- a/src/test/ui/rustdoc/doc-test-attr.rs +++ b/src/test/ui/rustdoc/doc-test-attr.rs @@ -10,7 +10,5 @@ #![doc(test(a))] //~^ ERROR unknown `doc(test)` attribute `a` //~^^ WARN this was previously accepted by the compiler -#![doc(test(no_crate_inject))] -#![doc(test(attr(deny(warnings))))] pub fn foo() {}