Skip to content

Commit cd45b19

Browse files
committed
Auto merge of #58872 - rep-nop:diagnostic-fix-56031, r=petrochenkov
Adds help message in error for invalid `impl for T` syntax Fixes #56031.
2 parents 03dafa7 + e19b228 commit cd45b19

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/libsyntax/parse/parser.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -6732,7 +6732,15 @@ impl<'a> Parser<'a> {
67326732
};
67336733

67346734
// Parse both types and traits as a type, then reinterpret if necessary.
6735-
let ty_first = self.parse_ty()?;
6735+
let err_path = |span| ast::Path::from_ident(Ident::new(keywords::Invalid.name(), span));
6736+
let ty_first = if self.token.is_keyword(keywords::For) &&
6737+
self.look_ahead(1, |t| t != &token::Lt) {
6738+
let span = self.prev_span.between(self.span);
6739+
self.struct_span_err(span, "missing trait in a trait impl").emit();
6740+
P(Ty { node: TyKind::Path(None, err_path(span)), span, id: ast::DUMMY_NODE_ID })
6741+
} else {
6742+
self.parse_ty()?
6743+
};
67366744

67376745
// If `for` is missing we try to recover.
67386746
let has_for = self.eat_keyword(keywords::For);
@@ -6741,7 +6749,7 @@ impl<'a> Parser<'a> {
67416749
let ty_second = if self.token == token::DotDot {
67426750
// We need to report this error after `cfg` expansion for compatibility reasons
67436751
self.bump(); // `..`, do not add it to expected tokens
6744-
Some(P(Ty { node: TyKind::Err, span: self.prev_span, id: ast::DUMMY_NODE_ID }))
6752+
Some(DummyResult::raw_ty(self.prev_span, true))
67456753
} else if has_for || self.token.can_begin_type() {
67466754
Some(self.parse_ty()?)
67476755
} else {
@@ -6771,7 +6779,7 @@ impl<'a> Parser<'a> {
67716779
TyKind::Path(None, path) => path,
67726780
_ => {
67736781
self.span_err(ty_first.span, "expected a trait, found type");
6774-
ast::Path::from_ident(Ident::new(keywords::Invalid.name(), ty_first.span))
6782+
err_path(ty_first.span)
67756783
}
67766784
};
67776785
let trait_ref = TraitRef { path, ref_id: ty_first.id };

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

+6
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

+8
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)