Skip to content

Recover param: Ty = EXPR #137396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions compiler/rustc_parse/src/parser/ty.rs
Copy link
Member

@fmease fmease Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second glance, I would think parse_param_general is 'a better fit' for this compared to parse_ty_for_param (which should probably only concern itself with parsing types).

Mostly an organizational nitpick since it shouldn't be able to affect behavior (CC anon params in Rust 2015). I was only able to find trait T { fn f((x)=0 fn) {} } which will emit two errors under this PR in Rust 2015 (parameter defaults are not supported and expected `:`, found `=`) instead of one and would keep emitting a single one if the logic was moved into parse_param_general I think but that's hardly interesting.

nw, r=me then

r=me with or without moving the logic into parse_param_general. Sorry for the churn / wrong signal there.

Copy link
Member

@fmease fmease Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(preexisting: the patterns aren't allowed in methods without bodies parser recovery is fragile already: it suggests the more butchered _: )=0 for trait T { fn f((x)=0); } under Rust 2015)

Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,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, P<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.
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/parser/fn-with-default-expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn foo(x: i32 = 1) {}
//~^ ERROR parameter defaults are not supported

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/parser/fn-with-default-expr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: parameter defaults are not supported
--> $DIR/fn-with-default-expr.rs:1:15
|
LL | fn foo(x: i32 = 1) {}
| ^^^

error: aborting due to 1 previous error

Loading