Skip to content

Commit cdd41ea

Browse files
committed
Auto merge of rust-lang#68633 - JohnTitor:avoid-ice-in-diagnostics, r=estebank
Avoid ICE in macro's diagnostics Fixes rust-lang#68629 r? @estebank
2 parents cd1ef39 + b1c91ee commit cdd41ea

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed

src/librustc_parse/parser/item.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -1638,26 +1638,32 @@ impl<'a> Parser<'a> {
16381638
.span_to_snippet(self.prev_span)
16391639
.map(|s| s.ends_with(")") || s.ends_with("]"))
16401640
.unwrap_or(false);
1641-
let right_brace_span = if has_close_delim {
1642-
// it's safe to peel off one character only when it has the close delim
1643-
self.prev_span.with_lo(self.prev_span.hi() - BytePos(1))
1644-
} else {
1645-
self.prev_span.shrink_to_hi()
1646-
};
16471641

1648-
self.struct_span_err(
1642+
let mut err = self.struct_span_err(
16491643
self.prev_span,
16501644
"macros that expand to items must be delimited with braces or followed by a semicolon",
1651-
)
1652-
.multipart_suggestion(
1653-
"change the delimiters to curly braces",
1654-
vec![
1655-
(self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), "{".to_string()),
1656-
(right_brace_span, '}'.to_string()),
1657-
],
1658-
Applicability::MaybeIncorrect,
1659-
)
1660-
.span_suggestion(
1645+
);
1646+
1647+
// To avoid ICE, we shouldn't emit actual suggestions when it hasn't closing delims
1648+
if has_close_delim {
1649+
err.multipart_suggestion(
1650+
"change the delimiters to curly braces",
1651+
vec![
1652+
(self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), '{'.to_string()),
1653+
(self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()),
1654+
],
1655+
Applicability::MaybeIncorrect,
1656+
);
1657+
} else {
1658+
err.span_suggestion(
1659+
self.prev_span,
1660+
"change the delimiters to curly braces",
1661+
" { /* items */ }".to_string(),
1662+
Applicability::HasPlaceholders,
1663+
);
1664+
}
1665+
1666+
err.span_suggestion(
16611667
self.prev_span.shrink_to_hi(),
16621668
"add a semicolon",
16631669
';'.to_string(),

src/test/ui/parser/issue-62524.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ LL | | Ϥ,
1616
|
1717
help: change the delimiters to curly braces
1818
|
19-
LL | y!{
20-
LL | Ϥ}
21-
|
19+
LL | y! { /* items */ }
20+
| ^^^^^^^^^^^^^^^
2221
help: add a semicolon
2322
|
2423
LL | Ϥ,;

src/test/ui/parser/issue-68629.rs

336 Bytes
Binary file not shown.

src/test/ui/parser/issue-68629.stderr

1.41 KB
Binary file not shown.

src/test/ui/parser/mbe_missing_right_paren.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ LL | macro_rules! abc(ؼ
1414
|
1515
help: change the delimiters to curly braces
1616
|
17-
LL | macro_rules! abc}
18-
| ^ ^
17+
LL | macro_rules! abc { /* items */ }
18+
| ^^^^^^^^^^^^^^^
1919
help: add a semicolon
2020
|
2121
LL | macro_rules! abc(ؼ;

0 commit comments

Comments
 (0)