@@ -44,6 +44,11 @@ pub(super) enum RecoverQPath {
44
44
No ,
45
45
}
46
46
47
+ pub ( super ) enum IsAsCast {
48
+ Yes ,
49
+ No ,
50
+ }
51
+
47
52
/// Signals whether parsing a type should recover `->`.
48
53
///
49
54
/// More specifically, when parsing a function like:
@@ -100,6 +105,7 @@ impl<'a> Parser<'a> {
100
105
RecoverQPath :: Yes ,
101
106
RecoverReturnSign :: Yes ,
102
107
None ,
108
+ IsAsCast :: No ,
103
109
)
104
110
}
105
111
@@ -113,6 +119,7 @@ impl<'a> Parser<'a> {
113
119
RecoverQPath :: Yes ,
114
120
RecoverReturnSign :: Yes ,
115
121
Some ( ty_params) ,
122
+ IsAsCast :: No ,
116
123
)
117
124
}
118
125
@@ -126,6 +133,7 @@ impl<'a> Parser<'a> {
126
133
RecoverQPath :: Yes ,
127
134
RecoverReturnSign :: Yes ,
128
135
None ,
136
+ IsAsCast :: No ,
129
137
)
130
138
}
131
139
@@ -142,9 +150,22 @@ impl<'a> Parser<'a> {
142
150
RecoverQPath :: Yes ,
143
151
RecoverReturnSign :: Yes ,
144
152
None ,
153
+ IsAsCast :: No ,
145
154
)
146
155
}
147
156
157
+ /// Parses a type following an `as` cast. Similar to `parse_ty_no_plus`, but signaling origin
158
+ /// for better diagnostics involving `?`.
159
+ pub ( super ) fn parse_as_cast_ty ( & mut self ) -> PResult < ' a , P < Ty > > {
160
+ self . parse_ty_common (
161
+ AllowPlus :: No ,
162
+ AllowCVariadic :: No ,
163
+ RecoverQPath :: Yes ,
164
+ RecoverReturnSign :: Yes ,
165
+ None ,
166
+ IsAsCast :: Yes ,
167
+ )
168
+ }
148
169
/// Parse a type without recovering `:` as `->` to avoid breaking code such as `where fn() : for<'a>`
149
170
pub ( super ) fn parse_ty_for_where_clause ( & mut self ) -> PResult < ' a , P < Ty > > {
150
171
self . parse_ty_common (
@@ -153,6 +174,7 @@ impl<'a> Parser<'a> {
153
174
RecoverQPath :: Yes ,
154
175
RecoverReturnSign :: OnlyFatArrow ,
155
176
None ,
177
+ IsAsCast :: No ,
156
178
)
157
179
}
158
180
@@ -171,6 +193,7 @@ impl<'a> Parser<'a> {
171
193
recover_qpath,
172
194
recover_return_sign,
173
195
None ,
196
+ IsAsCast :: No ,
174
197
) ?;
175
198
FnRetTy :: Ty ( ty)
176
199
} else if recover_return_sign. can_recover ( & self . token . kind ) {
@@ -191,6 +214,7 @@ impl<'a> Parser<'a> {
191
214
recover_qpath,
192
215
recover_return_sign,
193
216
None ,
217
+ IsAsCast :: No ,
194
218
) ?;
195
219
FnRetTy :: Ty ( ty)
196
220
} else {
@@ -205,6 +229,7 @@ impl<'a> Parser<'a> {
205
229
recover_qpath : RecoverQPath ,
206
230
recover_return_sign : RecoverReturnSign ,
207
231
ty_generics : Option < & Generics > ,
232
+ is_as_cast : IsAsCast ,
208
233
) -> PResult < ' a , P < Ty > > {
209
234
let allow_qpath_recovery = recover_qpath == RecoverQPath :: Yes ;
210
235
maybe_recover_from_interpolated_ty_qpath ! ( self , allow_qpath_recovery) ;
@@ -280,6 +305,7 @@ impl<'a> Parser<'a> {
280
305
// Try to recover from use of `+` with incorrect priority.
281
306
self . maybe_report_ambiguous_plus ( allow_plus, impl_dyn_multi, & ty) ;
282
307
self . maybe_recover_from_bad_type_plus ( allow_plus, & ty) ?;
308
+ let ty = self . maybe_recover_from_question_mark ( ty, is_as_cast) ;
283
309
self . maybe_recover_from_bad_qpath ( ty, allow_qpath_recovery)
284
310
}
285
311
0 commit comments