Skip to content

Commit bbf2f8c

Browse files
authored
Merge pull request #21561 from Veykril/push-xvymvmuvzuyn
fix: Fix more glob issues
2 parents 7cb789d + 1462d3d commit bbf2f8c

3 files changed

Lines changed: 99 additions & 25 deletions

File tree

crates/hir-def/src/item_scope.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,24 @@ impl ItemScope {
893893
self.macros.get_mut(name).expect("tried to update visibility of non-existent macro");
894894
res.vis = vis;
895895
}
896+
897+
pub(crate) fn update_def_types(&mut self, name: &Name, def: ModuleDefId, vis: Visibility) {
898+
let res = self.types.get_mut(name).expect("tried to update def of non-existent type");
899+
res.def = def;
900+
res.vis = vis;
901+
}
902+
903+
pub(crate) fn update_def_values(&mut self, name: &Name, def: ModuleDefId, vis: Visibility) {
904+
let res = self.values.get_mut(name).expect("tried to update def of non-existent value");
905+
res.def = def;
906+
res.vis = vis;
907+
}
908+
909+
pub(crate) fn update_def_macros(&mut self, name: &Name, def: MacroId, vis: Visibility) {
910+
let res = self.macros.get_mut(name).expect("tried to update def of non-existent macro");
911+
res.def = def;
912+
res.vis = vis;
913+
}
896914
}
897915

898916
impl PerNs {

crates/hir-def/src/nameres/collector.rs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,42 +1209,69 @@ impl<'db> DefCollector<'db> {
12091209
// `ItemScope::push_res_with_import()`.
12101210
if let Some(def) = defs.types
12111211
&& let Some(prev_def) = prev_defs.types
1212-
&& def.def == prev_def.def
1213-
&& self.from_glob_import.contains_type(module_id, name.clone())
1214-
&& def.vis != prev_def.vis
1215-
&& def.vis.max(self.db, prev_def.vis, &self.def_map) == Some(def.vis)
12161212
{
1217-
changed = true;
1218-
// This import is being handled here, don't pass it down to
1219-
// `ItemScope::push_res_with_import()`.
1220-
defs.types = None;
1221-
self.def_map.modules[module_id].scope.update_visibility_types(name, def.vis);
1213+
if def.def == prev_def.def
1214+
&& self.from_glob_import.contains_type(module_id, name.clone())
1215+
&& def.vis != prev_def.vis
1216+
&& def.vis.max(self.db, prev_def.vis, &self.def_map) == Some(def.vis)
1217+
{
1218+
changed = true;
1219+
// This import is being handled here, don't pass it down to
1220+
// `ItemScope::push_res_with_import()`.
1221+
defs.types = None;
1222+
self.def_map.modules[module_id].scope.update_visibility_types(name, def.vis);
1223+
}
1224+
// When the source module's definition changed (e.g., due to an explicit import
1225+
// shadowing a glob), propagate the new definition to modules that glob-import from it.
1226+
// We check that the previous definition came from the same glob import to avoid
1227+
// incorrectly overwriting definitions from different glob sources.
1228+
//
1229+
// Note this is not a perfect fix, but it makes
1230+
// https://github.com/rust-lang/rust-analyzer/issues/19224 work for now until we
1231+
// implement a proper glob graph
1232+
else if def.def != prev_def.def && prev_def.import == def_import_type {
1233+
changed = true;
1234+
defs.types = None;
1235+
self.def_map.modules[module_id].scope.update_def_types(name, def.def, def.vis);
1236+
}
12221237
}
12231238

12241239
if let Some(def) = defs.values
12251240
&& let Some(prev_def) = prev_defs.values
1226-
&& def.def == prev_def.def
1227-
&& self.from_glob_import.contains_value(module_id, name.clone())
1228-
&& def.vis != prev_def.vis
1229-
&& def.vis.max(self.db, prev_def.vis, &self.def_map) == Some(def.vis)
12301241
{
1231-
changed = true;
1232-
// See comment above.
1233-
defs.values = None;
1234-
self.def_map.modules[module_id].scope.update_visibility_values(name, def.vis);
1242+
if def.def == prev_def.def
1243+
&& self.from_glob_import.contains_value(module_id, name.clone())
1244+
&& def.vis != prev_def.vis
1245+
&& def.vis.max(self.db, prev_def.vis, &self.def_map) == Some(def.vis)
1246+
{
1247+
changed = true;
1248+
defs.values = None;
1249+
self.def_map.modules[module_id].scope.update_visibility_values(name, def.vis);
1250+
} else if def.def != prev_def.def
1251+
&& prev_def.import.map(ImportOrExternCrate::from) == def_import_type
1252+
{
1253+
changed = true;
1254+
defs.values = None;
1255+
self.def_map.modules[module_id].scope.update_def_values(name, def.def, def.vis);
1256+
}
12351257
}
12361258

12371259
if let Some(def) = defs.macros
12381260
&& let Some(prev_def) = prev_defs.macros
1239-
&& def.def == prev_def.def
1240-
&& self.from_glob_import.contains_macro(module_id, name.clone())
1241-
&& def.vis != prev_def.vis
1242-
&& def.vis.max(self.db, prev_def.vis, &self.def_map) == Some(def.vis)
12431261
{
1244-
changed = true;
1245-
// See comment above.
1246-
defs.macros = None;
1247-
self.def_map.modules[module_id].scope.update_visibility_macros(name, def.vis);
1262+
if def.def == prev_def.def
1263+
&& self.from_glob_import.contains_macro(module_id, name.clone())
1264+
&& def.vis != prev_def.vis
1265+
&& def.vis.max(self.db, prev_def.vis, &self.def_map) == Some(def.vis)
1266+
{
1267+
changed = true;
1268+
defs.macros = None;
1269+
self.def_map.modules[module_id].scope.update_visibility_macros(name, def.vis);
1270+
} else if def.def != prev_def.def && prev_def.import == def_import_type {
1271+
changed = true;
1272+
defs.macros = None;
1273+
self.def_map.modules[module_id].scope.update_def_macros(name, def.def, def.vis);
1274+
}
12481275
}
12491276
}
12501277

crates/hir-ty/src/tests/regression.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,3 +2645,32 @@ where
26452645
"#,
26462646
);
26472647
}
2648+
2649+
#[test]
2650+
fn issue_21560() {
2651+
check_no_mismatches(
2652+
r#"
2653+
mod bindings {
2654+
use super::*;
2655+
pub type HRESULT = i32;
2656+
}
2657+
use bindings::*;
2658+
2659+
2660+
mod error {
2661+
use super::*;
2662+
pub fn nonzero_hresult(hr: HRESULT) -> crate::HRESULT {
2663+
hr
2664+
}
2665+
}
2666+
pub use error::*;
2667+
2668+
mod hresult {
2669+
use super::*;
2670+
pub struct HRESULT(pub i32);
2671+
}
2672+
pub use hresult::HRESULT;
2673+
2674+
"#,
2675+
);
2676+
}

0 commit comments

Comments
 (0)