From cef7764a7624fabeb151b861f005a9d89da91a09 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Wed, 29 Jan 2020 18:02:58 +0900 Subject: [PATCH] Avoid ICE in macro's diagnostics --- src/librustc_parse/parser/item.rs | 40 ++++++++++-------- src/test/ui/parser/issue-62524.stderr | 5 +-- src/test/ui/parser/issue-68629.rs | Bin 0 -> 336 bytes src/test/ui/parser/issue-68629.stderr | Bin 0 -> 1441 bytes .../ui/parser/mbe_missing_right_paren.stderr | 4 +- 5 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 src/test/ui/parser/issue-68629.rs create mode 100644 src/test/ui/parser/issue-68629.stderr 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(), diff --git a/src/test/ui/parser/issue-62524.stderr b/src/test/ui/parser/issue-62524.stderr index 3482435cd1d..8191c9682ce 100644 --- a/src/test/ui/parser/issue-62524.stderr +++ b/src/test/ui/parser/issue-62524.stderr @@ -16,9 +16,8 @@ LL | | Ϥ, | help: change the delimiters to curly braces | -LL | y!{ -LL | Ϥ} - | +LL | y! { /* items */ } + | ^^^^^^^^^^^^^^^ help: add a semicolon | LL | Ϥ,; diff --git a/src/test/ui/parser/issue-68629.rs b/src/test/ui/parser/issue-68629.rs new file mode 100644 index 0000000000000000000000000000000000000000..672a31f12c838bf7b2e54e6012354acea5852515 GIT binary patch literal 336 zcmdPbSIA7y%P&gREy+x&)GaAW%*@HmOV`ayEzil!OD*Qo*H=g_D#|a?El4aWNiE8= zQYg*K&dV>)Qz$M;EGkjRPg5w#&rZ$5qOc?*vsfW5GbdFcIX|x?F*C1NAu&&(G%q

mG7!p&ia|ysmlowzDkK#pCS&zma$;Uyeu+X_W?qT{*bNE^CB+E} snRyCe&lD%;7o>7=$;g)!Gblz^XL2zxC@7@mDHPq^ou6n|ba!_y0LS)tYybcN literal 0 HcmV?d00001 diff --git a/src/test/ui/parser/issue-68629.stderr b/src/test/ui/parser/issue-68629.stderr new file mode 100644 index 0000000000000000000000000000000000000000..a7885ecec5647e4c3eece95ce5b4ea25c70273ca GIT binary patch literal 1441 zcmYc-D#|aiQYg*K&dV>)Qz$M;EGkjRPg5w#&rZ#=Qiv(7HcYPNQc%# zEG{lh)itv)GqThxDz>t)GUQTFP^jVZ@lmKzkdZGbW>Ac-&IF4o#Bp&EVSXBZ^Nqk} zk#D{Me&bDX8?Qi|2k=^8i4r~x3zbQEp;#QGT&PNk(FcLTW`pVqS_uNxnj6NosDf zLT+hsi9%8;Jfcz*$}>wc6q1S(lT(Wo@{1JG@^f`1trZNL<$_;E0QAqSUg?{L*3taKTy3#RUMN8m)2w literal 0 HcmV?d00001 diff --git a/src/test/ui/parser/mbe_missing_right_paren.stderr b/src/test/ui/parser/mbe_missing_right_paren.stderr index c5b3cc275ce..4ce39de8866 100644 --- a/src/test/ui/parser/mbe_missing_right_paren.stderr +++ b/src/test/ui/parser/mbe_missing_right_paren.stderr @@ -14,8 +14,8 @@ LL | macro_rules! abc(ؼ | help: change the delimiters to curly braces | -LL | macro_rules! abc{ؼ} - | ^ ^ +LL | macro_rules! abc { /* items */ } + | ^^^^^^^^^^^^^^^ help: add a semicolon | LL | macro_rules! abc(ؼ;