Skip to content

Commit c79f41e

Browse files
Improve code and better check doc(cfg(...)) attributes
1 parent 9d120c6 commit c79f41e

File tree

13 files changed

+209
-75
lines changed

13 files changed

+209
-75
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
183183
gate_doc!(
184184
"experimental" {
185185
cfg => doc_cfg
186+
auto_cfg => doc_cfg
186187
masked => doc_masked
187188
notable_trait => doc_notable_trait
188189
}

compiler/rustc_passes/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ passes_doc_auto_cfg_hide_show_expects_list =
171171
`#![doc(auto_cfg({$attr_name}(...)))]` expects a list of items
172172
173173
passes_doc_auto_cfg_hide_show_unexpected_item =
174-
`#![doc(auto_cfg({$attr_name}(...)))]` only accepts identifiers or key/values items
174+
`#![doc(auto_cfg({$attr_name}(...)))]` only accepts identifiers or key/value items
175175
176176
passes_doc_auto_cfg_wrong_literal =
177177
`expected boolean for #[doc(auto_cfg = ...)]`

compiler/rustc_passes/src/check_attr.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12831283
}
12841284
MetaItemKind::List(list) => {
12851285
for item in list {
1286-
let Some(attr_name) = item.name() else { continue };
1286+
let Some(attr_name) = item.name() else {
1287+
self.tcx.emit_node_span_lint(
1288+
INVALID_DOC_ATTRIBUTES,
1289+
hir_id,
1290+
meta.span,
1291+
errors::DocAutoCfgExpectsHideOrShow,
1292+
);
1293+
continue;
1294+
};
12871295
if attr_name != sym::hide && attr_name != sym::show {
12881296
self.tcx.emit_node_span_lint(
12891297
INVALID_DOC_ATTRIBUTES,
@@ -1302,6 +1310,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13021310
attr_name: attr_name.as_str(),
13031311
},
13041312
);
1313+
} else if match item {
1314+
MetaItemInner::Lit(_) => true,
1315+
// We already checked above that it's not a list.
1316+
MetaItemInner::MetaItem(meta) => meta.path.segments.len() != 1,
1317+
} {
1318+
self.tcx.emit_node_span_lint(
1319+
INVALID_DOC_ATTRIBUTES,
1320+
hir_id,
1321+
item.span(),
1322+
errors::DocAutoCfgHideShowUnexpectedItem {
1323+
attr_name: attr_name.as_str(),
1324+
},
1325+
);
13051326
}
13061327
}
13071328
} else {

compiler/rustc_span/src/symbol.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,6 @@ symbols! {
11271127
hashset_iter_ty,
11281128
hexagon_target_feature,
11291129
hidden,
1130-
hidden_cfg,
11311130
hide,
11321131
hint,
11331132
homogeneous_aggregate,

src/librustdoc/clean/inline.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,10 @@ pub(crate) fn build_impl(
596596
});
597597
}
598598

599+
// In here, we pass an empty `CfgInfo` because the computation of `cfg` happens later, so it
600+
// doesn't matter at this point.
601+
//
602+
// We need to pass this empty `CfgInfo` because `merge_attrs` is used when computing the `cfg`.
599603
let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs, &mut CfgInfo::default());
600604
trace!("merged_attrs={merged_attrs:?}");
601605

src/librustdoc/clean/types.rs

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,10 @@ fn show_hide_show_conflict_error(
10531053
diag.emit();
10541054
}
10551055

1056-
/// This function checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1057-
/// `auto_cfg(show(...))` on the same item. If so, it emits an error.
1056+
/// This functions updates the `hidden_cfg` field of the provided `cfg_info` argument.
1057+
///
1058+
/// It also checks if a same `cfg` is present in both `auto_cfg(hide(...))` and
1059+
/// `auto_cfg(show(...))` on the same item and emits an error if it's the case.
10581060
///
10591061
/// Because we go through a list of `cfg`s, we keep track of the `cfg`s we saw in `new_show_attrs`
10601062
/// and in `new_hide_attrs` arguments.
@@ -1106,6 +1108,31 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
11061108
Some(item)
11071109
}
11081110

1111+
fn check_changed_auto_active_status(
1112+
changed_auto_active_status: &mut Option<rustc_span::Span>,
1113+
attr: &ast::MetaItem,
1114+
cfg_info: &mut CfgInfo,
1115+
tcx: TyCtxt<'_>,
1116+
new_value: bool,
1117+
) -> bool {
1118+
if let Some(first_change) = changed_auto_active_status {
1119+
if cfg_info.auto_cfg_active != new_value {
1120+
tcx.sess
1121+
.dcx()
1122+
.struct_span_err(
1123+
vec![*first_change, attr.span],
1124+
"`auto_cfg` was disabled and enabled more than once on the same item",
1125+
)
1126+
.emit();
1127+
return true;
1128+
}
1129+
} else {
1130+
*changed_auto_active_status = Some(attr.span);
1131+
}
1132+
cfg_info.auto_cfg_active = new_value;
1133+
false
1134+
}
1135+
11091136
let mut new_show_attrs = FxHashMap::default();
11101137
let mut new_hide_attrs = FxHashMap::default();
11111138

@@ -1153,49 +1180,39 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
11531180
};
11541181
match &attr.kind {
11551182
MetaItemKind::Word => {
1156-
if let Some(first_change) = changed_auto_active_status {
1157-
if !cfg_info.auto_cfg_active {
1158-
tcx.sess.dcx().struct_span_err(
1159-
vec![first_change, attr.span],
1160-
"`auto_cfg` was disabled and enabled more than once on the same item",
1161-
).emit();
1162-
return None;
1163-
}
1164-
} else {
1165-
changed_auto_active_status = Some(attr.span);
1183+
if check_changed_auto_active_status(
1184+
&mut changed_auto_active_status,
1185+
attr,
1186+
cfg_info,
1187+
tcx,
1188+
true,
1189+
) {
1190+
return None;
11661191
}
1167-
cfg_info.auto_cfg_active = true;
11681192
}
11691193
MetaItemKind::NameValue(lit) => {
11701194
if let LitKind::Bool(value) = lit.kind {
1171-
if let Some(first_change) = changed_auto_active_status {
1172-
if cfg_info.auto_cfg_active != value {
1173-
tcx.sess.dcx().struct_span_err(
1174-
vec![first_change, attr.span],
1175-
"`auto_cfg` was disabled and enabled more than once on the same item",
1176-
).emit();
1177-
return None;
1178-
}
1179-
} else {
1180-
changed_auto_active_status = Some(attr.span);
1195+
if check_changed_auto_active_status(
1196+
&mut changed_auto_active_status,
1197+
attr,
1198+
cfg_info,
1199+
tcx,
1200+
value,
1201+
) {
1202+
return None;
11811203
}
1182-
cfg_info.auto_cfg_active = value;
11831204
}
11841205
}
11851206
MetaItemKind::List(sub_attrs) => {
1186-
if let Some(first_change) = changed_auto_active_status {
1187-
if !cfg_info.auto_cfg_active {
1188-
tcx.sess.dcx().struct_span_err(
1189-
vec![first_change, attr.span],
1190-
"`auto_cfg` was disabled and enabled more than once on the same item",
1191-
).emit();
1192-
return None;
1193-
}
1194-
} else {
1195-
changed_auto_active_status = Some(attr.span);
1207+
if check_changed_auto_active_status(
1208+
&mut changed_auto_active_status,
1209+
attr,
1210+
cfg_info,
1211+
tcx,
1212+
true,
1213+
) {
1214+
return None;
11961215
}
1197-
// Whatever happens next, the feature is enabled again.
1198-
cfg_info.auto_cfg_active = true;
11991216
for sub_attr in sub_attrs.iter() {
12001217
if let Some(ident) = sub_attr.ident()
12011218
&& (ident.name == sym::show || ident.name == sym::hide)

tests/rustdoc-ui/doc-cfg.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@
88
//~^^ WARN unexpected `cfg` condition name: `bar`
99
#[doc(cfg())] //~ ERROR
1010
#[doc(cfg(foo, bar))] //~ ERROR
11+
#[doc(auto_cfg(42))] //~ ERROR
12+
#[doc(auto_cfg(hide(true)))] //~ ERROR
13+
#[doc(auto_cfg(hide(42)))] //~ ERROR
14+
#[doc(auto_cfg(hide("a")))] //~ ERROR
15+
#[doc(auto_cfg(hide(foo::bar)))] //~ ERROR
16+
// Shouldn't lint
17+
#[doc(auto_cfg(hide(windows)))]
18+
#[doc(auto_cfg(hide(feature = "windows")))]
19+
#[doc(auto_cfg(hide(foo)))]
1120
pub fn foo() {}

tests/rustdoc-ui/doc-cfg.stderr

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
error: `cfg` predicate is not specified
2-
--> $DIR/doc-cfg.rs:3:7
1+
error: `only "hide" or "show" are allowed in "#[doc(auto_cfg(...))]"`
2+
--> $DIR/doc-cfg.rs:11:7
33
|
4-
LL | #[doc(cfg(), cfg(foo, bar))]
5-
| ^^^^^ help: expected syntax is: `cfg(/* predicate */)`
4+
LL | #[doc(auto_cfg(42))]
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: `#[deny(invalid_doc_attributes)]` on by default
68

7-
error: multiple `cfg` predicates are specified
8-
--> $DIR/doc-cfg.rs:3:23
9+
error: `#![doc(auto_cfg(hide(...)))]` only accepts identifiers or key/value items
10+
--> $DIR/doc-cfg.rs:12:21
911
|
10-
LL | #[doc(cfg(), cfg(foo, bar))]
11-
| ^^^
12+
LL | #[doc(auto_cfg(hide(true)))]
13+
| ^^^^
1214

13-
error: `cfg` predicate is not specified
14-
--> $DIR/doc-cfg.rs:9:7
15+
error: `#![doc(auto_cfg(hide(...)))]` only accepts identifiers or key/value items
16+
--> $DIR/doc-cfg.rs:13:21
1517
|
16-
LL | #[doc(cfg())]
17-
| ^^^^^ help: expected syntax is: `cfg(/* predicate */)`
18+
LL | #[doc(auto_cfg(hide(42)))]
19+
| ^^
1820

19-
error: multiple `cfg` predicates are specified
20-
--> $DIR/doc-cfg.rs:10:16
21+
error: `#![doc(auto_cfg(hide(...)))]` only accepts identifiers or key/value items
22+
--> $DIR/doc-cfg.rs:14:21
2123
|
22-
LL | #[doc(cfg(foo, bar))]
23-
| ^^^
24+
LL | #[doc(auto_cfg(hide("a")))]
25+
| ^^^
26+
27+
error: `#![doc(auto_cfg(hide(...)))]` only accepts identifiers or key/value items
28+
--> $DIR/doc-cfg.rs:15:21
29+
|
30+
LL | #[doc(auto_cfg(hide(foo::bar)))]
31+
| ^^^^^^^^
2432

2533
warning: unexpected `cfg` condition name: `foo`
2634
--> $DIR/doc-cfg.rs:6:11
@@ -42,5 +50,29 @@ LL | #[doc(cfg(foo), cfg(bar))]
4250
= help: to expect this configuration use `--check-cfg=cfg(bar)`
4351
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
4452

45-
error: aborting due to 4 previous errors; 2 warnings emitted
53+
error: `cfg` predicate is not specified
54+
--> $DIR/doc-cfg.rs:3:7
55+
|
56+
LL | #[doc(cfg(), cfg(foo, bar))]
57+
| ^^^^^ help: expected syntax is: `cfg(/* predicate */)`
58+
59+
error: multiple `cfg` predicates are specified
60+
--> $DIR/doc-cfg.rs:3:23
61+
|
62+
LL | #[doc(cfg(), cfg(foo, bar))]
63+
| ^^^
64+
65+
error: `cfg` predicate is not specified
66+
--> $DIR/doc-cfg.rs:9:7
67+
|
68+
LL | #[doc(cfg())]
69+
| ^^^^^ help: expected syntax is: `cfg(/* predicate */)`
70+
71+
error: multiple `cfg` predicates are specified
72+
--> $DIR/doc-cfg.rs:10:16
73+
|
74+
LL | #[doc(cfg(foo, bar))]
75+
| ^^^
76+
77+
error: aborting due to 9 previous errors; 2 warnings emitted
4678

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![doc(auto_cfg)] //~ ERROR
2+
#![doc(auto_cfg(false))] //~ ERROR
3+
#![doc(auto_cfg(true))] //~ ERROR
4+
#![doc(auto_cfg(hide(feature = "solecism")))] //~ ERROR
5+
#![doc(auto_cfg(show(feature = "bla")))] //~ ERROR
6+
#![doc(cfg(feature = "solecism"))] //~ ERROR
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
error[E0658]: `#[doc(auto_cfg)]` is experimental
2+
--> $DIR/feature-gate-doc_cfg.rs:1:1
3+
|
4+
LL | #![doc(auto_cfg)]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #43781 <https://github.com/rust-lang/rust/issues/43781> for more information
8+
= help: add `#![feature(doc_cfg)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: `#[doc(auto_cfg)]` is experimental
12+
--> $DIR/feature-gate-doc_cfg.rs:2:1
13+
|
14+
LL | #![doc(auto_cfg(false))]
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #43781 <https://github.com/rust-lang/rust/issues/43781> for more information
18+
= help: add `#![feature(doc_cfg)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0658]: `#[doc(auto_cfg)]` is experimental
22+
--> $DIR/feature-gate-doc_cfg.rs:3:1
23+
|
24+
LL | #![doc(auto_cfg(true))]
25+
| ^^^^^^^^^^^^^^^^^^^^^^^
26+
|
27+
= note: see issue #43781 <https://github.com/rust-lang/rust/issues/43781> for more information
28+
= help: add `#![feature(doc_cfg)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
31+
error[E0658]: `#[doc(auto_cfg)]` is experimental
32+
--> $DIR/feature-gate-doc_cfg.rs:4:1
33+
|
34+
LL | #![doc(auto_cfg(hide(feature = "solecism")))]
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
|
37+
= note: see issue #43781 <https://github.com/rust-lang/rust/issues/43781> for more information
38+
= help: add `#![feature(doc_cfg)]` to the crate attributes to enable
39+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
40+
41+
error[E0658]: `#[doc(auto_cfg)]` is experimental
42+
--> $DIR/feature-gate-doc_cfg.rs:5:1
43+
|
44+
LL | #![doc(auto_cfg(show(feature = "bla")))]
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
|
47+
= note: see issue #43781 <https://github.com/rust-lang/rust/issues/43781> for more information
48+
= help: add `#![feature(doc_cfg)]` to the crate attributes to enable
49+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
50+
51+
error[E0658]: `#[doc(cfg)]` is experimental
52+
--> $DIR/feature-gate-doc_cfg.rs:6:1
53+
|
54+
LL | #![doc(cfg(feature = "solecism"))]
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
|
57+
= note: see issue #43781 <https://github.com/rust-lang/rust/issues/43781> for more information
58+
= help: add `#![feature(doc_cfg)]` to the crate attributes to enable
59+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
60+
61+
error: aborting due to 6 previous errors
62+
63+
For more information about this error, try `rustc --explain E0658`.

tests/rustdoc-ui/feature-gate-doc_cfg_hide.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/rustdoc-ui/feature-gate-doc_cfg_hide.stderr

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/rustdoc-ui/lints/doc_cfg_hide.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error: `#![doc(auto_cfg(hide(...)))]` expects a list of items
1212
LL | #![doc(auto_cfg(hide))]
1313
| ^^^^^^^^^^^^^^
1414

15-
error: `#![doc(auto_cfg(hide(...)))]` only accepts identifiers or key/values items
15+
error: `#![doc(auto_cfg(hide(...)))]` only accepts identifiers or key/value items
1616
--> $DIR/doc_cfg_hide.rs:4:22
1717
|
1818
LL | #![doc(auto_cfg(hide(not(windows))))]

0 commit comments

Comments
 (0)