Skip to content

Commit 70a1e44

Browse files
Recover param: Ty = EXPR
1 parent 794c124 commit 70a1e44

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,37 @@ 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+
&& self.check_noexpect(&token::Eq)
147+
&& self.look_ahead(1, |tok| tok.can_begin_expr())
148+
{
149+
let mut snapshot = self.create_snapshot_for_diagnostic();
150+
snapshot.bump();
151+
let eq_span = snapshot.prev_token.span;
152+
match snapshot.parse_expr() {
153+
Ok(e) => {
154+
self.restore_snapshot(snapshot);
155+
self.dcx()
156+
.struct_span_err(eq_span.to(e.span), "parameter defaults are not supported")
157+
.emit();
158+
}
159+
Err(diag) => {
160+
diag.cancel();
161+
}
162+
}
163+
}
164+
165+
Ok(ty)
143166
}
144167

145168
/// 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)