-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Suggest importing for partial mod path matching in name resolving #112917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// edition:2018 | ||
|
||
// In this test baz isn't resolved when called as foo.baz even though | ||
// it's called from inside foo. This is somewhat surprising and may | ||
// want to change eventually. | ||
|
||
mod foo { | ||
pub fn bar() { foo::baz(); } //~ ERROR failed to resolve: use of undeclared crate or module `foo` | ||
|
||
fn baz() { } | ||
} | ||
|
||
fn main() { } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0433]: failed to resolve: use of undeclared crate or module `foo` | ||
--> $DIR/export-fully-qualified-2018.rs:8:20 | ||
| | ||
LL | pub fn bar() { foo::baz(); } | ||
| ^^^ use of undeclared crate or module `foo` | ||
| | ||
help: consider importing this module | ||
| | ||
LL + use crate::foo; | ||
| | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0433`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
error[E0433]: failed to resolve: use of undeclared crate or module `foo` | ||
--> $DIR/export-fully-qualified.rs:6:20 | ||
--> $DIR/export-fully-qualified.rs:8:20 | ||
| | ||
LL | pub fn bar() { foo::baz(); } | ||
| ^^^ use of undeclared crate or module `foo` | ||
| | ||
help: consider importing this module | ||
| | ||
LL + use foo; | ||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't that useful either 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a difference between With this PR, the output for rustc +dev2 --edition 2021 tests/ui/resolve/export-fully-qualified.rs
error[E0432]: unresolved import `foo`
--> tests/ui/resolve/export-fully-qualified.rs:7:13
|
7 | use foo;
| ^^^ no external crate `foo`
|
help: consider importing this module instead
|
7 | use crate::foo;
| the testing framework is running without 2021, so it's suggesting to write this: mod foo {
pub fn bar() {
use foo;
foo::baz();
} //~ ERROR failed to resolve: use of undeclared crate or module `foo`
fn baz() { }
} This is valid code for ~/rust ❯❯❯ rustc +dev2 --edition 2018 tests/ui/resolve/export-fully-qualified.rs
error[E0432]: unresolved import `foo`
--> tests/ui/resolve/export-fully-qualified.rs:7:13
|
7 | use foo;
| ^^^ no external crate `foo`
|
help: consider importing this module instead
|
7 | use crate::foo;
| ~~~~~~~~~~
error: aborting due to previous error Maybe it's related to this issue #112924 Anyway, this suggestion seems right for different versions. Or it's better to suggest writing like this? crate::foo::baz(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have two tests, one for 2015 and one for 2018, so that we can ensure that the suggestions keep being reasonable for both :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, added another test |
||
| | ||
|
||
error: aborting due to previous error | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pub struct S; | ||
|
||
impl fmt::Debug for S { //~ ERROR failed to resolve: use of undeclared crate or module `fmt` | ||
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { //~ ERROR failed to resolve: use of undeclared crate or module `fmt` | ||
//~^ ERROR failed to resolve: use of undeclared crate or module `fmt` | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn main() { } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
error[E0433]: failed to resolve: use of undeclared crate or module `fmt` | ||
--> $DIR/issue-112590-suggest-import.rs:3:6 | ||
| | ||
LL | impl fmt::Debug for S { | ||
| ^^^ use of undeclared crate or module `fmt` | ||
| | ||
help: consider importing this module | ||
| | ||
LL + use std::fmt; | ||
| | ||
|
||
error[E0433]: failed to resolve: use of undeclared crate or module `fmt` | ||
--> $DIR/issue-112590-suggest-import.rs:4:28 | ||
| | ||
LL | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| ^^^ use of undeclared crate or module `fmt` | ||
| | ||
help: consider importing this module | ||
| | ||
LL + use std::fmt; | ||
| | ||
|
||
error[E0433]: failed to resolve: use of undeclared crate or module `fmt` | ||
--> $DIR/issue-112590-suggest-import.rs:4:51 | ||
| | ||
LL | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| ^^^ use of undeclared crate or module `fmt` | ||
| | ||
help: consider importing this module | ||
| | ||
LL + use std::fmt; | ||
| | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0433`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suggestion isn't great, since it doesn't take into account the fact that the path may have additional segments that don't make sense after the suggestion is applied.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message is not introduced by this PR, but from here:
rust/compiler/rustc_resolve/src/diagnostics.rs
Line 1506 in 04075b3
The output is:
I guess the diagnostic emitter did some formatting stuff, if there are multiple diagnostics in the same place, it will display in list items?
We might fix this suggestion in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this is pre-existing and can be done separately. It can be dealt by making
smart_resolve_report_errors
pass in the wholepath
(and alen: usize
arg to signal the path segment that failed) and doing multipletry_lookup_name_relaxed
and try to select the right candidates. I would assume that the last path segment is always the most critical to bias towards when searching for the right candidate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the only thing left would be to check in the above suggestion if the suggested type has an associated item corresponding to the next path segment (in order to avoid suggesting things like
Vec::Vec
when we know thatVec
doesn't have anything namedVec
within it.I think we can do that as a separate PR.