diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl index 0aa3d6265dce..5b3cc5d99cc7 100644 --- a/compiler/rustc_passes/messages.ftl +++ b/compiler/rustc_passes/messages.ftl @@ -211,6 +211,17 @@ passes_doc_keyword_not_mod = passes_doc_keyword_only_impl = `#[doc(keyword = "...")]` should be used on impl blocks +passes_doc_masked_not_extern_crate_self = + this attribute cannot be applied to an `extern crate self` item + .label = not applicable on `extern crate self` items + .extern_crate_self_label = `extern crate self` defined here + +passes_doc_masked_only_extern_crate = + this attribute can only be applied to an `extern crate` item + .label = only applicable on `extern crate` items + .not_an_extern_crate_label = not an `extern crate` item + .note = read for more information + passes_doc_test_literal = `#![doc(test(...)]` does not take a literal passes_doc_test_takes_list = diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 4d7ebe3fefef..cbb030958c69 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -878,6 +878,44 @@ impl CheckAttrVisitor<'_> { } } + fn check_doc_masked( + &self, + attr: &Attribute, + meta: &NestedMetaItem, + hir_id: HirId, + target: Target, + ) -> bool { + if target != Target::ExternCrate { + self.tcx.emit_spanned_lint( + INVALID_DOC_ATTRIBUTES, + hir_id, + meta.span(), + errors::DocMaskedOnlyExternCrate { + attr_span: meta.span(), + item_span: (attr.style == AttrStyle::Outer) + .then(|| self.tcx.hir().span(hir_id)), + }, + ); + return false; + } + + if self.tcx.extern_mod_stmt_cnum(hir_id.owner).is_none() { + self.tcx.emit_spanned_lint( + INVALID_DOC_ATTRIBUTES, + hir_id, + meta.span(), + errors::DocMaskedNotExternCrateSelf { + attr_span: meta.span(), + item_span: (attr.style == AttrStyle::Outer) + .then(|| self.tcx.hir().span(hir_id)), + }, + ); + return false; + } + + true + } + /// Checks that an attribute is *not* used at the crate level. Returns `true` if valid. fn check_attr_not_crate_level( &self, @@ -1048,6 +1086,17 @@ impl CheckAttrVisitor<'_> { is_valid = false; } + sym::masked + if !self.check_doc_masked( + attr, + meta, + hir_id, + target, + ) => + { + is_valid = false; + } + // no_default_passes: deprecated // passes: deprecated // plugins: removed, but rustdoc warns about it itself diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index eae13f86049a..4f5514372d1e 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -267,6 +267,25 @@ pub struct DocInlineOnlyUse { pub item_span: Option, } +#[derive(LintDiagnostic)] +#[diag(passes_doc_masked_only_extern_crate)] +#[note] +pub struct DocMaskedOnlyExternCrate { + #[label] + pub attr_span: Span, + #[label(passes_not_an_extern_crate_label)] + pub item_span: Option, +} + +#[derive(LintDiagnostic)] +#[diag(passes_doc_masked_not_extern_crate_self)] +pub struct DocMaskedNotExternCrateSelf { + #[label] + pub attr_span: Span, + #[label(passes_extern_crate_self_label)] + pub item_span: Option, +} + #[derive(Diagnostic)] #[diag(passes_doc_attr_not_crate_level)] pub struct DocAttrNotCrateLevel<'a> { diff --git a/tests/rustdoc-ui/lints/invalid-doc-attr.rs b/tests/rustdoc-ui/lints/invalid-doc-attr.rs index c231e43b35ca..16b12cca5a0b 100644 --- a/tests/rustdoc-ui/lints/invalid-doc-attr.rs +++ b/tests/rustdoc-ui/lints/invalid-doc-attr.rs @@ -1,5 +1,10 @@ #![crate_type = "lib"] #![deny(warnings)] +#![feature(doc_masked)] + +#![doc(masked)] +//~^ ERROR this attribute can only be applied to an `extern crate` item +//~| WARN is being phased out #[doc(test(no_crate_inject))] //~^ ERROR can only be applied at the crate level @@ -30,3 +35,13 @@ pub mod bar { //~^^ ERROR conflicting doc inlining attributes //~| HELP remove one of the conflicting attributes pub use bar::baz; + +#[doc(masked)] +//~^ ERROR this attribute can only be applied to an `extern crate` item +//~| WARN is being phased out +pub struct Masked; + +#[doc(masked)] +//~^ ERROR this attribute cannot be applied to an `extern crate self` item +//~| WARN is being phased out +pub extern crate self as reexport; diff --git a/tests/rustdoc-ui/lints/invalid-doc-attr.stderr b/tests/rustdoc-ui/lints/invalid-doc-attr.stderr index b23b8ded8674..82ea33e1d895 100644 --- a/tests/rustdoc-ui/lints/invalid-doc-attr.stderr +++ b/tests/rustdoc-ui/lints/invalid-doc-attr.stderr @@ -1,5 +1,5 @@ error: this attribute can only be applied at the crate level - --> $DIR/invalid-doc-attr.rs:4:7 + --> $DIR/invalid-doc-attr.rs:9:7 | LL | #[doc(test(no_crate_inject))] | ^^^^^^^^^^^^^^^^^^^^^ @@ -19,7 +19,7 @@ LL | #![doc(test(no_crate_inject))] | + error: this attribute can only be applied to a `use` item - --> $DIR/invalid-doc-attr.rs:9:7 + --> $DIR/invalid-doc-attr.rs:14:7 | LL | #[doc(inline)] | ^^^^^^ only applicable on `use` items @@ -32,7 +32,7 @@ LL | pub fn foo() {} = note: read for more information error: this attribute can only be applied at the crate level - --> $DIR/invalid-doc-attr.rs:15:12 + --> $DIR/invalid-doc-attr.rs:20:12 | LL | #![doc(test(no_crate_inject))] | ^^^^^^^^^^^^^^^^^^^^^ @@ -42,7 +42,7 @@ LL | #![doc(test(no_crate_inject))] = note: read for more information error: conflicting doc inlining attributes - --> $DIR/invalid-doc-attr.rs:28:7 + --> $DIR/invalid-doc-attr.rs:33:7 | LL | #[doc(inline)] | ^^^^^^ this attribute... @@ -51,8 +51,43 @@ LL | #[doc(no_inline)] | = help: remove one of the conflicting attributes +error: this attribute can only be applied to an `extern crate` item + --> $DIR/invalid-doc-attr.rs:39:7 + | +LL | #[doc(masked)] + | ^^^^^^ only applicable on `extern crate` items +... +LL | pub struct Masked; + | ----------------- not an `extern crate` item + | + = 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 + +error: this attribute cannot be applied to an `extern crate self` item + --> $DIR/invalid-doc-attr.rs:44:7 + | +LL | #[doc(masked)] + | ^^^^^^ not applicable on `extern crate self` items +... +LL | pub extern crate self as reexport; + | --------------------------------- `extern crate self` defined here + | + = 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: this attribute can only be applied to an `extern crate` item + --> $DIR/invalid-doc-attr.rs:5:8 + | +LL | #![doc(masked)] + | ^^^^^^ only applicable on `extern crate` items + | + = 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 + error: this attribute can only be applied at the crate level - --> $DIR/invalid-doc-attr.rs:19:11 + --> $DIR/invalid-doc-attr.rs:24:11 | LL | #[doc(test(no_crate_inject))] | ^^^^^^^^^^^^^^^^^^^^^ @@ -62,7 +97,7 @@ LL | #[doc(test(no_crate_inject))] = note: read for more information error: this attribute can only be applied to a `use` item - --> $DIR/invalid-doc-attr.rs:22:11 + --> $DIR/invalid-doc-attr.rs:27:11 | LL | #[doc(inline)] | ^^^^^^ only applicable on `use` items @@ -74,5 +109,5 @@ LL | pub fn baz() {} = note: for more information, see issue #82730 = note: read for more information -error: aborting due to 6 previous errors +error: aborting due to 9 previous errors diff --git a/tests/ui/attributes/invalid-doc-attr.rs b/tests/ui/attributes/invalid-doc-attr.rs deleted file mode 100644 index c231e43b35ca..000000000000 --- a/tests/ui/attributes/invalid-doc-attr.rs +++ /dev/null @@ -1,32 +0,0 @@ -#![crate_type = "lib"] -#![deny(warnings)] - -#[doc(test(no_crate_inject))] -//~^ ERROR can only be applied at the crate level -//~| WARN is being phased out -//~| HELP to apply to the crate, use an inner attribute -//~| SUGGESTION ! -#[doc(inline)] -//~^ ERROR can only be applied to a `use` item -//~| WARN is being phased out -pub fn foo() {} - -pub mod bar { - #![doc(test(no_crate_inject))] - //~^ ERROR can only be applied at the crate level - //~| WARN is being phased out - - #[doc(test(no_crate_inject))] - //~^ ERROR can only be applied at the crate level - //~| WARN is being phased out - #[doc(inline)] - //~^ ERROR can only be applied to a `use` item - //~| WARN is being phased out - pub fn baz() {} -} - -#[doc(inline)] -#[doc(no_inline)] -//~^^ ERROR conflicting doc inlining attributes -//~| HELP remove one of the conflicting attributes -pub use bar::baz; diff --git a/tests/ui/attributes/invalid-doc-attr.stderr b/tests/ui/attributes/invalid-doc-attr.stderr deleted file mode 100644 index b23b8ded8674..000000000000 --- a/tests/ui/attributes/invalid-doc-attr.stderr +++ /dev/null @@ -1,78 +0,0 @@ -error: this attribute can only be applied at the crate level - --> $DIR/invalid-doc-attr.rs:4:7 - | -LL | #[doc(test(no_crate_inject))] - | ^^^^^^^^^^^^^^^^^^^^^ - | - = 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/invalid-doc-attr.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(test(no_crate_inject))] - | + - -error: this attribute can only be applied to a `use` item - --> $DIR/invalid-doc-attr.rs:9:7 - | -LL | #[doc(inline)] - | ^^^^^^ only applicable on `use` items -... -LL | pub fn foo() {} - | ------------ not a `use` item - | - = 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 - -error: this attribute can only be applied at the crate level - --> $DIR/invalid-doc-attr.rs:15:12 - | -LL | #![doc(test(no_crate_inject))] - | ^^^^^^^^^^^^^^^^^^^^^ - | - = 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 - -error: conflicting doc inlining attributes - --> $DIR/invalid-doc-attr.rs:28:7 - | -LL | #[doc(inline)] - | ^^^^^^ this attribute... -LL | #[doc(no_inline)] - | ^^^^^^^^^ ...conflicts with this attribute - | - = help: remove one of the conflicting attributes - -error: this attribute can only be applied at the crate level - --> $DIR/invalid-doc-attr.rs:19:11 - | -LL | #[doc(test(no_crate_inject))] - | ^^^^^^^^^^^^^^^^^^^^^ - | - = 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 - -error: this attribute can only be applied to a `use` item - --> $DIR/invalid-doc-attr.rs:22:11 - | -LL | #[doc(inline)] - | ^^^^^^ only applicable on `use` items -... -LL | pub fn baz() {} - | ------------ not a `use` item - | - = 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 - -error: aborting due to 6 previous errors -