Skip to content

Commit

Permalink
Point at unclosed delimiters as part of the primary MultiSpan
Browse files Browse the repository at this point in the history
Both the place where the parser encounters a needed closed delimiter and
the unclosed opening delimiter are important, so they should get the
same level of highlighting in the output.
  • Loading branch information
estebank committed Aug 27, 2021
1 parent 4a6547c commit c6d800d
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 67 deletions.
12 changes: 11 additions & 1 deletion compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,12 +1432,22 @@ impl<'a> Parser<'a> {
// the most sense, which is immediately after the last token:
//
// {foo(bar {}}
// - ^
// ^ ^
// | |
// | help: `)` may belong here
// |
// unclosed delimiter
if let Some(sp) = unmatched.unclosed_span {
let mut primary_span: Vec<Span> =
err.span.primary_spans().iter().cloned().collect();
primary_span.push(sp);
let mut primary_span: MultiSpan = primary_span.into();
for span_label in err.span.span_labels() {
if let Some(label) = span_label.label {
primary_span.push_span_label(span_label.span, label);
}
}
err.set_span(primary_span);
err.span_label(sp, "unclosed delimiter");
}
// Backticks should be removed to apply suggestions.
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use rustc_data_structures::sync::Lrc;
use rustc_errors::PResult;
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, FatalError};
use rustc_session::parse::ParseSess;
use rustc_span::source_map::{Span, DUMMY_SP};
use rustc_span::source_map::{MultiSpan, Span, DUMMY_SP};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use tracing::debug;

Expand Down Expand Up @@ -1335,8 +1335,13 @@ crate fn make_unclosed_delims_error(
// `None` here means an `Eof` was found. We already emit those errors elsewhere, we add them to
// `unmatched_braces` only for error recovery in the `Parser`.
let found_delim = unmatched.found_delim?;
let span: MultiSpan = if let Some(sp) = unmatched.unclosed_span {
vec![unmatched.found_span, sp].into()
} else {
unmatched.found_span.into()
};
let mut err = sess.span_diagnostic.struct_span_err(
unmatched.found_span,
span,
&format!(
"mismatched closing delimiter: `{}`",
pprust::token_kind_to_string(&token::CloseDelim(found_delim)),
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-10636-1.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: mismatched closing delimiter: `)`
--> $DIR/issue-10636-1.rs:4:1
--> $DIR/issue-10636-1.rs:1:12
|
LL | struct Obj {
| - unclosed delimiter
| ^ unclosed delimiter
...
LL | )
| ^ mismatched closing delimiter
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-10636-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
--> $DIR/issue-10636-2.rs:5:25
--> $DIR/issue-10636-2.rs:5:15
|
LL | option.map(|some| 42;
| - ^ help: `)` may belong here
| ^ ^ help: `)` may belong here
| |
| unclosed delimiter

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-58856-1.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: expected one of `)`, `,`, or `:`, found `>`
--> $DIR/issue-58856-1.rs:3:14
--> $DIR/issue-58856-1.rs:3:9
|
LL | fn b(self>
| - ^ help: `)` may belong here
| ^ ^ help: `)` may belong here
| |
| unclosed delimiter

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-58856-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: expected one of `)` or `,`, found `->`
--> $DIR/issue-58856-2.rs:6:26
--> $DIR/issue-58856-2.rs:6:19
|
LL | fn how_are_you(&self -> Empty {
| - -^^
| ^ -^^
| | |
| | help: `)` may belong here
| unclosed delimiter
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-60075.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ LL | }
| - item list ends here

error: mismatched closing delimiter: `)`
--> $DIR/issue-60075.rs:6:10
--> $DIR/issue-60075.rs:4:31
|
LL | fn qux() -> Option<usize> {
| - unclosed delimiter
| ^ unclosed delimiter
LL | let _ = if true {
LL | });
| ^ mismatched closing delimiter
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/parser/issue-62973.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ LL |
| ^

error: expected one of `,` or `}`, found `{`
--> $DIR/issue-62973.rs:6:25
--> $DIR/issue-62973.rs:6:8
|
LL | fn p() { match s { v, E { [) {) }
| - - -^ expected one of `,` or `}`
| ^ - -^ expected one of `,` or `}`
| | | |
| | | help: `}` may belong here
| | while parsing this struct
Expand Down Expand Up @@ -56,18 +56,18 @@ LL |
| ^ expected one of `.`, `?`, `{`, or an operator

error: mismatched closing delimiter: `)`
--> $DIR/issue-62973.rs:6:28
--> $DIR/issue-62973.rs:6:27
|
LL | fn p() { match s { v, E { [) {) }
| -^ mismatched closing delimiter
| ^^ mismatched closing delimiter
| |
| unclosed delimiter

error: mismatched closing delimiter: `)`
--> $DIR/issue-62973.rs:6:31
--> $DIR/issue-62973.rs:6:30
|
LL | fn p() { match s { v, E { [) {) }
| -^ mismatched closing delimiter
| ^^ mismatched closing delimiter
| |
| unclosed delimiter

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-63116.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ LL | impl W <s(f;Y(;]
| ^ expected one of 7 possible tokens

error: mismatched closing delimiter: `]`
--> $DIR/issue-63116.rs:3:16
--> $DIR/issue-63116.rs:3:14
|
LL | impl W <s(f;Y(;]
| - ^ mismatched closing delimiter
| ^ ^ mismatched closing delimiter
| |
| unclosed delimiter

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-66357-unexpected-unreachable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ LL | fn f() { |[](* }
| ^ expected one of `,` or `:`

error: expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `mut`, `ref`, `|`, identifier, or path, found `*`
--> $DIR/issue-66357-unexpected-unreachable.rs:12:14
--> $DIR/issue-66357-unexpected-unreachable.rs:12:13
|
LL | fn f() { |[](* }
| -^ help: `)` may belong here
| ^^ help: `)` may belong here
| |
| unclosed delimiter

Expand Down
Original file line number Diff line number Diff line change
@@ -1,107 +1,107 @@
error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:42
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:27
|
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:15:36
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:15:24
|
LL | V = [Vec::new; { [].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:26:36
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:26:24
|
LL | V = [Vec::new; { [0].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:42
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:27
|
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:15:36
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:15:24
|
LL | V = [Vec::new; { [].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:26:36
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:26:24
|
LL | V = [Vec::new; { [0].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:42
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:27
|
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:15:36
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:15:24
|
LL | V = [Vec::new; { [].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:26:36
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:26:24
|
LL | V = [Vec::new; { [0].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:42
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:27
|
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:15:36
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:15:24
|
LL | V = [Vec::new; { [].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this

error: mismatched closing delimiter: `]`
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:26:36
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:26:24
|
LL | V = [Vec::new; { [0].len() ].len() as isize,
| - - ^ mismatched closing delimiter
| - ^ ^ mismatched closing delimiter
| | |
| | unclosed delimiter
| closing delimiter possibly meant for this
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/macro-mismatched-delim-brace-paren.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: mismatched closing delimiter: `)`
--> $DIR/macro-mismatched-delim-brace-paren.rs:6:5
--> $DIR/macro-mismatched-delim-brace-paren.rs:4:10
|
LL | foo! {
| - unclosed delimiter
| ^ unclosed delimiter
LL | bar, "baz", 1, 2.0
LL | )
| ^ mismatched closing delimiter
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/macro-mismatched-delim-paren-brace.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ LL | }
| ^ unexpected closing delimiter

error: mismatched closing delimiter: `}`
--> $DIR/macro-mismatched-delim-paren-brace.rs:4:5
--> $DIR/macro-mismatched-delim-paren-brace.rs:2:10
|
LL | foo! (
| - unclosed delimiter
| ^ unclosed delimiter
LL | bar, "baz", 1, 2.0
LL | }
| ^ mismatched closing delimiter
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/parser-recovery-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ LL | let x = y.;
| ^

error: mismatched closing delimiter: `)`
--> $DIR/parser-recovery-2.rs:6:5
--> $DIR/parser-recovery-2.rs:4:14
|
LL | fn bar() {
| - unclosed delimiter
| ^ unclosed delimiter
LL | let x = foo();
LL | )
| ^ mismatched closing delimiter
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/unclosed-delimiter-in-dep.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: mismatched closing delimiter: `}`
--> $DIR/unclosed_delim_mod.rs:7:1
--> $DIR/unclosed_delim_mod.rs:5:7
|
LL | pub fn new() -> Result<Value, ()> {
| - closing delimiter possibly meant for this
LL | Ok(Value {
| - unclosed delimiter
| ^ unclosed delimiter
LL | }
LL | }
| ^ mismatched closing delimiter
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/unclosed_delim_mod.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: mismatched closing delimiter: `}`
--> $DIR/unclosed_delim_mod.rs:7:1
--> $DIR/unclosed_delim_mod.rs:5:7
|
LL | pub fn new() -> Result<Value, ()> {
| - closing delimiter possibly meant for this
LL | Ok(Value {
| - unclosed delimiter
| ^ unclosed delimiter
LL | }
LL | }
| ^ mismatched closing delimiter
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/use-unclosed-brace.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ LL | fn main() {}
| ^

error: expected one of `,`, `::`, `as`, or `}`, found `;`
--> $DIR/use-unclosed-brace.rs:4:19
--> $DIR/use-unclosed-brace.rs:4:10
|
LL | use foo::{bar, baz;
| - ^
| ^ ^
| | |
| | expected one of `,`, `::`, `as`, or `}`
| | help: `}` may belong here
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/resolve/token-error-correct-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: mismatched closing delimiter: `)`
--> $DIR/token-error-correct-2.rs:6:5
--> $DIR/token-error-correct-2.rs:4:12
|
LL | if foo {
| - unclosed delimiter
| ^ unclosed delimiter
LL |
LL | )
| ^ mismatched closing delimiter
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/resolve/token-error-correct-3.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
--> $DIR/token-error-correct-3.rs:13:35
--> $DIR/token-error-correct-3.rs:13:21
|
LL | callback(path.as_ref();
| - ^ help: `)` may belong here
| ^ ^ help: `)` may belong here
| |
| unclosed delimiter

Expand Down
Loading

0 comments on commit c6d800d

Please sign in to comment.