|
1 | 1 | use super::{StringReader, UnmatchedBrace};
|
2 | 2 |
|
3 |
| -use rustc_ast::token::{self, Token}; |
| 3 | +use rustc_ast::token::{self, Token, DelimToken}; |
4 | 4 | use rustc_ast::tokenstream::{
|
5 | 5 | DelimSpan,
|
6 | 6 | IsJoint::{self, *},
|
@@ -44,7 +44,7 @@ struct TokenTreesReader<'a> {
|
44 | 44 | /// Collect empty block spans that might have been auto-inserted by editors.
|
45 | 45 | last_delim_empty_block_spans: FxHashMap<token::DelimToken, Span>,
|
46 | 46 | /// Collect the spans of braces (Open, Close). Used only
|
47 |
| - /// for detecting if blocks are empty |
| 47 | + /// for detecting if blocks are empty and only braces. |
48 | 48 | matching_block_spans: Vec<(Span, Span)>,
|
49 | 49 | }
|
50 | 50 |
|
@@ -151,7 +151,13 @@ impl<'a> TokenTreesReader<'a> {
|
151 | 151 | }
|
152 | 152 | }
|
153 | 153 |
|
154 |
| - self.matching_block_spans.push((open_brace_span, close_brace_span)); |
| 154 | + match (open_brace, delim) { |
| 155 | + //only add braces |
| 156 | + (DelimToken::Brace, DelimToken::Brace) => { |
| 157 | + self.matching_block_spans.push((open_brace_span, close_brace_span)); |
| 158 | + } |
| 159 | + _ => {} |
| 160 | + } |
155 | 161 |
|
156 | 162 | if self.open_braces.is_empty() {
|
157 | 163 | // Clear up these spans to avoid suggesting them as we've found
|
@@ -232,18 +238,42 @@ impl<'a> TokenTreesReader<'a> {
|
232 | 238 | let mut err =
|
233 | 239 | self.string_reader.sess.span_diagnostic.struct_span_err(self.token.span, &msg);
|
234 | 240 |
|
235 |
| - if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) { |
236 | 241 | // Braces are added at the end, so the last element is the biggest block
|
237 | 242 | if let Some(parent) = self.matching_block_spans.last() {
|
238 |
| - // Check if the (empty block) is in the last properly closed block |
239 |
| - if (parent.0.to(parent.1)).contains(span) { |
| 243 | + |
| 244 | + if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) { |
| 245 | + // Check if the (empty block) is in the last properly closed block |
| 246 | + if (parent.0.to(parent.1)).contains(span) { |
| 247 | + err.span_label( |
| 248 | + span, |
| 249 | + "this block is empty, you might have not meant to close it", |
| 250 | + ); |
| 251 | + } |
| 252 | + else { |
| 253 | + err.span_label( |
| 254 | + parent.0, |
| 255 | + "this opening brace...", |
| 256 | + ); |
| 257 | + |
| 258 | + err.span_label( |
| 259 | + parent.1, |
| 260 | + "...matches this closing brace", |
| 261 | + ); |
| 262 | + } |
| 263 | + } |
| 264 | + else { |
| 265 | + err.span_label( |
| 266 | + parent.0, |
| 267 | + "this opening brace...", |
| 268 | + ); |
| 269 | + |
240 | 270 | err.span_label(
|
241 |
| - span, |
242 |
| - "this block is empty, you might have not meant to close it", |
| 271 | + parent.1, |
| 272 | + "...matches this closing brace", |
243 | 273 | );
|
244 | 274 | }
|
245 |
| - } |
246 | 275 | }
|
| 276 | + |
247 | 277 | err.span_label(self.token.span, "unexpected closing delimiter");
|
248 | 278 | Err(err)
|
249 | 279 | }
|
|
0 commit comments