diff options
| author | bors <bors@rust-lang.org> | 2024-03-11 16:51:54 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-03-11 16:51:54 +0000 |
| commit | 6554a5645a13e4d9331fd028960d69be91d7492d (patch) | |
| tree | 99769b8987cd4046411fa0e9e0f2a08e4fa45941 /compiler/rustc_parse/src | |
| parent | 65cd843ae06ad00123c131a431ed5304e4cd577a (diff) | |
| parent | 7fa6fa4a1f5d48ac46e22756b27cb56f2aebf02d (diff) | |
| download | rust-6554a5645a13e4d9331fd028960d69be91d7492d.tar.gz rust-6554a5645a13e4d9331fd028960d69be91d7492d.zip | |
Auto merge of #122338 - workingjubilee:rollup-xzpt4v4, r=workingjubilee
Rollup of 15 pull requests Successful merges: - #116791 (Allow codegen backends to opt-out of parallel codegen) - #116793 (Allow targets to override default codegen backend) - #117458 (LLVM Bitcode Linker: A self contained linker for nvptx and other targets) - #119385 (Fix type resolution of associated const equality bounds (take 2)) - #121438 (std support for wasm32 panic=unwind) - #121893 (Add tests (and a bit of cleanup) for interior mut handling in promotion and const-checking) - #122080 (Clarity improvements to `DropTree`) - #122152 (Improve diagnostics for parenthesized type arguments) - #122166 (Remove the unused `field_remapping` field from `TypeLowering`) - #122249 (interpret: do not call machine read hooks during validation) - #122299 (Store backtrace for `must_produce_diag`) - #122318 (Revision-related tweaks for next-solver tests) - #122320 (Use ptradd for vtable indexing) - #122328 (unix_sigpipe: Replace `inherit` with `sig_dfl` in syntax tests) - #122330 (bootstrap readme: fix, improve, update) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/parser/path.rs | 86 |
1 files changed, 84 insertions, 2 deletions
diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 545db5138a3..2edf2111de7 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -1,6 +1,7 @@ use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign}; use super::{Parser, Restrictions, TokenType}; use crate::errors::PathSingleColon; +use crate::parser::{CommaRecoveryMode, RecoverColon, RecoverComma}; use crate::{errors, maybe_whole}; use ast::token::IdentIsRaw; use rustc_ast::ptr::P; @@ -10,7 +11,7 @@ use rustc_ast::{ AssocConstraintKind, BlockCheckMode, GenericArg, GenericArgs, Generics, ParenthesizedArgs, Path, PathSegment, QSelf, }; -use rustc_errors::{Applicability, PResult}; +use rustc_errors::{Applicability, Diag, PResult}; use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::{BytePos, Span}; use std::mem; @@ -373,7 +374,38 @@ impl<'a> Parser<'a> { .into() } else { // `(T, U) -> R` - let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?; + + let prev_token_before_parsing = self.prev_token.clone(); + let token_before_parsing = self.token.clone(); + let mut snapshot = None; + if self.may_recover() + && prev_token_before_parsing.kind == token::ModSep + && (style == PathStyle::Expr && self.token.can_begin_expr() + || style == PathStyle::Pat && self.token.can_begin_pattern()) + { + snapshot = Some(self.create_snapshot_for_diagnostic()); + } + + let (inputs, _) = match self.parse_paren_comma_seq(|p| p.parse_ty()) { + Ok(output) => output, + Err(mut error) if prev_token_before_parsing.kind == token::ModSep => { + error.span_label( + prev_token_before_parsing.span.to(token_before_parsing.span), + "while parsing this parenthesized list of type arguments starting here", + ); + + if let Some(mut snapshot) = snapshot { + snapshot.recover_fn_call_leading_path_sep( + style, + prev_token_before_parsing, + &mut error, + ) + } + + return Err(error); + } + Err(error) => return Err(error), + }; let inputs_span = lo.to(self.prev_token.span); let output = self.parse_ret_ty(AllowPlus::No, RecoverQPath::No, RecoverReturnSign::No)?; @@ -399,6 +431,56 @@ impl<'a> Parser<'a> { } } + /// Recover `$path::(...)` as `$path(...)`. + /// + /// ```ignore (diagnostics) + /// foo::(420, "bar") + /// ^^ remove extra separator to make the function call + /// // or + /// match x { + /// Foo::(420, "bar") => { ... }, + /// ^^ remove extra separator to turn this into tuple struct pattern + /// _ => { ... }, + /// } + /// ``` + fn recover_fn_call_leading_path_sep( + &mut self, + style: PathStyle, + prev_token_before_parsing: Token, + error: &mut Diag<'_>, + ) { + if ((style == PathStyle::Expr && self.parse_paren_comma_seq(|p| p.parse_expr()).is_ok()) + || (style == PathStyle::Pat + && self + .parse_paren_comma_seq(|p| { + p.parse_pat_allow_top_alt( + None, + RecoverComma::No, + RecoverColon::No, + CommaRecoveryMode::LikelyTuple, + ) + }) + .is_ok())) + && !matches!(self.token.kind, token::ModSep | token::RArrow) + { + error.span_suggestion_verbose( + prev_token_before_parsing.span, + format!( + "consider removing the `::` here to {}", + match style { + PathStyle::Expr => "call the expression", + PathStyle::Pat => "turn this into a tuple struct pattern", + _ => { + return; + } + } + ), + "", + Applicability::MaybeIncorrect, + ); + } + } + /// Parses generic args (within a path segment) with recovery for extra leading angle brackets. /// For the purposes of understanding the parsing logic of generic arguments, this function /// can be thought of being the same as just calling `self.parse_angle_args()` if the source |
