Auto merge of #58872 - rep-nop:diagnostic-fix-56031, r=petrochenkov
Adds help message in error for invalid `impl for T` syntax Fixes #56031.
This commit is contained in:
commit
cd45b19bd2
3 changed files with 25 additions and 3 deletions
|
@ -6732,7 +6732,15 @@ impl<'a> Parser<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Parse both types and traits as a type, then reinterpret if necessary.
|
// Parse both types and traits as a type, then reinterpret if necessary.
|
||||||
let ty_first = self.parse_ty()?;
|
let err_path = |span| ast::Path::from_ident(Ident::new(keywords::Invalid.name(), span));
|
||||||
|
let ty_first = if self.token.is_keyword(keywords::For) &&
|
||||||
|
self.look_ahead(1, |t| t != &token::Lt) {
|
||||||
|
let span = self.prev_span.between(self.span);
|
||||||
|
self.struct_span_err(span, "missing trait in a trait impl").emit();
|
||||||
|
P(Ty { node: TyKind::Path(None, err_path(span)), span, id: ast::DUMMY_NODE_ID })
|
||||||
|
} else {
|
||||||
|
self.parse_ty()?
|
||||||
|
};
|
||||||
|
|
||||||
// If `for` is missing we try to recover.
|
// If `for` is missing we try to recover.
|
||||||
let has_for = self.eat_keyword(keywords::For);
|
let has_for = self.eat_keyword(keywords::For);
|
||||||
|
@ -6741,7 +6749,7 @@ impl<'a> Parser<'a> {
|
||||||
let ty_second = if self.token == token::DotDot {
|
let ty_second = if self.token == token::DotDot {
|
||||||
// We need to report this error after `cfg` expansion for compatibility reasons
|
// We need to report this error after `cfg` expansion for compatibility reasons
|
||||||
self.bump(); // `..`, do not add it to expected tokens
|
self.bump(); // `..`, do not add it to expected tokens
|
||||||
Some(P(Ty { node: TyKind::Err, span: self.prev_span, id: ast::DUMMY_NODE_ID }))
|
Some(DummyResult::raw_ty(self.prev_span, true))
|
||||||
} else if has_for || self.token.can_begin_type() {
|
} else if has_for || self.token.can_begin_type() {
|
||||||
Some(self.parse_ty()?)
|
Some(self.parse_ty()?)
|
||||||
} else {
|
} else {
|
||||||
|
@ -6771,7 +6779,7 @@ impl<'a> Parser<'a> {
|
||||||
TyKind::Path(None, path) => path,
|
TyKind::Path(None, path) => path,
|
||||||
_ => {
|
_ => {
|
||||||
self.span_err(ty_first.span, "expected a trait, found type");
|
self.span_err(ty_first.span, "expected a trait, found type");
|
||||||
ast::Path::from_ident(Ident::new(keywords::Invalid.name(), ty_first.span))
|
err_path(ty_first.span)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let trait_ref = TraitRef { path, ref_id: ty_first.id };
|
let trait_ref = TraitRef { path, ref_id: ty_first.id };
|
||||||
|
|
6
src/test/ui/issues/issue-56031.rs
Normal file
6
src/test/ui/issues/issue-56031.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
struct T;
|
||||||
|
|
||||||
|
impl for T {}
|
||||||
|
//~^ ERROR missing trait in a trait impl
|
||||||
|
|
||||||
|
fn main() {}
|
8
src/test/ui/issues/issue-56031.stderr
Normal file
8
src/test/ui/issues/issue-56031.stderr
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
error: missing trait in a trait impl
|
||||||
|
--> $DIR/issue-56031.rs:3:5
|
||||||
|
|
|
||||||
|
LL | impl for T {}
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue