-
Notifications
You must be signed in to change notification settings - Fork 13.4k
suggestions from other crates only work when an explicit extern crate
is present
#51212
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
Comments
It looks like the suggestions are issued here: rust/src/librustc_resolve/lib.rs Lines 4436 to 4463 in c131bdc
The behavior I think we want is this:
The function that prepares those suggestions appears to be this one: rust/src/librustc_resolve/lib.rs Lines 4398 to 4402 in c131bdc
Here is an example of checking for whether the feature is enabled: rust/src/librustc_resolve/lib.rs Line 3493 in c131bdc
|
@qmx expressed some interest in tackling this. |
Hmm, now i'm worried that this will apply even when it shouldn't. Consider this testcase: fn main() {
let x: &Debug = &22;
} This currently prints:
and I think we do not want it to print I think this suggests an alternative fix to what I recommended before (and what @qmx did in #51456) -- but I'd like to get @petrochenkov's opinion. Before I suggested changing how the rust/src/librustc_resolve/lib.rs Lines 4003 to 4015 in a32e979
Right now, this works by walking through the crate and finding candidates. I suspect (but we should test) that this means it will not work when the
|
@petrochenkov do the above notes seem ok? |
Looks reasonable. |
To get a bit more detailed in how to make this modification, ultimately I envision we'll have a helper like this: fn lookup_import_candidates_from_module(
&mut self,
lookup_name: Name,
namespace: Namespace,
start_module: Module<'_>,
filter_fn: impl Fn(Def) -> bool,
) -> Vec<ImportSuggestion> {
...
} This will be roughly the same as the existing rust/src/librustc_resolve/lib.rs Line 4020 in a32e979
Then, later, when it finds a suggestion: rust/src/librustc_resolve/lib.rs Line 4053 in a32e979
It will check if This is step 1. Step 2 in a second. =) |
OK, so step 2... The basic idea was to iterate over each of the "available" crates. For each crate, we would load the crate (if not already loaded), and then use the So the first question is where we find that list of available crates and how to load it. Here are some pointers. For example, here is where we find the set of things in the prelude: rust/src/librustc_resolve/lib.rs Lines 1627 to 1628 in fefe816
which ends up in the Resolver struct. And this is where we resolve paths that rely on external prelude: rust/src/librustc_resolve/lib.rs Lines 1928 to 1929 in fefe816
It seems sort of like this is the snippet of code to "load a crate if needed": rust/src/librustc_resolve/lib.rs Lines 1937 to 1939 in fefe816
I think the idea would be to refactor that last snippet of code into a helper fn, and then invoke it from fn lookup_import_candidates<FilterFn>(&mut self,
lookup_name: Name,
namespace: Namespace,
filter_fn: FilterFn)
-> Vec<ImportSuggestion>
where FilterFn: Fn(Def) -> bool
{
let mut suggestions = vec![];
suggestions.extend(
self.lookup_import_candidates_from_module(
lookup_name, namespace, self.graph_root, keywords::Crate.name(), filter_fn
)
);
if extern_prelude_feature_is_enabled {
let extern_prelude_names = self.extern_prelude.clone();
for &krate_name in extern_prelude_names.iter() {
let extern_prelude_module = self.load_extern_prelude_crate_if_needed(krate_name);
// ^^ that is the new helper method we added
suggestions.extend(
self.lookup_import_candidates_from_module(
lookup_name, namespace, extern_prelude_module, krate_name, filter_fn
)
);
}
}
} OK, there is a slight flaw -- I think that |
crate::
when enabledextern crate
is present
resolve suggestions should use `crate::` when enabled I couldn't find a way to add a specific assertion for the ui test, so the expected output is living under the `crates-in-path.stderr` ui test. - is this the right place for the test? fixes #51212
Uh oh!
There was an error while loading. Please reload this page.
UPDATE: There has been some progress on this issue.
crate::
prefixes now work, but you still don't get suggestions from other crates except when you write an explicitextern crate
.When you get name resolution suggestions from resolve after an error, they should use
crate::
prefixes where appropriate.Example (play):
gives
but I want to see
use crate::bar::Foo
suggested.The text was updated successfully, but these errors were encountered: