Skip to content

Commit 9cb540a

Browse files
committed
Reject leading unsafe in cfg!(...) and --check-cfg.
1 parent 2da3cb9 commit 9cb540a

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

compiler/rustc_builtin_macros/src/cfg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn parse_cfg<'a>(cx: &ExtCtxt<'a>, span: Span, tts: TokenStream) -> PResult<'a,
4343
return Err(cx.dcx().create_err(errors::RequiresCfgPattern { span }));
4444
}
4545

46-
let cfg = p.parse_meta_item(AllowLeadingUnsafe::Yes)?;
46+
let cfg = p.parse_meta_item(AllowLeadingUnsafe::No)?;
4747

4848
let _ = p.eat(&token::Comma);
4949

compiler/rustc_interface/src/interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
174174
}
175175
};
176176

177-
let meta_item = match parser.parse_meta_item(AllowLeadingUnsafe::Yes) {
177+
let meta_item = match parser.parse_meta_item(AllowLeadingUnsafe::No) {
178178
Ok(meta_item) if parser.token == token::Eof => meta_item,
179179
Ok(..) => expected_error(),
180180
Err(err) => {

tests/ui/attributes/unsafe/extraneous-unsafe-attributes.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ mod inner {
2727
#[unsafe(used)] //~ ERROR: is not an unsafe attribute
2828
static FOO: usize = 0;
2929

30-
fn main() {}
30+
fn main() {
31+
let _a = cfg!(unsafe(foo));
32+
//~^ ERROR: expected identifier, found keyword `unsafe`
33+
//~^^ ERROR: invalid predicate `r#unsafe`
34+
}

tests/ui/attributes/unsafe/extraneous-unsafe-attributes.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ LL | #[unsafe(test)]
2222
|
2323
= note: extraneous unsafe is not allowed in attributes
2424

25+
error: expected identifier, found keyword `unsafe`
26+
--> $DIR/extraneous-unsafe-attributes.rs:31:19
27+
|
28+
LL | let _a = cfg!(unsafe(foo));
29+
| ^^^^^^ expected identifier, found keyword
30+
|
31+
help: escape `unsafe` to use it as an identifier
32+
|
33+
LL | let _a = cfg!(r#unsafe(foo));
34+
| ++
35+
36+
error[E0537]: invalid predicate `r#unsafe`
37+
--> $DIR/extraneous-unsafe-attributes.rs:31:19
38+
|
39+
LL | let _a = cfg!(unsafe(foo));
40+
| ^^^^^^^^^^^
41+
2542
error: `ignore` is not an unsafe attribute
2643
--> $DIR/extraneous-unsafe-attributes.rs:13:3
2744
|
@@ -62,5 +79,6 @@ LL | #[unsafe(used)]
6279
|
6380
= note: extraneous unsafe is not allowed in attributes
6481

65-
error: aborting due to 8 previous errors
82+
error: aborting due to 10 previous errors
6683

84+
For more information about this error, try `rustc --explain E0537`.

tests/ui/check-cfg/invalid-arguments.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//@ revisions: values_any_missing_values values_any_before_ident ident_in_values_1
99
//@ revisions: ident_in_values_2 unknown_meta_item_1 unknown_meta_item_2 unknown_meta_item_3
1010
//@ revisions: mixed_values_any mixed_any any_values giberich unterminated
11-
//@ revisions: none_not_empty cfg_none
11+
//@ revisions: none_not_empty cfg_none unsafe_attr
1212
//
1313
//@ [anything_else]compile-flags: --check-cfg=anything_else(...)
1414
//@ [boolean]compile-flags: --check-cfg=cfg(true)
@@ -33,5 +33,6 @@
3333
//@ [cfg_none]compile-flags: --check-cfg=cfg(none())
3434
//@ [giberich]compile-flags: --check-cfg=cfg(...)
3535
//@ [unterminated]compile-flags: --check-cfg=cfg(
36+
//@ [unsafe_attr]compile-flags: --check-cfg=unsafe(cfg(foo))
3637

3738
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: invalid `--check-cfg` argument: `unsafe(cfg(foo))`
2+
|
3+
= note: expected `cfg(name, values("value1", "value2", ... "valueN"))`
4+
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
5+

0 commit comments

Comments
 (0)