forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#124200 - scrabsha:sasha/->, r=compiler-errors,fmease 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](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7f8b6f4433aa7866124123575456f54e)) 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)
- Loading branch information
Showing
8 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//@ run-rustfix | ||
#![allow( | ||
dead_code, | ||
unused_must_use | ||
)] | ||
|
||
struct Named { | ||
foo: usize | ||
} | ||
|
||
struct Unnamed(usize); | ||
|
||
fn named_struct_field_access(named: &Named) { | ||
named.foo; //~ ERROR `->` used for field access or method call | ||
} | ||
|
||
fn unnamed_struct_field_access(unnamed: &Unnamed) { | ||
unnamed.0; //~ ERROR `->` used for field access or method call | ||
} | ||
|
||
fn tuple_field_access(t: &(u8, u8)) { | ||
t.0; //~ ERROR `->` used for field access or method call | ||
t.1; //~ ERROR `->` used for field access or method call | ||
} | ||
|
||
#[derive(Clone)] | ||
struct Foo; | ||
|
||
fn method_call(foo: &Foo) { | ||
foo.clone(); //~ ERROR `->` used for field access or method call | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//@ run-rustfix | ||
#![allow( | ||
dead_code, | ||
unused_must_use | ||
)] | ||
|
||
struct Named { | ||
foo: usize | ||
} | ||
|
||
struct Unnamed(usize); | ||
|
||
fn named_struct_field_access(named: &Named) { | ||
named->foo; //~ ERROR `->` used for field access or method call | ||
} | ||
|
||
fn unnamed_struct_field_access(unnamed: &Unnamed) { | ||
unnamed->0; //~ ERROR `->` used for field access or method call | ||
} | ||
|
||
fn tuple_field_access(t: &(u8, u8)) { | ||
t->0; //~ ERROR `->` used for field access or method call | ||
t->1; //~ ERROR `->` used for field access or method call | ||
} | ||
|
||
#[derive(Clone)] | ||
struct Foo; | ||
|
||
fn method_call(foo: &Foo) { | ||
foo->clone(); //~ ERROR `->` used for field access or method call | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
error: `->` used for field access or method call | ||
--> $DIR/expr-rarrow-call.rs:14:10 | ||
| | ||
LL | named->foo; | ||
| ^^ help: try using `.` instead | ||
| | ||
= help: the `.` operator will dereference the value if needed | ||
|
||
error: `->` used for field access or method call | ||
--> $DIR/expr-rarrow-call.rs:18:12 | ||
| | ||
LL | unnamed->0; | ||
| ^^ help: try using `.` instead | ||
| | ||
= help: the `.` operator will dereference the value if needed | ||
|
||
error: `->` used for field access or method call | ||
--> $DIR/expr-rarrow-call.rs:22:6 | ||
| | ||
LL | t->0; | ||
| ^^ help: try using `.` instead | ||
| | ||
= help: the `.` operator will dereference the value if needed | ||
|
||
error: `->` used for field access or method call | ||
--> $DIR/expr-rarrow-call.rs:23:6 | ||
| | ||
LL | t->1; | ||
| ^^ help: try using `.` instead | ||
| | ||
= help: the `.` operator will dereference the value if needed | ||
|
||
error: `->` used for field access or method call | ||
--> $DIR/expr-rarrow-call.rs:30:8 | ||
| | ||
LL | foo->clone(); | ||
| ^^ help: try using `.` instead | ||
| | ||
= help: the `.` operator will dereference the value if needed | ||
|
||
error: aborting due to 5 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters