Skip to content

Commit e8016c2

Browse files
committed
review comments
1 parent 1b0836d commit e8016c2

File tree

3 files changed

+41
-44
lines changed

3 files changed

+41
-44
lines changed

src/libsyntax/parse/parser/diagnostics.rs

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -327,31 +327,27 @@ impl<'a> Parser<'a> {
327327
}
328328

329329
let sm = self.sess.source_map();
330-
match (sm.lookup_line(self.token.span.lo()), sm.lookup_line(sp.lo())) {
331-
(Ok(ref a), Ok(ref b)) if a.line == b.line => {
332-
// When the spans are in the same line, it means that the only content between
333-
// them is whitespace, point at the found token in that case:
334-
//
335-
// X | () => { syntax error };
336-
// | ^^^^^ expected one of 8 possible tokens here
337-
//
338-
// instead of having:
339-
//
340-
// X | () => { syntax error };
341-
// | -^^^^^ unexpected token
342-
// | |
343-
// | expected one of 8 possible tokens here
344-
err.span_label(self.token.span, label_exp);
345-
}
346-
_ if self.prev_span == syntax_pos::DUMMY_SP => {
347-
// Account for macro context where the previous span might not be
348-
// available to avoid incorrect output (#54841).
349-
err.span_label(self.token.span, "unexpected token");
350-
}
351-
_ => {
352-
err.span_label(sp, label_exp);
353-
err.span_label(self.token.span, "unexpected token");
354-
}
330+
if self.prev_span == DUMMY_SP {
331+
// Account for macro context where the previous span might not be
332+
// available to avoid incorrect output (#54841).
333+
err.span_label(self.token.span, label_exp);
334+
} else if !sm.is_multiline(self.token.span.shrink_to_hi().until(sp.shrink_to_lo())) {
335+
// When the spans are in the same line, it means that the only content between
336+
// them is whitespace, point at the found token in that case:
337+
//
338+
// X | () => { syntax error };
339+
// | ^^^^^ expected one of 8 possible tokens here
340+
//
341+
// instead of having:
342+
//
343+
// X | () => { syntax error };
344+
// | -^^^^^ unexpected token
345+
// | |
346+
// | expected one of 8 possible tokens here
347+
err.span_label(self.token.span, label_exp);
348+
} else {
349+
err.span_label(sp, label_exp);
350+
err.span_label(self.token.span, "unexpected token");
355351
}
356352
self.maybe_annotate_with_ascription(&mut err, false);
357353
Err(err)
@@ -894,7 +890,12 @@ impl<'a> Parser<'a> {
894890
let sm = self.sess.source_map();
895891
let msg = format!("expected `;`, found `{}`", self.this_token_descr());
896892
let appl = Applicability::MachineApplicable;
897-
if self.look_ahead(1, |t| t == &token::CloseDelim(token::Brace)
893+
if self.token.span == DUMMY_SP || self.prev_span == DUMMY_SP {
894+
// Likely inside a macro, can't provide meaninful suggestions.
895+
return self.expect(&token::Semi).map(|_| ());
896+
} else if !sm.is_multiline(self.prev_span.until(self.token.span)) {
897+
// The current token is in the same line as the prior token, not recoverable.
898+
} else if self.look_ahead(1, |t| t == &token::CloseDelim(token::Brace)
898899
|| token_can_begin_expr(t) && t.kind != token::Colon
899900
) && [token::Comma, token::Colon].contains(&self.token.kind) {
900901
// Likely typo: `,` → `;` or `:` → `;`. This is triggered if the current token is
@@ -903,14 +904,12 @@ impl<'a> Parser<'a> {
903904
//
904905
// let x = 32:
905906
// let y = 42;
906-
if sm.is_multiline(self.prev_span.until(self.token.span)) {
907-
self.bump();
908-
let sp = self.prev_span;
909-
self.struct_span_err(sp, &msg)
910-
.span_suggestion(sp, "change this to `;`", ";".to_string(), appl)
911-
.emit();
912-
return Ok(())
913-
}
907+
self.bump();
908+
let sp = self.prev_span;
909+
self.struct_span_err(sp, &msg)
910+
.span_suggestion(sp, "change this to `;`", ";".to_string(), appl)
911+
.emit();
912+
return Ok(())
914913
} else if self.look_ahead(0, |t| t == &token::CloseDelim(token::Brace) || (
915914
token_can_begin_expr(t)
916915
&& t != &token::Semi
@@ -921,14 +920,12 @@ impl<'a> Parser<'a> {
921920
//
922921
// let x = 32
923922
// let y = 42;
924-
if sm.is_multiline(self.prev_span.until(self.token.span)) {
925-
let sp = self.prev_span.shrink_to_hi();
926-
self.struct_span_err(sp, &msg)
927-
.span_label(self.token.span, "unexpected token")
928-
.span_suggestion_short(sp, "add `;` here", ";".to_string(), appl)
929-
.emit();
930-
return Ok(())
931-
}
923+
let sp = self.prev_span.shrink_to_hi();
924+
self.struct_span_err(sp, &msg)
925+
.span_label(self.token.span, "unexpected token")
926+
.span_suggestion_short(sp, "add `;` here", ";".to_string(), appl)
927+
.emit();
928+
return Ok(())
932929
}
933930
self.expect(&token::Semi).map(|_| ()) // Error unconditionally
934931
}

src/test/ui/macros/issue-54441.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: expected one of `crate`, `fn`, `pub`, `static`, or `type`, found keyword
22
--> $DIR/issue-54441.rs:3:9
33
|
44
LL | let
5-
| ^^^ unexpected token
5+
| ^^^ expected one of `crate`, `fn`, `pub`, `static`, or `type` here
66
...
77
LL | m!();
88
| ----- in this macro invocation

src/test/ui/parser/macro/trait-non-item-macros.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: expected one of `async`, `const`, `extern`, `fn`, `type`, or `unsafe`, fo
22
--> $DIR/trait-non-item-macros.rs:2:19
33
|
44
LL | ($a:expr) => ($a)
5-
| ^^ unexpected token
5+
| ^^ expected one of `async`, `const`, `extern`, `fn`, `type`, or `unsafe` here
66
...
77
LL | bah!(2);
88
| -------- in this macro invocation

0 commit comments

Comments
 (0)