Skip to content

Commit 97560c7

Browse files
authored
Rollup merge of rust-lang#102954 - GuillaumeGomez:cfg-hide-attr-checks, r=Manishearth
Add missing checks for `doc(cfg_hide(...))` Part of rust-lang#43781. The `doc(cfg_hide(...))` attribute can only be used at the crate level and takes a list of attributes as argument. r? `@Manishearth`
2 parents eb5863a + 6f0c247 commit 97560c7

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

compiler/rustc_error_messages/locales/en-US/passes.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ passes_doc_test_takes_list =
145145
passes_doc_primitive =
146146
`doc(primitive)` should never have been stable
147147
148+
passes_doc_cfg_hide_takes_list =
149+
`#[doc(cfg_hide(...)]` takes a list of attributes
150+
148151
passes_doc_test_unknown_any =
149152
unknown `doc` attribute `{$path}`
150153

compiler/rustc_passes/src/check_attr.rs

+23
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,22 @@ impl CheckAttrVisitor<'_> {
934934
is_valid
935935
}
936936

937+
/// Check that the `#![doc(cfg_hide(...))]` attribute only contains a list of attributes.
938+
/// Returns `true` if valid.
939+
fn check_doc_cfg_hide(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
940+
if meta.meta_item_list().is_some() {
941+
true
942+
} else {
943+
self.tcx.emit_spanned_lint(
944+
INVALID_DOC_ATTRIBUTES,
945+
hir_id,
946+
meta.span(),
947+
errors::DocCfgHideTakesList,
948+
);
949+
false
950+
}
951+
}
952+
937953
/// Runs various checks on `#[doc]` attributes. Returns `true` if valid.
938954
///
939955
/// `specified_inline` should be initialized to `None` and kept for the scope
@@ -987,6 +1003,13 @@ impl CheckAttrVisitor<'_> {
9871003
is_valid = false;
9881004
}
9891005

1006+
sym::cfg_hide
1007+
if !self.check_attr_crate_level(attr, meta, hir_id)
1008+
|| !self.check_doc_cfg_hide(meta, hir_id) =>
1009+
{
1010+
is_valid = false;
1011+
}
1012+
9901013
sym::inline | sym::no_inline
9911014
if !self.check_doc_inline(
9921015
attr,

compiler/rustc_passes/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ pub struct DocTestUnknown {
271271
#[diag(passes::doc_test_takes_list)]
272272
pub struct DocTestTakesList;
273273

274+
#[derive(LintDiagnostic)]
275+
#[diag(passes::doc_cfg_hide_takes_list)]
276+
pub struct DocCfgHideTakesList;
277+
274278
#[derive(LintDiagnostic)]
275279
#[diag(passes::doc_primitive)]
276280
pub struct DocPrimitive;

src/test/rustdoc-ui/doc_cfg_hide.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(doc_cfg_hide)]
2+
#![deny(warnings)]
3+
4+
#![doc(cfg_hide = "test")] //~ ERROR
5+
//~^ WARN
6+
#![doc(cfg_hide)] //~ ERROR
7+
//~^ WARN
8+
9+
#[doc(cfg_hide(doc))] //~ ERROR
10+
//~^ WARN
11+
pub fn foo() {}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error: this attribute can only be applied at the crate level
2+
--> $DIR/doc_cfg_hide.rs:9:7
3+
|
4+
LL | #[doc(cfg_hide(doc))]
5+
| ^^^^^^^^^^^^^
6+
|
7+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
9+
= note: read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> for more information
10+
note: the lint level is defined here
11+
--> $DIR/doc_cfg_hide.rs:2:9
12+
|
13+
LL | #![deny(warnings)]
14+
| ^^^^^^^^
15+
= note: `#[deny(invalid_doc_attributes)]` implied by `#[deny(warnings)]`
16+
help: to apply to the crate, use an inner attribute
17+
|
18+
LL | #![doc(cfg_hide(doc))]
19+
| ~~~~~~~~~~~~~~~~~~~~~~
20+
21+
error: `#[doc(cfg_hide(...)]` takes a list of attributes
22+
--> $DIR/doc_cfg_hide.rs:4:8
23+
|
24+
LL | #![doc(cfg_hide = "test")]
25+
| ^^^^^^^^^^^^^^^^^
26+
|
27+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
28+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
29+
30+
error: `#[doc(cfg_hide(...)]` takes a list of attributes
31+
--> $DIR/doc_cfg_hide.rs:6:8
32+
|
33+
LL | #![doc(cfg_hide)]
34+
| ^^^^^^^^
35+
|
36+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
37+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
38+
39+
error: aborting due to 3 previous errors
40+

0 commit comments

Comments
 (0)