Skip to content

Commit ac2ebd3

Browse files
ytmimicalebcartwright
authored andcommitted
Prevent ICE when calling parse_attribute without an attribute
Fixes 5729 `parse_attribute` will panic if the first token is not a `#`. To prevent this we return early instead of trying to parse an invalid attribute.
1 parent a463f23 commit ac2ebd3

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

src/parse/macros/cfg_if.rs

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ fn parse_cfg_if_inner<'a>(
3434
if !parser.eat_keyword(kw::If) {
3535
return Err("Expected `if`");
3636
}
37+
38+
if !matches!(parser.token.kind, TokenKind::Pound) {
39+
return Err("Failed to parse attributes");
40+
}
41+
3742
// Inner attributes are not actually syntactically permitted here, but we don't
3843
// care about inner vs outer attributes in this position. Our purpose with this
3944
// special case parsing of cfg_if macros is to ensure we can correctly resolve

tests/rustfmt/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn rustfmt_emits_error_on_line_overflow_true() {
178178
#[test]
179179
#[allow(non_snake_case)]
180180
fn dont_emit_ICE() {
181-
let files = ["tests/target/issue_5728.rs"];
181+
let files = ["tests/target/issue_5728.rs", "tests/target/issue_5729.rs"];
182182

183183
for file in files {
184184
let args = [file];

tests/target/issue_5729.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cfg_if::cfg_if! {
2+
if {
3+
} else if #(&cpus) {
4+
} else [libc::CTL_HW, libc::HW_NCPU, 0, 0]
5+
}

0 commit comments

Comments
 (0)