diff options
| author | Guanqun Lu <guanqun.lu@gmail.com> | 2019-11-10 15:38:09 +0800 |
|---|---|---|
| committer | Guanqun Lu <guanqun.lu@gmail.com> | 2019-11-11 22:21:16 +0800 |
| commit | 292ba98cb723fe2ab6ddcac9e4852be960deaaaa (patch) | |
| tree | 8d6f916b003df4696ee2cc66c188c2b45cff9bb3 /src/librustc_parse/parser | |
| parent | 1062b698c1faa66f7d53078cbb9ef2a493dc2ab7 (diff) | |
| download | rust-292ba98cb723fe2ab6ddcac9e4852be960deaaaa.tar.gz rust-292ba98cb723fe2ab6ddcac9e4852be960deaaaa.zip | |
fix an ICE in macro's diagnostic message
Diffstat (limited to 'src/librustc_parse/parser')
| -rw-r--r-- | src/librustc_parse/parser/item.rs | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 3e21436d313..1f386b8bbbf 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -1742,14 +1742,25 @@ impl<'a> Parser<'a> { } fn report_invalid_macro_expansion_item(&self) { + let has_close_delim = self.sess.source_map() + .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.sess.source_map().next_point(self.prev_span) + }; + 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)), String::from(" {")), - (self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()), + (self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), "{".to_string()), + (right_brace_span, '}'.to_string()), ], Applicability::MaybeIncorrect, ).span_suggestion( |
