File tree Expand file tree Collapse file tree 3 files changed +37
-2
lines changed
compiler/rustc_parse/src/parser Expand file tree Collapse file tree 3 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -132,14 +132,37 @@ impl<'a> Parser<'a> {
132
132
/// The difference from `parse_ty` is that this version allows `...`
133
133
/// (`CVarArgs`) at the top level of the type.
134
134
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 (
136
136
AllowPlus :: Yes ,
137
137
AllowCVariadic :: Yes ,
138
138
RecoverQPath :: Yes ,
139
139
RecoverReturnSign :: Yes ,
140
140
None ,
141
141
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)
143
166
}
144
167
145
168
/// Parses a type in restricted contexts where `+` is not permitted.
Original file line number Diff line number Diff line change
1
+ fn foo ( x : i32 = 1 ) { }
2
+ //~^ ERROR parameter defaults are not supported
3
+
4
+ fn main ( ) { }
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments