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

Rollup of 6 pull requests #102867

Merged
merged 18 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
36 changes: 34 additions & 2 deletions compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::{ForceCollect, Parser, TrailingToken};

use rustc_ast::token;
use rustc_ast::{self as ast, AttrVec, GenericBounds, GenericParam, GenericParamKind, WhereClause};
use rustc_ast::{
self as ast, AttrVec, GenericBounds, GenericParam, GenericParamKind, TyKind, WhereClause,
};
use rustc_errors::{Applicability, PResult};
use rustc_span::symbol::kw;

Expand Down Expand Up @@ -31,13 +33,43 @@ impl<'a> Parser<'a> {
let mut colon_span = None;
let bounds = if self.eat(&token::Colon) {
colon_span = Some(self.prev_token.span);
// recover from `impl Trait` in type param bound
if self.token.is_keyword(kw::Impl) {
let impl_span = self.token.span;
let snapshot = self.create_snapshot_for_diagnostic();
match self.parse_ty() {
Ok(p) => {
if let TyKind::ImplTrait(_, bounds) = &(*p).kind {
let span = impl_span.to(self.token.span.shrink_to_lo());
let mut err = self.struct_span_err(
span,
"expected trait bound, found `impl Trait` type",
);
err.span_label(span, "not a trait");
if let [bound, ..] = &bounds[..] {
err.span_suggestion_verbose(
impl_span.until(bound.span()),
"use the trait bounds directly",
String::new(),
Applicability::MachineApplicable,
);
}
err.emit();
return Err(err);
}
}
Err(err) => {
err.cancel();
}
}
self.restore_snapshot(snapshot);
}
self.parse_generic_bounds(colon_span)?
} else {
Vec::new()
};

let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };

Ok(GenericParam {
ident,
id: ast::DUMMY_NODE_ID,
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/parser/issues/issue-102182-impl-trait-recover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn foo<T: impl Trait>() {}
//~^ ERROR expected trait bound, found `impl Trait` type
fn main() {}
14 changes: 14 additions & 0 deletions src/test/ui/parser/issues/issue-102182-impl-trait-recover.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: expected trait bound, found `impl Trait` type
--> $DIR/issue-102182-impl-trait-recover.rs:1:11
|
LL | fn foo<T: impl Trait>() {}
| ^^^^^^^^^^ not a trait
|
help: use the trait bounds directly
|
LL - fn foo<T: impl Trait>() {}
LL + fn foo<T: Trait>() {}
|

error: aborting due to previous error