Skip to content

Commit 3f32a32

Browse files
committed
Use Option<Symbol> in ModuleKind::Def.
This way, `None` represents "crate root without a name" instead of `kw::Empty`. This changes makes it impossible to forget to handle the exceptional case.
1 parent 2469ab1 commit 3f32a32

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

Diff for: compiler/rustc_resolve/src/build_reduced_graph.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
131131
let expn_id = self.cstore().expn_that_defined_untracked(def_id, self.tcx.sess);
132132
return Some(self.new_module(
133133
parent,
134-
ModuleKind::Def(def_kind, def_id, self.tcx.item_name(def_id)),
134+
ModuleKind::Def(def_kind, def_id, Some(self.tcx.item_name(def_id))),
135135
expn_id,
136136
self.def_span(def_id),
137137
// FIXME: Account for `#[no_implicit_prelude]` attributes.
@@ -594,7 +594,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
594594
// HACK(eddyb) unclear how good this is, but keeping `$crate`
595595
// in `source` breaks `tests/ui/imports/import-crate-var.rs`,
596596
// while the current crate doesn't have a valid `crate_name`.
597-
if crate_name != kw::Empty {
597+
if let Some(crate_name) = crate_name {
598598
// `crate_name` should not be interpreted as relative.
599599
module_path.push(Segment::from_ident_and_id(
600600
Ident { name: kw::PathRoot, span: source.ident.span },
@@ -603,7 +603,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
603603
source.ident.name = crate_name;
604604
}
605605
if rename.is_none() {
606-
ident.name = crate_name;
606+
ident.name = sym::dummy;
607607
}
608608

609609
self.r.dcx().emit_err(errors::CrateImported { span: item.span });
@@ -775,7 +775,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
775775
ItemKind::Mod(.., ref mod_kind) => {
776776
let module = self.r.new_module(
777777
Some(parent),
778-
ModuleKind::Def(def_kind, def_id, ident.name),
778+
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
779779
expansion.to_expn_id(),
780780
item.span,
781781
parent.no_implicit_prelude
@@ -811,7 +811,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
811811
ItemKind::Enum(_, _) | ItemKind::Trait(..) => {
812812
let module = self.r.new_module(
813813
Some(parent),
814-
ModuleKind::Def(def_kind, def_id, ident.name),
814+
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
815815
expansion.to_expn_id(),
816816
item.span,
817817
parent.no_implicit_prelude,

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24392439
let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() else {
24402440
return None;
24412441
};
2442-
let module_name = crate_module.kind.name().unwrap();
2442+
let module_name = crate_module.kind.name().unwrap_or(kw::Empty);
24432443
let import_snippet = match import.kind {
24442444
ImportKind::Single { source, target, .. } if source != target => {
24452445
format!("{source} as {target}")

Diff for: compiler/rustc_resolve/src/lib.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -503,18 +503,18 @@ enum ModuleKind {
503503
///
504504
/// * A normal module – either `mod from_file;` or `mod from_block { }` –
505505
/// or the crate root (which is conceptually a top-level module).
506-
/// Note that the crate root's [name][Self::name] will be [`kw::Empty`].
506+
/// The crate root will have `None` for the symbol.
507507
/// * A trait or an enum (it implicitly contains associated types, methods and variant
508508
/// constructors).
509-
Def(DefKind, DefId, Symbol),
509+
Def(DefKind, DefId, Option<Symbol>),
510510
}
511511

512512
impl ModuleKind {
513513
/// Get name of the module.
514514
fn name(&self) -> Option<Symbol> {
515-
match self {
515+
match *self {
516516
ModuleKind::Block => None,
517-
ModuleKind::Def(.., name) => Some(*name),
517+
ModuleKind::Def(.., name) => name,
518518
}
519519
}
520520
}
@@ -1402,7 +1402,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14021402
let mut module_self_bindings = FxHashMap::default();
14031403
let graph_root = arenas.new_module(
14041404
None,
1405-
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
1405+
ModuleKind::Def(DefKind::Mod, root_def_id, None),
14061406
ExpnId::root(),
14071407
crate_span,
14081408
attr::contains_name(attrs, sym::no_implicit_prelude),
@@ -1411,7 +1411,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14111411
);
14121412
let empty_module = arenas.new_module(
14131413
None,
1414-
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
1414+
ModuleKind::Def(DefKind::Mod, root_def_id, None),
14151415
ExpnId::root(),
14161416
DUMMY_SP,
14171417
true,
@@ -2286,7 +2286,8 @@ fn module_to_string(mut module: Module<'_>) -> Option<String> {
22862286
loop {
22872287
if let ModuleKind::Def(.., name) = module.kind {
22882288
if let Some(parent) = module.parent {
2289-
names.push(name);
2289+
// `unwrap` is safe: the presence of a parent means it's not the crate root.
2290+
names.push(name.unwrap());
22902291
module = parent
22912292
} else {
22922293
break;

Diff for: compiler/rustc_resolve/src/macros.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
168168
hygiene::update_dollar_crate_names(|ctxt| {
169169
let ident = Ident::new(kw::DollarCrate, DUMMY_SP.with_ctxt(ctxt));
170170
match self.resolve_crate_root(ident).kind {
171-
ModuleKind::Def(.., name) if name != kw::Empty => name,
171+
ModuleKind::Def(.., name) if let Some(name) = name => name,
172172
_ => kw::Crate,
173173
}
174174
});
@@ -1068,11 +1068,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10681068
);
10691069
if fallback_binding.ok().and_then(|b| b.res().opt_def_id()) != Some(def_id) {
10701070
let location = match parent_scope.module.kind {
1071-
ModuleKind::Def(_, _, name) if name == kw::Empty => {
1072-
"the crate root".to_string()
1073-
}
10741071
ModuleKind::Def(kind, def_id, name) => {
1075-
format!("{} `{name}`", kind.descr(def_id))
1072+
if let Some(name) = name {
1073+
format!("{} `{name}`", kind.descr(def_id))
1074+
} else {
1075+
"the crate root".to_string()
1076+
}
10761077
}
10771078
ModuleKind::Block => "this scope".to_string(),
10781079
};

0 commit comments

Comments
 (0)