Recover param: Ty = EXPR

This commit is contained in:
Michael Goulet
2025-02-21 17:39:49 +00:00
committed by León Orell Valerian Liehr
parent 831e291d3b
commit 6caa586f57
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.