Skip to content

Commit a8eaa9e

Browse files
committed
Auto merge of rust-lang#18160 - ChayimFriedman2:fix-18138, r=Veykril
fix: Fix name resolution when an import is resolved to some namespace and then later in the algorithm another namespace is added The import is flagged as "indeterminate", and previously it was re-resolved, but only at the end of name resolution, when it's already too late for anything that depends on it. This issue was tried to fix in rust-lang/rust-analyzer#2466, but it was not fixed fully. That PR is also why IDE features did work: the import at the end was resolved correctly, so IDE features that re-resolved the macro path resolved it correctly. I was concerned about the performance of this, but this doesn't seem to regress `analysis-stats .`, so I guess it's fine to land this. I have no idea about the incremental perf however and I don't know how to measure that, although when typing in `zbus` (including creating a new function, which should recompute the def map) completion was fast enough. I didn't check what rustc does, so maybe it does something more performant, like keeping track of only possibly problematic imports. Fixes rust-lang#18138. Probably fixes rust-lang#17630.
2 parents 80c0682 + 0a25908 commit a8eaa9e

File tree

3 files changed

+82
-13
lines changed

3 files changed

+82
-13
lines changed

src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs

+30-13
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ struct DefCollector<'a> {
221221
deps: FxHashMap<Name, Dependency>,
222222
glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility, UseId)>>,
223223
unresolved_imports: Vec<ImportDirective>,
224-
indeterminate_imports: Vec<ImportDirective>,
224+
indeterminate_imports: Vec<(ImportDirective, PerNs)>,
225225
unresolved_macros: Vec<MacroDirective>,
226226
mod_dirs: FxHashMap<LocalModuleId, ModDir>,
227227
cfg_options: &'a CfgOptions,
@@ -415,16 +415,6 @@ impl DefCollector<'_> {
415415

416416
self.resolution_loop();
417417

418-
// Resolve all indeterminate resolved imports again
419-
// As some of the macros will expand newly import shadowing partial resolved imports
420-
// FIXME: We maybe could skip this, if we handle the indeterminate imports in `resolve_imports`
421-
// correctly
422-
let partial_resolved = self.indeterminate_imports.drain(..).map(|directive| {
423-
ImportDirective { status: PartialResolvedImport::Unresolved, ..directive }
424-
});
425-
self.unresolved_imports.extend(partial_resolved);
426-
self.resolve_imports();
427-
428418
let unresolved_imports = mem::take(&mut self.unresolved_imports);
429419
// show unresolved imports in completion, etc
430420
for directive in &unresolved_imports {
@@ -749,9 +739,9 @@ impl DefCollector<'_> {
749739
.filter_map(|mut directive| {
750740
directive.status = self.resolve_import(directive.module_id, &directive.import);
751741
match directive.status {
752-
PartialResolvedImport::Indeterminate(_) => {
742+
PartialResolvedImport::Indeterminate(resolved) => {
753743
self.record_resolved_import(&directive);
754-
self.indeterminate_imports.push(directive);
744+
self.indeterminate_imports.push((directive, resolved));
755745
res = ReachedFixedPoint::No;
756746
None
757747
}
@@ -764,6 +754,33 @@ impl DefCollector<'_> {
764754
}
765755
})
766756
.collect();
757+
758+
// Resolve all indeterminate resolved imports again
759+
// As some of the macros will expand newly import shadowing partial resolved imports
760+
// FIXME: We maybe could skip this, if we handle the indeterminate imports in `resolve_imports`
761+
// correctly
762+
let mut indeterminate_imports = std::mem::take(&mut self.indeterminate_imports);
763+
indeterminate_imports.retain_mut(|(directive, partially_resolved)| {
764+
let partially_resolved = partially_resolved.availability();
765+
directive.status = self.resolve_import(directive.module_id, &directive.import);
766+
match directive.status {
767+
PartialResolvedImport::Indeterminate(import)
768+
if partially_resolved != import.availability() =>
769+
{
770+
self.record_resolved_import(directive);
771+
res = ReachedFixedPoint::No;
772+
false
773+
}
774+
PartialResolvedImport::Resolved(_) => {
775+
self.record_resolved_import(directive);
776+
res = ReachedFixedPoint::No;
777+
false
778+
}
779+
_ => true,
780+
}
781+
});
782+
self.indeterminate_imports = indeterminate_imports;
783+
767784
res
768785
}
769786

src/tools/rust-analyzer/crates/hir-def/src/per_ns.rs

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//!
44
//! `PerNs` (per namespace) captures this.
55
6+
use bitflags::bitflags;
7+
68
use crate::{
79
item_scope::{ImportId, ImportOrExternCrate, ItemInNs},
810
visibility::Visibility,
@@ -16,6 +18,16 @@ pub enum Namespace {
1618
Macros,
1719
}
1820

21+
bitflags! {
22+
/// Describes only the presence/absence of each namespace, without its value.
23+
#[derive(Debug, PartialEq, Eq)]
24+
pub(crate) struct NsAvailability : u32 {
25+
const TYPES = 1 << 0;
26+
const VALUES = 1 << 1;
27+
const MACROS = 1 << 2;
28+
}
29+
}
30+
1931
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
2032
pub struct PerNs {
2133
pub types: Option<(ModuleDefId, Visibility, Option<ImportOrExternCrate>)>,
@@ -24,6 +36,14 @@ pub struct PerNs {
2436
}
2537

2638
impl PerNs {
39+
pub(crate) fn availability(&self) -> NsAvailability {
40+
let mut result = NsAvailability::empty();
41+
result.set(NsAvailability::TYPES, self.types.is_some());
42+
result.set(NsAvailability::VALUES, self.values.is_some());
43+
result.set(NsAvailability::MACROS, self.macros.is_some());
44+
result
45+
}
46+
2747
pub fn none() -> PerNs {
2848
PerNs { types: None, values: None, macros: None }
2949
}

src/tools/rust-analyzer/crates/ide/src/goto_definition.rs

+32
Original file line numberDiff line numberDiff line change
@@ -2750,4 +2750,36 @@ fn foo() {
27502750
"#,
27512751
);
27522752
}
2753+
2754+
#[test]
2755+
fn issue_18138() {
2756+
check(
2757+
r#"
2758+
mod foo {
2759+
macro_rules! x {
2760+
() => {
2761+
pub struct Foo;
2762+
// ^^^
2763+
};
2764+
}
2765+
pub(crate) use x as m;
2766+
}
2767+
2768+
mod bar {
2769+
use crate::m;
2770+
2771+
m!();
2772+
// ^^^^^
2773+
2774+
fn qux() {
2775+
Foo$0;
2776+
}
2777+
}
2778+
2779+
mod m {}
2780+
2781+
use foo::m;
2782+
"#,
2783+
);
2784+
}
27532785
}

0 commit comments

Comments
 (0)