Skip to content

Commit d627e2a

Browse files
committed
Fix parser ICE when recovering dyn/impl after for<...>
1 parent c9808f8 commit d627e2a

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

compiler/rustc_parse/src/parser/ty.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -304,23 +304,25 @@ impl<'a> Parser<'a> {
304304
if self.may_recover()
305305
&& (self.eat_keyword_noexpect(kw::Impl) || self.eat_keyword_noexpect(kw::Dyn))
306306
{
307-
let kw = self.prev_token.ident().unwrap().0.name;
307+
let kw = self.prev_token.ident().unwrap().0;
308+
let removal_span = kw.span.with_hi(self.token.span.lo());
309+
let path = self.parse_path(PathStyle::Type)?;
310+
let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
311+
let kind =
312+
self.parse_remaining_bounds_path(lifetime_defs, path, lo, parse_plus)?;
308313
let mut err = self.sess.create_err(errors::TransposeDynOrImpl {
309-
span: self.prev_token.span,
310-
kw: kw.as_str(),
314+
span: kw.span,
315+
kw: kw.name.as_str(),
311316
sugg: errors::TransposeDynOrImplSugg {
312-
removal_span: self.prev_token.span.with_hi(self.token.span.lo()),
317+
removal_span,
313318
insertion_span: for_span.shrink_to_lo(),
314-
kw: kw.as_str(),
319+
kw: kw.name.as_str(),
315320
},
316321
});
317-
let path = self.parse_path(PathStyle::Type)?;
318-
let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
319-
let kind =
320-
self.parse_remaining_bounds_path(lifetime_defs, path, lo, parse_plus)?;
322+
321323
// Take the parsed bare trait object and turn it either
322324
// into a `dyn` object or an `impl Trait`.
323-
let kind = match (kind, kw) {
325+
let kind = match (kind, kw.name) {
324326
(TyKind::TraitObject(bounds, _), kw::Dyn) => {
325327
TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn)
326328
}

tests/ui/parser/recover-hrtb-before-dyn-impl-kw.rs

+4
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ fn test(_: &for<'a> dyn Trait) {}
66
fn test2(_: for<'a> impl Trait) {}
77
//~^ ERROR `for<...>` expected after `impl`, not before
88

9+
// Issue #118564
10+
type A2 = dyn<for<> dyn>;
11+
//~^ ERROR expected identifier, found `>`
12+
913
fn main() {}

tests/ui/parser/recover-hrtb-before-dyn-impl-kw.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@ LL - fn test2(_: for<'a> impl Trait) {}
2222
LL + fn test2(_: impl for<'a> Trait) {}
2323
|
2424

25-
error: aborting due to 2 previous errors
25+
error: expected identifier, found `>`
26+
--> $DIR/recover-hrtb-before-dyn-impl-kw.rs:10:24
27+
|
28+
LL | type A2 = dyn<for<> dyn>;
29+
| ^ expected identifier
30+
31+
error: aborting due to 3 previous errors
2632

0 commit comments

Comments
 (0)