Skip to content

Commit a4e82fd

Browse files
Recover param: Ty = EXPR
1 parent 28b83ee commit a4e82fd

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,38 @@ impl<'a> Parser<'a> {
132132
/// The difference from `parse_ty` is that this version allows `...`
133133
/// (`CVarArgs`) at the top level of the type.
134134
pub(super) fn parse_ty_for_param(&mut self) -> PResult<'a, P<Ty>> {
135-
self.parse_ty_common(
135+
let ty = self.parse_ty_common(
136136
AllowPlus::Yes,
137137
AllowCVariadic::Yes,
138138
RecoverQPath::Yes,
139139
RecoverReturnSign::Yes,
140140
None,
141141
RecoverQuestionMark::Yes,
142-
)
142+
)?;
143+
144+
// Recover a trailing `= EXPR` if present.
145+
if self.may_recover() {
146+
let snapshot = self.create_snapshot_for_diagnostic();
147+
if self.eat_noexpect(&token::Eq) {
148+
let eq_span = self.prev_token.span;
149+
match self.parse_expr() {
150+
Ok(e) => {
151+
self.dcx()
152+
.struct_span_err(
153+
eq_span.to(e.span),
154+
"parameter defaults are not supported",
155+
)
156+
.emit();
157+
}
158+
Err(diag) => {
159+
diag.cancel();
160+
self.restore_snapshot(snapshot);
161+
}
162+
}
163+
}
164+
}
165+
166+
Ok(ty)
143167
}
144168

145169
/// Parses a type in restricted contexts where `+` is not permitted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn foo(x: i32 = 1) {}
2+
//~^ ERROR parameter defaults are not supported
3+
4+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: parameter defaults are not supported
2+
--> $DIR/fn-with-default-expr.rs:1:15
3+
|
4+
LL | fn foo(x: i32 = 1) {}
5+
| ^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)