Skip to content

Commit 29d8fb7

Browse files
committed
Auto merge of #88386 - estebank:unmatched-delims, r=jackh726
Point at unclosed delimiters as part of the primary MultiSpan 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. _Context: https://twitter.com/mwk4/status/1430631546432675840_
2 parents 97f2698 + c6d800d commit 29d8fb7

21 files changed

+82
-67
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,12 +1432,22 @@ impl<'a> Parser<'a> {
14321432
// the most sense, which is immediately after the last token:
14331433
//
14341434
// {foo(bar {}}
1435-
// - ^
1435+
// ^ ^
14361436
// | |
14371437
// | help: `)` may belong here
14381438
// |
14391439
// unclosed delimiter
14401440
if let Some(sp) = unmatched.unclosed_span {
1441+
let mut primary_span: Vec<Span> =
1442+
err.span.primary_spans().iter().cloned().collect();
1443+
primary_span.push(sp);
1444+
let mut primary_span: MultiSpan = primary_span.into();
1445+
for span_label in err.span.span_labels() {
1446+
if let Some(label) = span_label.label {
1447+
primary_span.push_span_label(span_label.span, label);
1448+
}
1449+
}
1450+
err.set_span(primary_span);
14411451
err.span_label(sp, "unclosed delimiter");
14421452
}
14431453
// Backticks should be removed to apply suggestions.

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use rustc_data_structures::sync::Lrc;
3333
use rustc_errors::PResult;
3434
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, FatalError};
3535
use rustc_session::parse::ParseSess;
36-
use rustc_span::source_map::{Span, DUMMY_SP};
36+
use rustc_span::source_map::{MultiSpan, Span, DUMMY_SP};
3737
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3838
use tracing::debug;
3939

@@ -1335,8 +1335,13 @@ crate fn make_unclosed_delims_error(
13351335
// `None` here means an `Eof` was found. We already emit those errors elsewhere, we add them to
13361336
// `unmatched_braces` only for error recovery in the `Parser`.
13371337
let found_delim = unmatched.found_delim?;
1338+
let span: MultiSpan = if let Some(sp) = unmatched.unclosed_span {
1339+
vec![unmatched.found_span, sp].into()
1340+
} else {
1341+
unmatched.found_span.into()
1342+
};
13381343
let mut err = sess.span_diagnostic.struct_span_err(
1339-
unmatched.found_span,
1344+
span,
13401345
&format!(
13411346
"mismatched closing delimiter: `{}`",
13421347
pprust::token_kind_to_string(&token::CloseDelim(found_delim)),

src/test/ui/parser/issue-10636-1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: mismatched closing delimiter: `)`
2-
--> $DIR/issue-10636-1.rs:4:1
2+
--> $DIR/issue-10636-1.rs:1:12
33
|
44
LL | struct Obj {
5-
| - unclosed delimiter
5+
| ^ unclosed delimiter
66
...
77
LL | )
88
| ^ mismatched closing delimiter

src/test/ui/parser/issue-10636-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
2-
--> $DIR/issue-10636-2.rs:5:25
2+
--> $DIR/issue-10636-2.rs:5:15
33
|
44
LL | option.map(|some| 42;
5-
| - ^ help: `)` may belong here
5+
| ^ ^ help: `)` may belong here
66
| |
77
| unclosed delimiter
88

src/test/ui/parser/issue-58856-1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: expected one of `)`, `,`, or `:`, found `>`
2-
--> $DIR/issue-58856-1.rs:3:14
2+
--> $DIR/issue-58856-1.rs:3:9
33
|
44
LL | fn b(self>
5-
| - ^ help: `)` may belong here
5+
| ^ ^ help: `)` may belong here
66
| |
77
| unclosed delimiter
88

src/test/ui/parser/issue-58856-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: expected one of `)` or `,`, found `->`
2-
--> $DIR/issue-58856-2.rs:6:26
2+
--> $DIR/issue-58856-2.rs:6:19
33
|
44
LL | fn how_are_you(&self -> Empty {
5-
| - -^^
5+
| ^ -^^
66
| | |
77
| | help: `)` may belong here
88
| unclosed delimiter

src/test/ui/parser/issue-60075.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ LL | }
1717
| - item list ends here
1818

1919
error: mismatched closing delimiter: `)`
20-
--> $DIR/issue-60075.rs:6:10
20+
--> $DIR/issue-60075.rs:4:31
2121
|
2222
LL | fn qux() -> Option<usize> {
23-
| - unclosed delimiter
23+
| ^ unclosed delimiter
2424
LL | let _ = if true {
2525
LL | });
2626
| ^ mismatched closing delimiter

src/test/ui/parser/issue-62973.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ LL |
2121
| ^
2222

2323
error: expected one of `,` or `}`, found `{`
24-
--> $DIR/issue-62973.rs:6:25
24+
--> $DIR/issue-62973.rs:6:8
2525
|
2626
LL | fn p() { match s { v, E { [) {) }
27-
| - - -^ expected one of `,` or `}`
27+
| ^ - -^ expected one of `,` or `}`
2828
| | | |
2929
| | | help: `}` may belong here
3030
| | while parsing this struct
@@ -56,18 +56,18 @@ LL |
5656
| ^ expected one of `.`, `?`, `{`, or an operator
5757

5858
error: mismatched closing delimiter: `)`
59-
--> $DIR/issue-62973.rs:6:28
59+
--> $DIR/issue-62973.rs:6:27
6060
|
6161
LL | fn p() { match s { v, E { [) {) }
62-
| -^ mismatched closing delimiter
62+
| ^^ mismatched closing delimiter
6363
| |
6464
| unclosed delimiter
6565

6666
error: mismatched closing delimiter: `)`
67-
--> $DIR/issue-62973.rs:6:31
67+
--> $DIR/issue-62973.rs:6:30
6868
|
6969
LL | fn p() { match s { v, E { [) {) }
70-
| -^ mismatched closing delimiter
70+
| ^^ mismatched closing delimiter
7171
| |
7272
| unclosed delimiter
7373

src/test/ui/parser/issue-63116.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ LL | impl W <s(f;Y(;]
1313
| ^ expected one of 7 possible tokens
1414

1515
error: mismatched closing delimiter: `]`
16-
--> $DIR/issue-63116.rs:3:16
16+
--> $DIR/issue-63116.rs:3:14
1717
|
1818
LL | impl W <s(f;Y(;]
19-
| - ^ mismatched closing delimiter
19+
| ^ ^ mismatched closing delimiter
2020
| |
2121
| unclosed delimiter
2222

src/test/ui/parser/issue-66357-unexpected-unreachable.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | fn f() { |[](* }
55
| ^ expected one of `,` or `:`
66

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

0 commit comments

Comments
 (0)