Improve handling of expr->field errors
The current message for "`->` used for field access" is the following: ```rust error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `->` --> src/main.rs:2:6 | 2 | a->b; | ^^ expected one of 8 possible tokens ``` (playground link[1]) This PR tries to address this by adding a dedicated error message and recovery. The proposed error message is: ``` error: `->` used for field access or method call --> ./tiny_test.rs:2:6 | 2 | a->b; | ^^ help: try using `.` instead | = help: the `.` operator will dereference the value if needed ``` (feel free to bikeshed it as much as necessary) [1]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7f8b6f4433aa7866124123575456f54e Signed-off-by: Sasha Pourcelot <sasha.pourcelot@protonmail.com>
This commit is contained in:
parent
a61b14d15e
commit
98332c108b
8 changed files with 140 additions and 4 deletions
|
@ -230,6 +230,10 @@ parse_expected_struct_field = expected one of `,`, `:`, or `{"}"}`, found `{$tok
|
|||
|
||||
parse_expected_trait_in_trait_impl_found_type = expected a trait, found type
|
||||
|
||||
parse_expr_rarrow_call = `->` used for field access or method call
|
||||
.suggestion = try using `.` instead
|
||||
.help = the `.` operator will dereference the value if needed
|
||||
|
||||
parse_extern_crate_name_with_dashes = crate name using dashes are not valid in `extern crate` statements
|
||||
.label = dash-separated idents are not valid
|
||||
.suggestion = if the original crate name uses dashes you need to use underscores in the code
|
||||
|
|
|
@ -2988,3 +2988,12 @@ pub(crate) struct AsyncImpl {
|
|||
#[primary_span]
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(parse_expr_rarrow_call)]
|
||||
#[help]
|
||||
pub(crate) struct ExprRArrowCall {
|
||||
#[primary_span]
|
||||
#[suggestion(style = "short", applicability = "machine-applicable", code = ".")]
|
||||
pub span: Span,
|
||||
}
|
||||
|
|
|
@ -979,6 +979,12 @@ impl<'a> Parser<'a> {
|
|||
// we are using noexpect here because we don't expect a `.` directly after a `return`
|
||||
// which could be suggested otherwise
|
||||
self.eat_noexpect(&token::Dot)
|
||||
} else if self.token.kind == TokenKind::RArrow && self.may_recover() {
|
||||
// Recovery for `expr->suffix`.
|
||||
self.bump();
|
||||
let span = self.prev_token.span;
|
||||
self.dcx().emit_err(errors::ExprRArrowCall { span });
|
||||
true
|
||||
} else {
|
||||
self.eat(&token::Dot)
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue