Skip to content

Commit

Permalink
resolve: Avoid comparing modules by optional def-id
Browse files Browse the repository at this point in the history
It makes all block modules identical during comparison
  • Loading branch information
petrochenkov committed Oct 2, 2021
1 parent 5aa732a commit ded08e4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
3 changes: 1 addition & 2 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/diagnostics.rs
Expand Up @@ -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| {
Expand Down Expand Up @@ -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));
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_resolve/src/imports.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Expand Up @@ -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 };
Expand Down
19 changes: 12 additions & 7 deletions compiler/rustc_resolve/src/lib.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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![] });
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
};

Expand Down

0 comments on commit ded08e4

Please sign in to comment.