forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#112239 - jieyouxu:targeted-no-method-suggesti…
…ons, r=cjgillot Add `#[rustc_confusables]` attribute to allow targeted "no method" error suggestions on standard library types After this PR, the standard library developer can annotate methods on e.g. `BTreeSet::push` with `#[rustc_confusables("insert")]`. When the user mistypes `btreeset.push()`, `BTreeSet::insert` will be suggested if there are no other candidates to suggest. This PR lays the foundations for contributors to add `rustc_confusables` annotations to standard library types for targeted suggestions, as specified in rust-lang#59450, or to address cases such as rust-lang#108437. ### Example Assume `BTreeSet` is the standard library type: ``` // Standard library definition #![feature(rustc_attrs)] struct BTreeSet; impl BTreeSet { #[rustc_confusables("push")] fn insert(&self) {} } // User code fn main() { let x = BTreeSet {}; x.push(); } ``` A new suggestion (which has lower precedence than suggestions for misspellings and only is shown when there are no misspellings suggestions) will be added to hint the user maybe they intended to write `x.insert()` instead: ``` error[E0599]: no method named `push` found for struct `BTreeSet` in the current scope --> test.rs:12:7 | 3 | struct BTreeSet; | --------------- method `push` not found for this struct ... 12 | x.push(); | ^^^^ method not found in `BTreeSet` | help: you might have meant to use `insert` | 12 | x.insert(); | ~~~~~~ error: aborting due to previous error ```
- Loading branch information
Showing
12 changed files
with
259 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
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
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
11 changes: 11 additions & 0 deletions
11
tests/ui/attributes/auxiliary/rustc_confusables_across_crate.rs
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,11 @@ | ||
#![feature(rustc_attrs)] | ||
|
||
pub struct BTreeSet; | ||
|
||
impl BTreeSet { | ||
#[rustc_confusables("push", "test_b")] | ||
pub fn insert(&self) {} | ||
|
||
#[rustc_confusables("pulled")] | ||
pub fn pull(&self) {} | ||
} |
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,47 @@ | ||
// aux-build: rustc_confusables_across_crate.rs | ||
|
||
#![feature(rustc_attrs)] | ||
|
||
extern crate rustc_confusables_across_crate; | ||
|
||
use rustc_confusables_across_crate::BTreeSet; | ||
|
||
fn main() { | ||
// Misspellings (similarly named methods) take precedence over `rustc_confusables`. | ||
let x = BTreeSet {}; | ||
x.inser(); | ||
//~^ ERROR no method named | ||
//~| HELP there is a method with a similar name | ||
x.foo(); | ||
//~^ ERROR no method named | ||
x.push(); | ||
//~^ ERROR no method named | ||
//~| HELP you might have meant to use `insert` | ||
x.test(); | ||
//~^ ERROR no method named | ||
x.pulled(); | ||
//~^ ERROR no method named | ||
//~| HELP there is a method with a similar name | ||
} | ||
|
||
struct Bar; | ||
|
||
impl Bar { | ||
#[rustc_confusables()] | ||
//~^ ERROR expected at least one confusable name | ||
fn baz() {} | ||
|
||
#[rustc_confusables] | ||
//~^ ERROR malformed `rustc_confusables` attribute input | ||
//~| HELP must be of the form | ||
fn qux() {} | ||
|
||
#[rustc_confusables(invalid_meta_item)] | ||
//~^ ERROR expected a quoted string literal | ||
//~| HELP consider surrounding this with quotes | ||
fn quux() {} | ||
} | ||
|
||
#[rustc_confusables("blah")] | ||
//~^ ERROR attribute should be applied to an inherent method | ||
fn not_inherent_impl_method() {} |
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,68 @@ | ||
error: malformed `rustc_confusables` attribute input | ||
--> $DIR/rustc_confusables.rs:34:5 | ||
| | ||
LL | #[rustc_confusables] | ||
| ^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_confusables("name1", "name2", ...)]` | ||
|
||
error: attribute should be applied to an inherent method | ||
--> $DIR/rustc_confusables.rs:45:1 | ||
| | ||
LL | #[rustc_confusables("blah")] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: expected at least one confusable name | ||
--> $DIR/rustc_confusables.rs:30:5 | ||
| | ||
LL | #[rustc_confusables()] | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error[E0539]: expected a quoted string literal | ||
--> $DIR/rustc_confusables.rs:39:25 | ||
| | ||
LL | #[rustc_confusables(invalid_meta_item)] | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider surrounding this with quotes | ||
| | ||
LL | #[rustc_confusables("invalid_meta_item")] | ||
| + + | ||
|
||
error[E0599]: no method named `inser` found for struct `rustc_confusables_across_crate::BTreeSet` in the current scope | ||
--> $DIR/rustc_confusables.rs:12:7 | ||
| | ||
LL | x.inser(); | ||
| ^^^^^ help: there is a method with a similar name: `insert` | ||
|
||
error[E0599]: no method named `foo` found for struct `rustc_confusables_across_crate::BTreeSet` in the current scope | ||
--> $DIR/rustc_confusables.rs:15:7 | ||
| | ||
LL | x.foo(); | ||
| ^^^ method not found in `BTreeSet` | ||
|
||
error[E0599]: no method named `push` found for struct `rustc_confusables_across_crate::BTreeSet` in the current scope | ||
--> $DIR/rustc_confusables.rs:17:7 | ||
| | ||
LL | x.push(); | ||
| ^^^^ method not found in `BTreeSet` | ||
| | ||
help: you might have meant to use `insert` | ||
| | ||
LL | x.insert(); | ||
| ~~~~~~ | ||
|
||
error[E0599]: no method named `test` found for struct `rustc_confusables_across_crate::BTreeSet` in the current scope | ||
--> $DIR/rustc_confusables.rs:20:7 | ||
| | ||
LL | x.test(); | ||
| ^^^^ method not found in `BTreeSet` | ||
|
||
error[E0599]: no method named `pulled` found for struct `rustc_confusables_across_crate::BTreeSet` in the current scope | ||
--> $DIR/rustc_confusables.rs:22:7 | ||
| | ||
LL | x.pulled(); | ||
| ^^^^^^ help: there is a method with a similar name: `pull` | ||
|
||
error: aborting due to 9 previous errors | ||
|
||
Some errors have detailed explanations: E0539, E0599. | ||
For more information about an error, try `rustc --explain E0539`. |