Skip to content

Commit ded08e4

Browse files
committed
resolve: Avoid comparing modules by optional def-id
It makes all block modules identical during comparison
1 parent 5aa732a commit ded08e4

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ impl<'a> Resolver<'a> {
220220
}
221221

222222
crate fn build_reduced_graph_external(&mut self, module: Module<'a>) {
223-
let def_id = module.def_id().expect("unpopulated module without a def-id");
224-
for child in self.cstore().item_children_untracked(def_id, self.session) {
223+
for child in self.cstore().item_children_untracked(module.def_id(), self.session) {
225224
let parent_scope = ParentScope::module(module, self);
226225
BuildReducedGraphVisitor { r: self, parent_scope }
227226
.build_reduced_graph_for_external_crate_res(child);

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ impl<'a> Resolver<'a> {
801801
None => worklist_via_import.pop(),
802802
Some(x) => Some(x),
803803
} {
804-
let in_module_is_extern = !in_module.def_id().unwrap().is_local();
804+
let in_module_is_extern = !in_module.def_id().is_local();
805805
// We have to visit module children in deterministic order to avoid
806806
// instabilities in reported imports (#43552).
807807
in_module.for_each_child(self, |this, ident, ns, name_binding| {
@@ -884,7 +884,7 @@ impl<'a> Resolver<'a> {
884884

885885
if !is_extern_crate_that_also_appears_in_prelude {
886886
// add the module to the lookup
887-
if seen_modules.insert(module.def_id().unwrap()) {
887+
if seen_modules.insert(module.def_id()) {
888888
if via_import { &mut worklist_via_import } else { &mut worklist }
889889
.push((module, path_segments, child_accessible));
890890
}

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
989989
}
990990

991991
if let ModuleOrUniformRoot::Module(module) = module {
992-
if module.def_id() == import.parent_scope.module.def_id() {
992+
if ptr::eq(module, import.parent_scope.module) {
993993
// Importing a module into itself is not allowed.
994994
return Some(UnresolvedImportError {
995995
span: import.span,
@@ -1341,7 +1341,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
13411341
if module.is_trait() {
13421342
self.r.session.span_err(import.span, "items in traits are not importable.");
13431343
return;
1344-
} else if module.def_id() == import.parent_scope.module.def_id() {
1344+
} else if ptr::eq(module, import.parent_scope.module) {
13451345
return;
13461346
} else if let ImportKind::Glob { is_prelude: true, .. } = import.kind {
13471347
self.r.prelude = Some(module);
@@ -1400,7 +1400,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14001400
});
14011401

14021402
if !reexports.is_empty() {
1403-
if let Some(def_id) = module.def_id() {
1403+
if let Some(def_id) = module.opt_def_id() {
14041404
// Call to `expect_local` should be fine because current
14051405
// code is only called for local modules.
14061406
self.r.export_map.insert(def_id.expect_local(), reexports);

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
14911491
// form the path
14921492
let mut path_segments = path_segments.clone();
14931493
path_segments.push(ast::PathSegment::from_ident(ident));
1494-
let module_def_id = module.def_id().unwrap();
1494+
let module_def_id = module.def_id();
14951495
if module_def_id == def_id {
14961496
let path =
14971497
Path { span: name_binding.span, segments: path_segments, tokens: None };

compiler/rustc_resolve/src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl ModuleOrUniformRoot<'_> {
413413
fn same_def(lhs: Self, rhs: Self) -> bool {
414414
match (lhs, rhs) {
415415
(ModuleOrUniformRoot::Module(lhs), ModuleOrUniformRoot::Module(rhs)) => {
416-
lhs.def_id() == rhs.def_id()
416+
ptr::eq(lhs, rhs)
417417
}
418418
(
419419
ModuleOrUniformRoot::CrateRootAndExternPrelude,
@@ -602,7 +602,11 @@ impl<'a> ModuleData<'a> {
602602
}
603603
}
604604

605-
fn def_id(&self) -> Option<DefId> {
605+
fn def_id(&self) -> DefId {
606+
self.opt_def_id().expect("`ModuleData::def_id` is called on a block module")
607+
}
608+
609+
fn opt_def_id(&self) -> Option<DefId> {
606610
match self.kind {
607611
ModuleKind::Def(_, def_id, _) => Some(def_id),
608612
_ => None,
@@ -1075,7 +1079,7 @@ impl<'a> ResolverArenas<'a> {
10751079
) -> Module<'a> {
10761080
let module =
10771081
self.modules.alloc(ModuleData::new(parent, kind, expn_id, span, no_implicit_prelude));
1078-
let def_id = module.def_id();
1082+
let def_id = module.opt_def_id();
10791083
if def_id.map_or(true, |def_id| def_id.is_local()) {
10801084
self.local_modules.borrow_mut().push(module);
10811085
}
@@ -1588,7 +1592,7 @@ impl<'a> Resolver<'a> {
15881592

15891593
if let Some(module) = current_trait {
15901594
if self.trait_may_have_item(Some(module), assoc_item) {
1591-
let def_id = module.def_id().unwrap();
1595+
let def_id = module.def_id();
15921596
found_traits.push(TraitCandidate { def_id, import_ids: smallvec![] });
15931597
}
15941598
}
@@ -2189,8 +2193,9 @@ impl<'a> Resolver<'a> {
21892193
return self.graph_root;
21902194
}
21912195
};
2192-
let module = self
2193-
.expect_module(module.def_id().map_or(LOCAL_CRATE, |def_id| def_id.krate).as_def_id());
2196+
let module = self.expect_module(
2197+
module.opt_def_id().map_or(LOCAL_CRATE, |def_id| def_id.krate).as_def_id(),
2198+
);
21942199
debug!(
21952200
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})",
21962201
ident,
@@ -3017,7 +3022,7 @@ impl<'a> Resolver<'a> {
30173022
}
30183023

30193024
let container = match parent.kind {
3020-
ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id().unwrap()),
3025+
ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id()),
30213026
ModuleKind::Block(..) => "block",
30223027
};
30233028

0 commit comments

Comments
 (0)