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.
This commit is contained in:
Jacob Pratt
2025-08-22 22:00:44 -04:00
committed by GitHub
3 changed files with 44 additions and 2 deletions

View File

@@ -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.