Skip to content

Commit 475396a

Browse files
ytmimicalebcartwright
authored andcommitted
Prevent ICE when calling parse_attribute in parse_cfg_if_inner
Fixes 5728 Previously we were ignoring the diagnostic error, which lead to the ICE. Now we properly cancel the error.
1 parent 34f9ca2 commit 475396a

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Diff for: src/parse/macros/cfg_if.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ fn parse_cfg_if_inner<'a>(
4444
// See also https://github.com/rust-lang/rust/pull/79433
4545
parser
4646
.parse_attribute(rustc_parse::parser::attr::InnerAttrPolicy::Permitted)
47-
.map_err(|_| "Failed to parse attributes")?;
47+
.map_err(|e| {
48+
e.cancel();
49+
"Failed to parse attributes"
50+
})?;
4851
}
4952

5053
if !parser.eat(&TokenKind::OpenDelim(Delimiter::Brace)) {

Diff for: tests/rustfmt/main.rs

+12
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,15 @@ fn rustfmt_emits_error_on_line_overflow_true() {
174174
"line formatted, but exceeded maximum width (maximum: 100 (see `max_width` option)"
175175
))
176176
}
177+
178+
#[test]
179+
#[allow(non_snake_case)]
180+
fn dont_emit_ICE() {
181+
let files = ["tests/target/issue_5728.rs"];
182+
183+
for file in files {
184+
let args = [file];
185+
let (_stdout, stderr) = rustfmt(&args);
186+
assert!(!stderr.contains("thread 'main' panicked"));
187+
}
188+
}

Diff for: tests/target/issue_5728.rs

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

0 commit comments

Comments
 (0)