Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/parser/recover/param-default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn foo(x: i32 = 1) {} //~ ERROR parameter defaults are not supported

type Foo = fn(i32 = 0); //~ ERROR parameter defaults are not supported

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

error: parameter defaults are not supported
--> $DIR/param-default.rs:3:19
|
LL | type Foo = fn(i32 = 0);
| ^^^

error: aborting due to 2 previous errors

Loading