Skip to content

Commit

Permalink
validate doc(masked)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Jul 24, 2023
1 parent 237ed16 commit 637ea3f
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 117 deletions.
11 changes: 11 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://doc.rust-lang.org/unstable-book/language-features/doc-masked.html> for more information
passes_doc_test_literal = `#![doc(test(...)]` does not take a literal
passes_doc_test_takes_list =
Expand Down
49 changes: 49 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,25 @@ pub struct DocInlineOnlyUse {
pub item_span: Option<Span>,
}

#[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<Span>,
}

#[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<Span>,
}

#[derive(Diagnostic)]
#[diag(passes_doc_attr_not_crate_level)]
pub struct DocAttrNotCrateLevel<'a> {
Expand Down
15 changes: 15 additions & 0 deletions tests/rustdoc-ui/lints/invalid-doc-attr.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
49 changes: 42 additions & 7 deletions tests/rustdoc-ui/lints/invalid-doc-attr.stderr
Original file line number Diff line number Diff line change
@@ -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))]
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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
Expand All @@ -32,7 +32,7 @@ LL | pub fn foo() {}
= note: read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#inline-and-no_inline> 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))]
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -42,7 +42,7 @@ LL | #![doc(test(no_crate_inject))]
= note: read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> 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...
Expand All @@ -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 <https://github.com/rust-lang/rust/issues/82730>
= note: read <https://doc.rust-lang.org/unstable-book/language-features/doc-masked.html> 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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/82730>
= note: read <https://doc.rust-lang.org/unstable-book/language-features/doc-masked.html> 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))]
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -62,7 +97,7 @@ LL | #[doc(test(no_crate_inject))]
= note: read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> 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
Expand All @@ -74,5 +109,5 @@ LL | pub fn baz() {}
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
= note: read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#inline-and-no_inline> for more information

error: aborting due to 6 previous errors
error: aborting due to 9 previous errors

32 changes: 0 additions & 32 deletions tests/ui/attributes/invalid-doc-attr.rs

This file was deleted.

78 changes: 0 additions & 78 deletions tests/ui/attributes/invalid-doc-attr.stderr

This file was deleted.

0 comments on commit 637ea3f

Please sign in to comment.