Skip to content

Commit 6caa586

Browse files
compiler-errorsfmease
authored andcommitted
Recover param: Ty = EXPR
1 parent 831e291 commit 6caa586

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,37 @@ impl<'a> Parser<'a> {
137137
/// The difference from `parse_ty` is that this version allows `...`
138138
/// (`CVarArgs`) at the top level of the type.
139139
pub(super) fn parse_ty_for_param(&mut self) -> PResult<'a, Box<Ty>> {
140-
self.parse_ty_common(
140+
let ty = self.parse_ty_common(
141141
AllowPlus::Yes,
142142
AllowCVariadic::Yes,
143143
RecoverQPath::Yes,
144144
RecoverReturnSign::Yes,
145145
None,
146146
RecoverQuestionMark::Yes,
147-
)
147+
)?;
148+
149+
// Recover a trailing `= EXPR` if present.
150+
if self.may_recover()
151+
&& self.check_noexpect(&token::Eq)
152+
&& self.look_ahead(1, |tok| tok.can_begin_expr())
153+
{
154+
let snapshot = self.create_snapshot_for_diagnostic();
155+
self.bump();
156+
let eq_span = self.prev_token.span;
157+
match self.parse_expr() {
158+
Ok(e) => {
159+
self.dcx()
160+
.struct_span_err(eq_span.to(e.span), "parameter defaults are not supported")
161+
.emit();
162+
}
163+
Err(diag) => {
164+
diag.cancel();
165+
self.restore_snapshot(snapshot);
166+
}
167+
}
168+
}
169+
170+
Ok(ty)
148171
}
149172

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

0 commit comments

Comments
 (0)