resolve: Avoid comparing modules by optional def-id

It makes all block modules identical during comparison
This commit is contained in:
Vadim Petrochenkov 2021-09-26 19:29:53 +03:00
parent 5aa732a364
commit ded08e44c6
5 changed files with 19 additions and 15 deletions

View File

@ -220,8 +220,7 @@ impl<'a> Resolver<'a> {
} }
crate fn build_reduced_graph_external(&mut self, module: Module<'a>) { crate fn build_reduced_graph_external(&mut self, module: Module<'a>) {
let def_id = module.def_id().expect("unpopulated module without a def-id"); for child in self.cstore().item_children_untracked(module.def_id(), self.session) {
for child in self.cstore().item_children_untracked(def_id, self.session) {
let parent_scope = ParentScope::module(module, self); let parent_scope = ParentScope::module(module, self);
BuildReducedGraphVisitor { r: self, parent_scope } BuildReducedGraphVisitor { r: self, parent_scope }
.build_reduced_graph_for_external_crate_res(child); .build_reduced_graph_for_external_crate_res(child);

View File

@ -801,7 +801,7 @@ impl<'a> Resolver<'a> {
None => worklist_via_import.pop(), None => worklist_via_import.pop(),
Some(x) => Some(x), Some(x) => Some(x),
} { } {
let in_module_is_extern = !in_module.def_id().unwrap().is_local(); let in_module_is_extern = !in_module.def_id().is_local();
// We have to visit module children in deterministic order to avoid // We have to visit module children in deterministic order to avoid
// instabilities in reported imports (#43552). // instabilities in reported imports (#43552).
in_module.for_each_child(self, |this, ident, ns, name_binding| { in_module.for_each_child(self, |this, ident, ns, name_binding| {
@ -884,7 +884,7 @@ impl<'a> Resolver<'a> {
if !is_extern_crate_that_also_appears_in_prelude { if !is_extern_crate_that_also_appears_in_prelude {
// add the module to the lookup // add the module to the lookup
if seen_modules.insert(module.def_id().unwrap()) { if seen_modules.insert(module.def_id()) {
if via_import { &mut worklist_via_import } else { &mut worklist } if via_import { &mut worklist_via_import } else { &mut worklist }
.push((module, path_segments, child_accessible)); .push((module, path_segments, child_accessible));
} }

View File

@ -989,7 +989,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
} }
if let ModuleOrUniformRoot::Module(module) = module { if let ModuleOrUniformRoot::Module(module) = module {
if module.def_id() == import.parent_scope.module.def_id() { if ptr::eq(module, import.parent_scope.module) {
// Importing a module into itself is not allowed. // Importing a module into itself is not allowed.
return Some(UnresolvedImportError { return Some(UnresolvedImportError {
span: import.span, span: import.span,
@ -1341,7 +1341,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
if module.is_trait() { if module.is_trait() {
self.r.session.span_err(import.span, "items in traits are not importable."); self.r.session.span_err(import.span, "items in traits are not importable.");
return; return;
} else if module.def_id() == import.parent_scope.module.def_id() { } else if ptr::eq(module, import.parent_scope.module) {
return; return;
} else if let ImportKind::Glob { is_prelude: true, .. } = import.kind { } else if let ImportKind::Glob { is_prelude: true, .. } = import.kind {
self.r.prelude = Some(module); self.r.prelude = Some(module);
@ -1400,7 +1400,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
}); });
if !reexports.is_empty() { if !reexports.is_empty() {
if let Some(def_id) = module.def_id() { if let Some(def_id) = module.opt_def_id() {
// Call to `expect_local` should be fine because current // Call to `expect_local` should be fine because current
// code is only called for local modules. // code is only called for local modules.
self.r.export_map.insert(def_id.expect_local(), reexports); self.r.export_map.insert(def_id.expect_local(), reexports);

View File

@ -1491,7 +1491,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
// form the path // form the path
let mut path_segments = path_segments.clone(); let mut path_segments = path_segments.clone();
path_segments.push(ast::PathSegment::from_ident(ident)); path_segments.push(ast::PathSegment::from_ident(ident));
let module_def_id = module.def_id().unwrap(); let module_def_id = module.def_id();
if module_def_id == def_id { if module_def_id == def_id {
let path = let path =
Path { span: name_binding.span, segments: path_segments, tokens: None }; Path { span: name_binding.span, segments: path_segments, tokens: None };

View File

@ -413,7 +413,7 @@ impl ModuleOrUniformRoot<'_> {
fn same_def(lhs: Self, rhs: Self) -> bool { fn same_def(lhs: Self, rhs: Self) -> bool {
match (lhs, rhs) { match (lhs, rhs) {
(ModuleOrUniformRoot::Module(lhs), ModuleOrUniformRoot::Module(rhs)) => { (ModuleOrUniformRoot::Module(lhs), ModuleOrUniformRoot::Module(rhs)) => {
lhs.def_id() == rhs.def_id() ptr::eq(lhs, rhs)
} }
( (
ModuleOrUniformRoot::CrateRootAndExternPrelude, ModuleOrUniformRoot::CrateRootAndExternPrelude,
@ -602,7 +602,11 @@ impl<'a> ModuleData<'a> {
} }
} }
fn def_id(&self) -> Option<DefId> { fn def_id(&self) -> DefId {
self.opt_def_id().expect("`ModuleData::def_id` is called on a block module")
}
fn opt_def_id(&self) -> Option<DefId> {
match self.kind { match self.kind {
ModuleKind::Def(_, def_id, _) => Some(def_id), ModuleKind::Def(_, def_id, _) => Some(def_id),
_ => None, _ => None,
@ -1075,7 +1079,7 @@ impl<'a> ResolverArenas<'a> {
) -> Module<'a> { ) -> Module<'a> {
let module = let module =
self.modules.alloc(ModuleData::new(parent, kind, expn_id, span, no_implicit_prelude)); self.modules.alloc(ModuleData::new(parent, kind, expn_id, span, no_implicit_prelude));
let def_id = module.def_id(); let def_id = module.opt_def_id();
if def_id.map_or(true, |def_id| def_id.is_local()) { if def_id.map_or(true, |def_id| def_id.is_local()) {
self.local_modules.borrow_mut().push(module); self.local_modules.borrow_mut().push(module);
} }
@ -1588,7 +1592,7 @@ impl<'a> Resolver<'a> {
if let Some(module) = current_trait { if let Some(module) = current_trait {
if self.trait_may_have_item(Some(module), assoc_item) { if self.trait_may_have_item(Some(module), assoc_item) {
let def_id = module.def_id().unwrap(); let def_id = module.def_id();
found_traits.push(TraitCandidate { def_id, import_ids: smallvec![] }); found_traits.push(TraitCandidate { def_id, import_ids: smallvec![] });
} }
} }
@ -2189,8 +2193,9 @@ impl<'a> Resolver<'a> {
return self.graph_root; return self.graph_root;
} }
}; };
let module = self let module = self.expect_module(
.expect_module(module.def_id().map_or(LOCAL_CRATE, |def_id| def_id.krate).as_def_id()); module.opt_def_id().map_or(LOCAL_CRATE, |def_id| def_id.krate).as_def_id(),
);
debug!( debug!(
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})", "resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})",
ident, ident,
@ -3017,7 +3022,7 @@ impl<'a> Resolver<'a> {
} }
let container = match parent.kind { let container = match parent.kind {
ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id().unwrap()), ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id()),
ModuleKind::Block(..) => "block", ModuleKind::Block(..) => "block",
}; };