From c6d800d85439d0a95a2943905d0fd3842fa4dd20 Mon Sep 17 00:00:00 2001 From: Esteban Kuber Date: Fri, 27 Aug 2021 10:57:28 +0000 Subject: [PATCH] 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. --- .../rustc_parse/src/parser/diagnostics.rs | 12 ++++- compiler/rustc_parse/src/parser/mod.rs | 9 +++- src/test/ui/parser/issue-10636-1.stderr | 4 +- src/test/ui/parser/issue-10636-2.stderr | 4 +- src/test/ui/parser/issue-58856-1.stderr | 4 +- src/test/ui/parser/issue-58856-2.stderr | 4 +- src/test/ui/parser/issue-60075.stderr | 4 +- src/test/ui/parser/issue-62973.stderr | 12 ++--- src/test/ui/parser/issue-63116.stderr | 4 +- .../issue-66357-unexpected-unreachable.stderr | 4 +- ...invalid-syntax-in-enum-discriminant.stderr | 48 +++++++++---------- .../macro-mismatched-delim-brace-paren.stderr | 4 +- .../macro-mismatched-delim-paren-brace.stderr | 4 +- src/test/ui/parser/parser-recovery-2.stderr | 4 +- .../parser/unclosed-delimiter-in-dep.stderr | 4 +- src/test/ui/parser/unclosed_delim_mod.stderr | 4 +- src/test/ui/parser/use-unclosed-brace.stderr | 4 +- .../ui/resolve/token-error-correct-2.stderr | 4 +- .../ui/resolve/token-error-correct-3.stderr | 4 +- .../ui/resolve/token-error-correct-4.stderr | 4 +- .../ui/resolve/token-error-correct.stderr | 4 +- 21 files changed, 82 insertions(+), 67 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 273fbea358021..cdc1d1cf99f3c 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -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 = + 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. diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 4c3c140d17100..64f7a447fea3c 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -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; @@ -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)), diff --git a/src/test/ui/parser/issue-10636-1.stderr b/src/test/ui/parser/issue-10636-1.stderr index ff90cb97096f7..1e6294ebe1691 100644 --- a/src/test/ui/parser/issue-10636-1.stderr +++ b/src/test/ui/parser/issue-10636-1.stderr @@ -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 diff --git a/src/test/ui/parser/issue-10636-2.stderr b/src/test/ui/parser/issue-10636-2.stderr index 5b9a9b7f06c39..d4f2da9e3ab6f 100644 --- a/src/test/ui/parser/issue-10636-2.stderr +++ b/src/test/ui/parser/issue-10636-2.stderr @@ -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 diff --git a/src/test/ui/parser/issue-58856-1.stderr b/src/test/ui/parser/issue-58856-1.stderr index f1abb40ed7a73..2afb26d175834 100644 --- a/src/test/ui/parser/issue-58856-1.stderr +++ b/src/test/ui/parser/issue-58856-1.stderr @@ -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 diff --git a/src/test/ui/parser/issue-58856-2.stderr b/src/test/ui/parser/issue-58856-2.stderr index 303b5eacc3296..627dd389059cd 100644 --- a/src/test/ui/parser/issue-58856-2.stderr +++ b/src/test/ui/parser/issue-58856-2.stderr @@ -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 diff --git a/src/test/ui/parser/issue-60075.stderr b/src/test/ui/parser/issue-60075.stderr index e3b7f4ad420e8..210ef700cd4bc 100644 --- a/src/test/ui/parser/issue-60075.stderr +++ b/src/test/ui/parser/issue-60075.stderr @@ -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 { - | - unclosed delimiter + | ^ unclosed delimiter LL | let _ = if true { LL | }); | ^ mismatched closing delimiter diff --git a/src/test/ui/parser/issue-62973.stderr b/src/test/ui/parser/issue-62973.stderr index 3065d642fe105..51d835e732988 100644 --- a/src/test/ui/parser/issue-62973.stderr +++ b/src/test/ui/parser/issue-62973.stderr @@ -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 @@ -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 diff --git a/src/test/ui/parser/issue-63116.stderr b/src/test/ui/parser/issue-63116.stderr index 4766dfafea1c9..cfdd99d1434ae 100644 --- a/src/test/ui/parser/issue-63116.stderr +++ b/src/test/ui/parser/issue-63116.stderr @@ -13,10 +13,10 @@ LL | impl W $DIR/issue-63116.rs:3:16 + --> $DIR/issue-63116.rs:3:14 | LL | impl W $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 diff --git a/src/test/ui/parser/issue-67377-invalid-syntax-in-enum-discriminant.stderr b/src/test/ui/parser/issue-67377-invalid-syntax-in-enum-discriminant.stderr index f20ec75535354..34f1397ce1d5b 100644 --- a/src/test/ui/parser/issue-67377-invalid-syntax-in-enum-discriminant.stderr +++ b/src/test/ui/parser/issue-67377-invalid-syntax-in-enum-discriminant.stderr @@ -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 diff --git a/src/test/ui/parser/macro-mismatched-delim-brace-paren.stderr b/src/test/ui/parser/macro-mismatched-delim-brace-paren.stderr index 93c5ab383d488..077d318004896 100644 --- a/src/test/ui/parser/macro-mismatched-delim-brace-paren.stderr +++ b/src/test/ui/parser/macro-mismatched-delim-brace-paren.stderr @@ -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 diff --git a/src/test/ui/parser/macro-mismatched-delim-paren-brace.stderr b/src/test/ui/parser/macro-mismatched-delim-paren-brace.stderr index 424c7a60c196f..967a3e6fdc11b 100644 --- a/src/test/ui/parser/macro-mismatched-delim-paren-brace.stderr +++ b/src/test/ui/parser/macro-mismatched-delim-paren-brace.stderr @@ -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 diff --git a/src/test/ui/parser/parser-recovery-2.stderr b/src/test/ui/parser/parser-recovery-2.stderr index cd3da4c71f0b5..0980d033fe73a 100644 --- a/src/test/ui/parser/parser-recovery-2.stderr +++ b/src/test/ui/parser/parser-recovery-2.stderr @@ -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 diff --git a/src/test/ui/parser/unclosed-delimiter-in-dep.stderr b/src/test/ui/parser/unclosed-delimiter-in-dep.stderr index 00861a5a3d49a..1366ef1bba8bf 100644 --- a/src/test/ui/parser/unclosed-delimiter-in-dep.stderr +++ b/src/test/ui/parser/unclosed-delimiter-in-dep.stderr @@ -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 { | - closing delimiter possibly meant for this LL | Ok(Value { - | - unclosed delimiter + | ^ unclosed delimiter LL | } LL | } | ^ mismatched closing delimiter diff --git a/src/test/ui/parser/unclosed_delim_mod.stderr b/src/test/ui/parser/unclosed_delim_mod.stderr index 9c16707212367..a46d020b9672e 100644 --- a/src/test/ui/parser/unclosed_delim_mod.stderr +++ b/src/test/ui/parser/unclosed_delim_mod.stderr @@ -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 { | - closing delimiter possibly meant for this LL | Ok(Value { - | - unclosed delimiter + | ^ unclosed delimiter LL | } LL | } | ^ mismatched closing delimiter diff --git a/src/test/ui/parser/use-unclosed-brace.stderr b/src/test/ui/parser/use-unclosed-brace.stderr index d29a68f821481..438fe9c47eac1 100644 --- a/src/test/ui/parser/use-unclosed-brace.stderr +++ b/src/test/ui/parser/use-unclosed-brace.stderr @@ -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 diff --git a/src/test/ui/resolve/token-error-correct-2.stderr b/src/test/ui/resolve/token-error-correct-2.stderr index 4014af2f41b61..cca9f2dc88ca0 100644 --- a/src/test/ui/resolve/token-error-correct-2.stderr +++ b/src/test/ui/resolve/token-error-correct-2.stderr @@ -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 diff --git a/src/test/ui/resolve/token-error-correct-3.stderr b/src/test/ui/resolve/token-error-correct-3.stderr index 31087e394adfe..77c87c78466b1 100644 --- a/src/test/ui/resolve/token-error-correct-3.stderr +++ b/src/test/ui/resolve/token-error-correct-3.stderr @@ -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 diff --git a/src/test/ui/resolve/token-error-correct-4.stderr b/src/test/ui/resolve/token-error-correct-4.stderr index 64aff54ba7311..81e5a13369159 100644 --- a/src/test/ui/resolve/token-error-correct-4.stderr +++ b/src/test/ui/resolve/token-error-correct-4.stderr @@ -1,8 +1,8 @@ error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;` - --> $DIR/token-error-correct-4.rs:9:21 + --> $DIR/token-error-correct-4.rs:9:12 | LL | setsuna(kazusa(); - | - ^ help: `)` may belong here + | ^ ^ help: `)` may belong here | | | unclosed delimiter diff --git a/src/test/ui/resolve/token-error-correct.stderr b/src/test/ui/resolve/token-error-correct.stderr index bf300ecd78173..ca0c4c18ad4ba 100644 --- a/src/test/ui/resolve/token-error-correct.stderr +++ b/src/test/ui/resolve/token-error-correct.stderr @@ -1,10 +1,10 @@ error: mismatched closing delimiter: `}` - --> $DIR/token-error-correct.rs:6:1 + --> $DIR/token-error-correct.rs:4:12 | LL | fn main() { | - closing delimiter possibly meant for this LL | foo(bar(; - | - unclosed delimiter + | ^ unclosed delimiter LL | LL | } | ^ mismatched closing delimiter