diff options
| author | Jacob Pratt <jacob@jhpratt.dev> | 2025-08-22 22:00:44 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-22 22:00:44 -0400 |
| commit | 85c9af57f49a8d5e6f09846592677666b76c3581 (patch) | |
| tree | 98784560e01f854335c31dac88b8ed0a0d2fbc4d /compiler/rustc_parse/src | |
| parent | dbc38eed1dc7fba0a6c3d6c3edc4ec1f8fa88099 (diff) | |
| parent | 6caa586f572429fd08a5df7283a97666d898d043 (diff) | |
| download | rust-85c9af57f49a8d5e6f09846592677666b76c3581.tar.gz rust-85c9af57f49a8d5e6f09846592677666b76c3581.zip | |
Rollup merge of #137396 - compiler-errors:param-default, r=fmease
Recover `param: Ty = EXPR` Fixes #137310 Pretty basic recovery here, but better than giving an unexpected token error.
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/parser/ty.rs | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 899a43955ab..94f381ee051 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -137,14 +137,37 @@ impl<'a> Parser<'a> { /// The difference from `parse_ty` is that this version allows `...` /// (`CVarArgs`) at the top level of the type. pub(super) fn parse_ty_for_param(&mut self) -> PResult<'a, Box<Ty>> { - self.parse_ty_common( + let ty = self.parse_ty_common( AllowPlus::Yes, AllowCVariadic::Yes, RecoverQPath::Yes, RecoverReturnSign::Yes, None, RecoverQuestionMark::Yes, - ) + )?; + + // Recover a trailing `= EXPR` if present. + if self.may_recover() + && self.check_noexpect(&token::Eq) + && self.look_ahead(1, |tok| tok.can_begin_expr()) + { + let snapshot = self.create_snapshot_for_diagnostic(); + self.bump(); + let eq_span = self.prev_token.span; + match self.parse_expr() { + Ok(e) => { + self.dcx() + .struct_span_err(eq_span.to(e.span), "parameter defaults are not supported") + .emit(); + } + Err(diag) => { + diag.cancel(); + self.restore_snapshot(snapshot); + } + } + } + + Ok(ty) } /// Parses a type in restricted contexts where `+` is not permitted. |
