From cef7764a7624fabeb151b861f005a9d89da91a09 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Wed, 29 Jan 2020 18:02:58 +0900 Subject: Avoid ICE in macro's diagnostics --- src/librustc_parse/parser/item.rs | 40 ++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'src/librustc_parse') diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 31db7fc5f75..11adc9fafd6 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -1638,26 +1638,32 @@ impl<'a> Parser<'a> { .span_to_snippet(self.prev_span) .map(|s| s.ends_with(")") || s.ends_with("]")) .unwrap_or(false); - let right_brace_span = if has_close_delim { - // it's safe to peel off one character only when it has the close delim - self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)) - } else { - self.prev_span.shrink_to_hi() - }; - self.struct_span_err( + let mut err = self.struct_span_err( self.prev_span, "macros that expand to items must be delimited with braces or followed by a semicolon", - ) - .multipart_suggestion( - "change the delimiters to curly braces", - vec![ - (self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), "{".to_string()), - (right_brace_span, '}'.to_string()), - ], - Applicability::MaybeIncorrect, - ) - .span_suggestion( + ); + + // To avoid ICE, we shouldn't emit actual suggestions when it hasn't closing delims + if has_close_delim { + err.multipart_suggestion( + "change the delimiters to curly braces", + vec![ + (self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), '{'.to_string()), + (self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()), + ], + Applicability::MaybeIncorrect, + ); + } else { + err.span_suggestion( + self.prev_span, + "change the delimiters to curly braces", + " { /* items */ }".to_string(), + Applicability::MaybeIncorrect, + ); + } + + err.span_suggestion( self.prev_span.shrink_to_hi(), "add a semicolon", ';'.to_string(), -- cgit 1.4.1-3-g733a5 From b1c91ee1b1a671e1d277763169abd9986195712a Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 30 Jan 2020 17:33:19 +0900 Subject: Change Applicability to `HasPlaceholders` --- src/librustc_parse/parser/item.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/librustc_parse') diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 11adc9fafd6..b55756d8129 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -1659,7 +1659,7 @@ impl<'a> Parser<'a> { self.prev_span, "change the delimiters to curly braces", " { /* items */ }".to_string(), - Applicability::MaybeIncorrect, + Applicability::HasPlaceholders, ); } -- cgit 1.4.1-3-g733a5