Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parser ICE when recovering dyn/impl after for<...> #118585

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,23 +304,25 @@ impl<'a> Parser<'a> {
if self.may_recover()
&& (self.eat_keyword_noexpect(kw::Impl) || self.eat_keyword_noexpect(kw::Dyn))
{
let kw = self.prev_token.ident().unwrap().0.name;
let kw = self.prev_token.ident().unwrap().0;
let removal_span = kw.span.with_hi(self.token.span.lo());
let path = self.parse_path(PathStyle::Type)?;
let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
let kind =
self.parse_remaining_bounds_path(lifetime_defs, path, lo, parse_plus)?;
let mut err = self.sess.create_err(errors::TransposeDynOrImpl {
span: self.prev_token.span,
kw: kw.as_str(),
span: kw.span,
kw: kw.name.as_str(),
sugg: errors::TransposeDynOrImplSugg {
removal_span: self.prev_token.span.with_hi(self.token.span.lo()),
removal_span,
insertion_span: for_span.shrink_to_lo(),
kw: kw.as_str(),
kw: kw.name.as_str(),
},
});
let path = self.parse_path(PathStyle::Type)?;
let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
let kind =
self.parse_remaining_bounds_path(lifetime_defs, path, lo, parse_plus)?;

// Take the parsed bare trait object and turn it either
// into a `dyn` object or an `impl Trait`.
let kind = match (kind, kw) {
let kind = match (kind, kw.name) {
(TyKind::TraitObject(bounds, _), kw::Dyn) => {
TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn)
}
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/parser/recover-hrtb-before-dyn-impl-kw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ fn test(_: &for<'a> dyn Trait) {}
fn test2(_: for<'a> impl Trait) {}
//~^ ERROR `for<...>` expected after `impl`, not before

// Issue #118564
type A2 = dyn<for<> dyn>;
//~^ ERROR expected identifier, found `>`

fn main() {}
8 changes: 7 additions & 1 deletion tests/ui/parser/recover-hrtb-before-dyn-impl-kw.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ LL - fn test2(_: for<'a> impl Trait) {}
LL + fn test2(_: impl for<'a> Trait) {}
|

error: aborting due to 2 previous errors
error: expected identifier, found `>`
--> $DIR/recover-hrtb-before-dyn-impl-kw.rs:10:24
|
LL | type A2 = dyn<for<> dyn>;
| ^ expected identifier

error: aborting due to 3 previous errors