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>) {
let def_id = module.def_id().expect("unpopulated module without a def-id");
for child in self.cstore().item_children_untracked(def_id, self.session) {
for child in self.cstore().item_children_untracked(module.def_id(), self.session) {
let parent_scope = ParentScope::module(module, self);
BuildReducedGraphVisitor { r: self, parent_scope }
.build_reduced_graph_for_external_crate_res(child);

View File

@ -801,7 +801,7 @@ impl<'a> Resolver<'a> {
None => worklist_via_import.pop(),
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
// instabilities in reported imports (#43552).
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 {
// 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 }
.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 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.
return Some(UnresolvedImportError {
span: import.span,
@ -1341,7 +1341,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
if module.is_trait() {
self.r.session.span_err(import.span, "items in traits are not importable.");
return;
} else if module.def_id() == import.parent_scope.module.def_id() {
} else if ptr::eq(module, import.parent_scope.module) {
return;
} else if let ImportKind::Glob { is_prelude: true, .. } = import.kind {
self.r.prelude = Some(module);
@ -1400,7 +1400,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
});
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
// code is only called for local modules.
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
let mut path_segments = path_segments.clone();
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 {
let path =
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 {
match (lhs, rhs) {
(ModuleOrUniformRoot::Module(lhs), ModuleOrUniformRoot::Module(rhs)) => {
lhs.def_id() == rhs.def_id()
ptr::eq(lhs, rhs)
}
(
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 {
ModuleKind::Def(_, def_id, _) => Some(def_id),
_ => None,
@ -1075,7 +1079,7 @@ impl<'a> ResolverArenas<'a> {
) -> Module<'a> {
let module =
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()) {
self.local_modules.borrow_mut().push(module);
}
@ -1588,7 +1592,7 @@ impl<'a> Resolver<'a> {
if let Some(module) = current_trait {
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![] });
}
}
@ -2189,8 +2193,9 @@ impl<'a> Resolver<'a> {
return self.graph_root;
}
};
let module = self
.expect_module(module.def_id().map_or(LOCAL_CRATE, |def_id| def_id.krate).as_def_id());
let module = self.expect_module(
module.opt_def_id().map_or(LOCAL_CRATE, |def_id| def_id.krate).as_def_id(),
);
debug!(
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})",
ident,
@ -3017,7 +3022,7 @@ impl<'a> Resolver<'a> {
}
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",
};