diff options
| author | bors <bors@rust-lang.org> | 2023-02-02 12:01:17 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-02-02 12:01:17 +0000 |
| commit | 97872b792c9dd6a9bc5c3f4e62a0bd5958b09cdc (patch) | |
| tree | ac382c6f916b98074fe5ce756d3778d5d3f0d407 /compiler/rustc_parse/src/parser/diagnostics.rs | |
| parent | a9985cf172e7cb8ab5c58ce2818752c3572754fc (diff) | |
| parent | 39db65c526ae3b97f0ee90642242c8c07865707e (diff) | |
| download | rust-97872b792c9dd6a9bc5c3f4e62a0bd5958b09cdc.tar.gz rust-97872b792c9dd6a9bc5c3f4e62a0bd5958b09cdc.zip | |
Auto merge of #107478 - compiler-errors:anon-enum-tys-are-ambiguous, r=estebank
Revert "Teach parser to understand fake anonymous enum syntax" and related commits anonymous enum types are currently ambiguous in positions like: * `|` operator: `a as fn() -> B | C` * closure args: `|_: as fn() -> A | B` I first tried to thread around `RecoverAnonEnum` into all these positions, but the resulting complexity in the compiler is IMO not worth it, or at least worth a bit more thinking time. In the mean time, let's revert this syntax for now, so we can go back to the drawing board. Fixes #107461 cc: `@estebank` `@cjgillot` #106960 --- ### Squashed revert commits: Revert "review comment: Remove AST AnonTy" This reverts commit 020cca8d36cb678e3ddc2ead41364be314d19e93. Revert "Ensure macros are not affected" This reverts commit 12d18e403139eeeeb339e8611b2bed4910864edb. Revert "Emit fewer errors on patterns with possible type ascription" This reverts commit c847a01a3b1f620c4fdb98c75805033e768975d1. Revert "Teach parser to understand fake anonymous enum syntax" This reverts commit 2d824206655bfb26cb5eed43490ee396542b153e.
Diffstat (limited to 'compiler/rustc_parse/src/parser/diagnostics.rs')
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 52 |
1 files changed, 13 insertions, 39 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 26274822ed8..16d5edfd303 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -2394,7 +2394,7 @@ impl<'a> Parser<'a> { /// Some special error handling for the "top-level" patterns in a match arm, /// `for` loop, `let`, &c. (in contrast to subpatterns within such). - pub(crate) fn maybe_recover_colon_colon_in_pat_typo_or_anon_enum( + pub(crate) fn maybe_recover_colon_colon_in_pat_typo( &mut self, mut first_pat: P<Pat>, expected: Option<Expected>, @@ -2405,41 +2405,26 @@ impl<'a> Parser<'a> { if !matches!(first_pat.kind, PatKind::Ident(_, _, None) | PatKind::Path(..)) || !self.look_ahead(1, |token| token.is_ident() && !token.is_reserved_ident()) { - let mut snapshot_type = self.create_snapshot_for_diagnostic(); - snapshot_type.bump(); // `:` - match snapshot_type.parse_ty() { - Err(inner_err) => { - inner_err.cancel(); - } - Ok(ty) => { - let Err(mut err) = self.expected_one_of_not_found(&[], &[]) else { - return first_pat; - }; - err.span_label(ty.span, "specifying the type of a pattern isn't supported"); - self.restore_snapshot(snapshot_type); - let span = first_pat.span.to(ty.span); - first_pat = self.mk_pat(span, PatKind::Wild); - err.emit(); - } - } return first_pat; } // The pattern looks like it might be a path with a `::` -> `:` typo: // `match foo { bar:baz => {} }` - let colon_span = self.token.span; + let span = self.token.span; // We only emit "unexpected `:`" error here if we can successfully parse the // whole pattern correctly in that case. - let mut snapshot_pat = self.create_snapshot_for_diagnostic(); - let mut snapshot_type = self.create_snapshot_for_diagnostic(); + let snapshot = self.create_snapshot_for_diagnostic(); // Create error for "unexpected `:`". match self.expected_one_of_not_found(&[], &[]) { Err(mut err) => { - snapshot_pat.bump(); // Skip the `:`. - snapshot_type.bump(); // Skip the `:`. - match snapshot_pat.parse_pat_no_top_alt(expected) { + self.bump(); // Skip the `:`. + match self.parse_pat_no_top_alt(expected) { Err(inner_err) => { + // Carry on as if we had not done anything, callers will emit a + // reasonable error. inner_err.cancel(); + err.cancel(); + self.restore_snapshot(snapshot); } Ok(mut pat) => { // We've parsed the rest of the pattern. @@ -2503,8 +2488,8 @@ impl<'a> Parser<'a> { _ => {} } if show_sugg { - err.span_suggestion_verbose( - colon_span.until(self.look_ahead(1, |t| t.span)), + err.span_suggestion( + span, "maybe write a path separator here", "::", Applicability::MaybeIncorrect, @@ -2512,24 +2497,13 @@ impl<'a> Parser<'a> { } else { first_pat = self.mk_pat(new_span, PatKind::Wild); } - self.restore_snapshot(snapshot_pat); + err.emit(); } } - match snapshot_type.parse_ty() { - Err(inner_err) => { - inner_err.cancel(); - } - Ok(ty) => { - err.span_label(ty.span, "specifying the type of a pattern isn't supported"); - self.restore_snapshot(snapshot_type); - let new_span = first_pat.span.to(ty.span); - first_pat = self.mk_pat(new_span, PatKind::Wild); - } - } - err.emit(); } _ => { // Carry on as if we had not done anything. This should be unreachable. + self.restore_snapshot(snapshot); } }; first_pat |
