Skip to content

Commit f721977

Browse files
authored
Rollup merge of rust-lang#58872 - rep-nop:diagnostic-fix-56031, r=petrochenkov
Adds help message in error for invalid `impl for T` syntax Fixes rust-lang#56031.
2 parents 112007c + e19b228 commit f721977

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6743,7 +6743,15 @@ impl<'a> Parser<'a> {
67436743
};
67446744

67456745
// Parse both types and traits as a type, then reinterpret if necessary.
6746-
let ty_first = self.parse_ty()?;
6746+
let err_path = |span| ast::Path::from_ident(Ident::new(keywords::Invalid.name(), span));
6747+
let ty_first = if self.token.is_keyword(keywords::For) &&
6748+
self.look_ahead(1, |t| t != &token::Lt) {
6749+
let span = self.prev_span.between(self.span);
6750+
self.struct_span_err(span, "missing trait in a trait impl").emit();
6751+
P(Ty { node: TyKind::Path(None, err_path(span)), span, id: ast::DUMMY_NODE_ID })
6752+
} else {
6753+
self.parse_ty()?
6754+
};
67476755

67486756
// If `for` is missing we try to recover.
67496757
let has_for = self.eat_keyword(keywords::For);
@@ -6752,7 +6760,7 @@ impl<'a> Parser<'a> {
67526760
let ty_second = if self.token == token::DotDot {
67536761
// We need to report this error after `cfg` expansion for compatibility reasons
67546762
self.bump(); // `..`, do not add it to expected tokens
6755-
Some(P(Ty { node: TyKind::Err, span: self.prev_span, id: ast::DUMMY_NODE_ID }))
6763+
Some(DummyResult::raw_ty(self.prev_span, true))
67566764
} else if has_for || self.token.can_begin_type() {
67576765
Some(self.parse_ty()?)
67586766
} else {
@@ -6782,7 +6790,7 @@ impl<'a> Parser<'a> {
67826790
TyKind::Path(None, path) => path,
67836791
_ => {
67846792
self.span_err(ty_first.span, "expected a trait, found type");
6785-
ast::Path::from_ident(Ident::new(keywords::Invalid.name(), ty_first.span))
6793+
err_path(ty_first.span)
67866794
}
67876795
};
67886796
let trait_ref = TraitRef { path, ref_id: ty_first.id };

src/test/ui/issues/issue-56031.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
struct T;
2+
3+
impl for T {}
4+
//~^ ERROR missing trait in a trait impl
5+
6+
fn main() {}

src/test/ui/issues/issue-56031.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: missing trait in a trait impl
2+
--> $DIR/issue-56031.rs:3:5
3+
|
4+
LL | impl for T {}
5+
| ^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)