-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 #103706 - zbyrn:issue-101637-fix, r=estebank
Fix E0433 No Typo Suggestions Fixes #48676 Fixes #87791 Fixes #96625 Fixes #95462 Fixes #101637 Follows up PR #72923 Several open issues refer to the problem that E0433 does not suggest typos like other errors normally do. This fix augments the implementation of PR #72923. **Background** When the path of a function call, e.g. `Struct::foo()`, involves names that cannot be resolved, there are two errors that could be emitted by the compiler: - If `Struct` is not found, it is ``E0433: failed to resolve: use of undeclared type `Struct` ``. - If `foo` is not found in `Struct`, it is ``E0599: no function or associated item named `foo` found for struct `Struct` in the current scope`` When a name is used as a type, `e.g. fn foo() -> Struct`, and the name cannot be resolved, it is ``E0412: cannot find type `Struct` in this scope``. Before #72923, `E0433` does not implement any suggestions, and the PR introduces suggestions for missing `use`s. When a resolution error occurs in the path of a function call, it tries to smart resolve just the type part of the path, e.g. `module::Struct` of a call to `module::Struct::foo()`. However, along with the suggestions, the smart-resolve function will report `E0412` since it only knows that it is a type that we cannot resolve instead of being a part of the path. So, the original implementation swap out `E0412` errors returned by the smart-resolve function with the real `E0433` error, but keeps the "missing `use`" suggestions to be reported to the programmer. **Issue** The current implementation only reports if there are "missing `use`" suggestions returned by the smart-resolve function; otherwise, it would fall back the normal reporting, which does not emit suggestions. But the smart-resolve function could also produce typo suggestions, which are omitted currently. Also, it seems like that not all info has been swapped out when there are missing suggestions. The error message underlining the name in the snippet still says ``not found in this scope``, which is a `E0412` messages, if there are `use` suggestions, but says the normal `use of undeclared type` otherwise. **Fixes** This fix swaps out all fields in `Diagnostic` returned by the smart-resolve function except for `suggestions` with the current error, and merges the `suggestions` of the returned error and that of the current error together. If there are `use` suggestions, the error is saved to `use_injection` to be reported at the end; otherwise, the error is emitted immediately as `Resolver::report_error` does. Some tests are updated to use the correct underlining error messages, and one additional test for typo suggestion is added to the test suite. r? rust-lang/diagnostics
- Loading branch information
Showing
12 changed files
with
148 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
struct Struct; | ||
//~^ NOTE function or associated item `fob` not found for this struct | ||
|
||
impl Struct { | ||
fn foo() { } | ||
} | ||
|
||
mod module { | ||
fn foo() { } | ||
|
||
struct Struct; | ||
|
||
impl Struct { | ||
fn foo() { } | ||
} | ||
} | ||
|
||
trait Trait { | ||
fn foo(); | ||
} | ||
|
||
fn main() { | ||
Struct::fob(); | ||
//~^ ERROR no function or associated item named `fob` found for struct `Struct` in the current scope | ||
//~| NOTE function or associated item not found in `Struct` | ||
|
||
Struc::foo(); | ||
//~^ ERROR failed to resolve: use of undeclared type `Struc` | ||
//~| NOTE use of undeclared type `Struc` | ||
|
||
modul::foo(); | ||
//~^ ERROR failed to resolve: use of undeclared crate or module `modul` | ||
//~| NOTE use of undeclared crate or module `modul` | ||
|
||
module::Struc::foo(); | ||
//~^ ERROR failed to resolve: could not find `Struc` in `module` | ||
//~| NOTE could not find `Struc` in `module` | ||
|
||
Trai::foo(); | ||
//~^ ERROR failed to resolve: use of undeclared type `Trai` | ||
//~| NOTE use of undeclared type `Trai` | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/ui/resolve/typo-suggestion-mistyped-in-path.stderr
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,54 @@ | ||
error[E0433]: failed to resolve: use of undeclared type `Struc` | ||
--> $DIR/typo-suggestion-mistyped-in-path.rs:27:5 | ||
| | ||
LL | Struc::foo(); | ||
| ^^^^^ | ||
| | | ||
| use of undeclared type `Struc` | ||
| help: a struct with a similar name exists: `Struct` | ||
|
||
error[E0433]: failed to resolve: use of undeclared crate or module `modul` | ||
--> $DIR/typo-suggestion-mistyped-in-path.rs:31:5 | ||
| | ||
LL | modul::foo(); | ||
| ^^^^^ use of undeclared crate or module `modul` | ||
| | ||
help: there is a crate or module with a similar name | ||
| | ||
LL | module::foo(); | ||
| ~~~~~~ | ||
|
||
error[E0433]: failed to resolve: could not find `Struc` in `module` | ||
--> $DIR/typo-suggestion-mistyped-in-path.rs:35:13 | ||
| | ||
LL | module::Struc::foo(); | ||
| ^^^^^ | ||
| | | ||
| could not find `Struc` in `module` | ||
| help: a struct with a similar name exists: `Struct` | ||
|
||
error[E0433]: failed to resolve: use of undeclared type `Trai` | ||
--> $DIR/typo-suggestion-mistyped-in-path.rs:39:5 | ||
| | ||
LL | Trai::foo(); | ||
| ^^^^ | ||
| | | ||
| use of undeclared type `Trai` | ||
| help: a trait with a similar name exists: `Trait` | ||
|
||
error[E0599]: no function or associated item named `fob` found for struct `Struct` in the current scope | ||
--> $DIR/typo-suggestion-mistyped-in-path.rs:23:13 | ||
| | ||
LL | struct Struct; | ||
| ------------- function or associated item `fob` not found for this struct | ||
... | ||
LL | Struct::fob(); | ||
| ^^^ | ||
| | | ||
| function or associated item not found in `Struct` | ||
| help: there is an associated function with a similar name: `foo` | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
Some errors have detailed explanations: E0433, E0599. | ||
For more information about an error, try `rustc --explain E0433`. |
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