Skip to content

Commit 637ea3f

Browse files
author
Lukas Markeffsky
committed
validate doc(masked)
1 parent 237ed16 commit 637ea3f

File tree

7 files changed

+136
-117
lines changed

7 files changed

+136
-117
lines changed

compiler/rustc_passes/messages.ftl

+11
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ passes_doc_keyword_not_mod =
211211
passes_doc_keyword_only_impl =
212212
`#[doc(keyword = "...")]` should be used on impl blocks
213213
214+
passes_doc_masked_not_extern_crate_self =
215+
this attribute cannot be applied to an `extern crate self` item
216+
.label = not applicable on `extern crate self` items
217+
.extern_crate_self_label = `extern crate self` defined here
218+
219+
passes_doc_masked_only_extern_crate =
220+
this attribute can only be applied to an `extern crate` item
221+
.label = only applicable on `extern crate` items
222+
.not_an_extern_crate_label = not an `extern crate` item
223+
.note = read <https://doc.rust-lang.org/unstable-book/language-features/doc-masked.html> for more information
224+
214225
passes_doc_test_literal = `#![doc(test(...)]` does not take a literal
215226
216227
passes_doc_test_takes_list =

compiler/rustc_passes/src/check_attr.rs

+49
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,44 @@ impl CheckAttrVisitor<'_> {
878878
}
879879
}
880880

881+
fn check_doc_masked(
882+
&self,
883+
attr: &Attribute,
884+
meta: &NestedMetaItem,
885+
hir_id: HirId,
886+
target: Target,
887+
) -> bool {
888+
if target != Target::ExternCrate {
889+
self.tcx.emit_spanned_lint(
890+
INVALID_DOC_ATTRIBUTES,
891+
hir_id,
892+
meta.span(),
893+
errors::DocMaskedOnlyExternCrate {
894+
attr_span: meta.span(),
895+
item_span: (attr.style == AttrStyle::Outer)
896+
.then(|| self.tcx.hir().span(hir_id)),
897+
},
898+
);
899+
return false;
900+
}
901+
902+
if self.tcx.extern_mod_stmt_cnum(hir_id.owner).is_none() {
903+
self.tcx.emit_spanned_lint(
904+
INVALID_DOC_ATTRIBUTES,
905+
hir_id,
906+
meta.span(),
907+
errors::DocMaskedNotExternCrateSelf {
908+
attr_span: meta.span(),
909+
item_span: (attr.style == AttrStyle::Outer)
910+
.then(|| self.tcx.hir().span(hir_id)),
911+
},
912+
);
913+
return false;
914+
}
915+
916+
true
917+
}
918+
881919
/// Checks that an attribute is *not* used at the crate level. Returns `true` if valid.
882920
fn check_attr_not_crate_level(
883921
&self,
@@ -1048,6 +1086,17 @@ impl CheckAttrVisitor<'_> {
10481086
is_valid = false;
10491087
}
10501088

1089+
sym::masked
1090+
if !self.check_doc_masked(
1091+
attr,
1092+
meta,
1093+
hir_id,
1094+
target,
1095+
) =>
1096+
{
1097+
is_valid = false;
1098+
}
1099+
10511100
// no_default_passes: deprecated
10521101
// passes: deprecated
10531102
// plugins: removed, but rustdoc warns about it itself

compiler/rustc_passes/src/errors.rs

+19
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,25 @@ pub struct DocInlineOnlyUse {
267267
pub item_span: Option<Span>,
268268
}
269269

270+
#[derive(LintDiagnostic)]
271+
#[diag(passes_doc_masked_only_extern_crate)]
272+
#[note]
273+
pub struct DocMaskedOnlyExternCrate {
274+
#[label]
275+
pub attr_span: Span,
276+
#[label(passes_not_an_extern_crate_label)]
277+
pub item_span: Option<Span>,
278+
}
279+
280+
#[derive(LintDiagnostic)]
281+
#[diag(passes_doc_masked_not_extern_crate_self)]
282+
pub struct DocMaskedNotExternCrateSelf {
283+
#[label]
284+
pub attr_span: Span,
285+
#[label(passes_extern_crate_self_label)]
286+
pub item_span: Option<Span>,
287+
}
288+
270289
#[derive(Diagnostic)]
271290
#[diag(passes_doc_attr_not_crate_level)]
272291
pub struct DocAttrNotCrateLevel<'a> {

tests/rustdoc-ui/lints/invalid-doc-attr.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#![crate_type = "lib"]
22
#![deny(warnings)]
3+
#![feature(doc_masked)]
4+
5+
#![doc(masked)]
6+
//~^ ERROR this attribute can only be applied to an `extern crate` item
7+
//~| WARN is being phased out
38

49
#[doc(test(no_crate_inject))]
510
//~^ ERROR can only be applied at the crate level
@@ -30,3 +35,13 @@ pub mod bar {
3035
//~^^ ERROR conflicting doc inlining attributes
3136
//~| HELP remove one of the conflicting attributes
3237
pub use bar::baz;
38+
39+
#[doc(masked)]
40+
//~^ ERROR this attribute can only be applied to an `extern crate` item
41+
//~| WARN is being phased out
42+
pub struct Masked;
43+
44+
#[doc(masked)]
45+
//~^ ERROR this attribute cannot be applied to an `extern crate self` item
46+
//~| WARN is being phased out
47+
pub extern crate self as reexport;

tests/rustdoc-ui/lints/invalid-doc-attr.stderr

+42-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this attribute can only be applied at the crate level
2-
--> $DIR/invalid-doc-attr.rs:4:7
2+
--> $DIR/invalid-doc-attr.rs:9:7
33
|
44
LL | #[doc(test(no_crate_inject))]
55
| ^^^^^^^^^^^^^^^^^^^^^
@@ -19,7 +19,7 @@ LL | #![doc(test(no_crate_inject))]
1919
| +
2020

2121
error: this attribute can only be applied to a `use` item
22-
--> $DIR/invalid-doc-attr.rs:9:7
22+
--> $DIR/invalid-doc-attr.rs:14:7
2323
|
2424
LL | #[doc(inline)]
2525
| ^^^^^^ only applicable on `use` items
@@ -32,7 +32,7 @@ LL | pub fn foo() {}
3232
= note: read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#inline-and-no_inline> for more information
3333

3434
error: this attribute can only be applied at the crate level
35-
--> $DIR/invalid-doc-attr.rs:15:12
35+
--> $DIR/invalid-doc-attr.rs:20:12
3636
|
3737
LL | #![doc(test(no_crate_inject))]
3838
| ^^^^^^^^^^^^^^^^^^^^^
@@ -42,7 +42,7 @@ LL | #![doc(test(no_crate_inject))]
4242
= note: read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> for more information
4343

4444
error: conflicting doc inlining attributes
45-
--> $DIR/invalid-doc-attr.rs:28:7
45+
--> $DIR/invalid-doc-attr.rs:33:7
4646
|
4747
LL | #[doc(inline)]
4848
| ^^^^^^ this attribute...
@@ -51,8 +51,43 @@ LL | #[doc(no_inline)]
5151
|
5252
= help: remove one of the conflicting attributes
5353

54+
error: this attribute can only be applied to an `extern crate` item
55+
--> $DIR/invalid-doc-attr.rs:39:7
56+
|
57+
LL | #[doc(masked)]
58+
| ^^^^^^ only applicable on `extern crate` items
59+
...
60+
LL | pub struct Masked;
61+
| ----------------- not an `extern crate` item
62+
|
63+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
64+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
65+
= note: read <https://doc.rust-lang.org/unstable-book/language-features/doc-masked.html> for more information
66+
67+
error: this attribute cannot be applied to an `extern crate self` item
68+
--> $DIR/invalid-doc-attr.rs:44:7
69+
|
70+
LL | #[doc(masked)]
71+
| ^^^^^^ not applicable on `extern crate self` items
72+
...
73+
LL | pub extern crate self as reexport;
74+
| --------------------------------- `extern crate self` defined here
75+
|
76+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
77+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
78+
79+
error: this attribute can only be applied to an `extern crate` item
80+
--> $DIR/invalid-doc-attr.rs:5:8
81+
|
82+
LL | #![doc(masked)]
83+
| ^^^^^^ only applicable on `extern crate` items
84+
|
85+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
86+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
87+
= note: read <https://doc.rust-lang.org/unstable-book/language-features/doc-masked.html> for more information
88+
5489
error: this attribute can only be applied at the crate level
55-
--> $DIR/invalid-doc-attr.rs:19:11
90+
--> $DIR/invalid-doc-attr.rs:24:11
5691
|
5792
LL | #[doc(test(no_crate_inject))]
5893
| ^^^^^^^^^^^^^^^^^^^^^
@@ -62,7 +97,7 @@ LL | #[doc(test(no_crate_inject))]
6297
= note: read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> for more information
6398

6499
error: this attribute can only be applied to a `use` item
65-
--> $DIR/invalid-doc-attr.rs:22:11
100+
--> $DIR/invalid-doc-attr.rs:27:11
66101
|
67102
LL | #[doc(inline)]
68103
| ^^^^^^ only applicable on `use` items
@@ -74,5 +109,5 @@ LL | pub fn baz() {}
74109
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
75110
= note: read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#inline-and-no_inline> for more information
76111

77-
error: aborting due to 6 previous errors
112+
error: aborting due to 9 previous errors
78113

tests/ui/attributes/invalid-doc-attr.rs

-32
This file was deleted.

tests/ui/attributes/invalid-doc-attr.stderr

-78
This file was deleted.

0 commit comments

Comments
 (0)