Rollup merge of #138924 - nnethercote:less-kw-Empty-3, r=compiler-errors

Reduce `kw::Empty` usage, part 3

Remove some more `kw::Empty` uses, in support of #137978.

r? `@davidtwco`
This commit is contained in:
Matthias Krüger 2025-03-25 18:09:07 +01:00 committed by GitHub
commit ffc571797b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 66 additions and 46 deletions

View file

@ -131,7 +131,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let expn_id = self.cstore().expn_that_defined_untracked(def_id, self.tcx.sess);
return Some(self.new_module(
parent,
ModuleKind::Def(def_kind, def_id, self.tcx.item_name(def_id)),
ModuleKind::Def(def_kind, def_id, Some(self.tcx.item_name(def_id))),
expn_id,
self.def_span(def_id),
// FIXME: Account for `#[no_implicit_prelude]` attributes.
@ -594,7 +594,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
// HACK(eddyb) unclear how good this is, but keeping `$crate`
// in `source` breaks `tests/ui/imports/import-crate-var.rs`,
// while the current crate doesn't have a valid `crate_name`.
if crate_name != kw::Empty {
if let Some(crate_name) = crate_name {
// `crate_name` should not be interpreted as relative.
module_path.push(Segment::from_ident_and_id(
Ident { name: kw::PathRoot, span: source.ident.span },
@ -603,7 +603,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
source.ident.name = crate_name;
}
if rename.is_none() {
ident.name = crate_name;
ident.name = sym::dummy;
}
self.r.dcx().emit_err(errors::CrateImported { span: item.span });
@ -775,7 +775,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
ItemKind::Mod(.., ref mod_kind) => {
let module = self.r.new_module(
Some(parent),
ModuleKind::Def(def_kind, def_id, ident.name),
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
expansion.to_expn_id(),
item.span,
parent.no_implicit_prelude
@ -811,7 +811,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
ItemKind::Enum(_, _) | ItemKind::Trait(..) => {
let module = self.r.new_module(
Some(parent),
ModuleKind::Def(def_kind, def_id, ident.name),
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
expansion.to_expn_id(),
item.span,
parent.no_implicit_prelude,

View file

@ -2438,7 +2438,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() else {
return None;
};
let module_name = crate_module.kind.name().unwrap();
let module_name = crate_module.kind.name().unwrap_or(kw::Empty);
let import_snippet = match import.kind {
ImportKind::Single { source, target, .. } if source != target => {
format!("{source} as {target}")

View file

@ -503,18 +503,18 @@ enum ModuleKind {
///
/// * A normal module either `mod from_file;` or `mod from_block { }`
/// or the crate root (which is conceptually a top-level module).
/// Note that the crate root's [name][Self::name] will be [`kw::Empty`].
/// The crate root will have `None` for the symbol.
/// * A trait or an enum (it implicitly contains associated types, methods and variant
/// constructors).
Def(DefKind, DefId, Symbol),
Def(DefKind, DefId, Option<Symbol>),
}
impl ModuleKind {
/// Get name of the module.
fn name(&self) -> Option<Symbol> {
match self {
match *self {
ModuleKind::Block => None,
ModuleKind::Def(.., name) => Some(*name),
ModuleKind::Def(.., name) => name,
}
}
}
@ -1402,7 +1402,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let mut module_self_bindings = FxHashMap::default();
let graph_root = arenas.new_module(
None,
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
ModuleKind::Def(DefKind::Mod, root_def_id, None),
ExpnId::root(),
crate_span,
attr::contains_name(attrs, sym::no_implicit_prelude),
@ -1411,7 +1411,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
);
let empty_module = arenas.new_module(
None,
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
ModuleKind::Def(DefKind::Mod, root_def_id, None),
ExpnId::root(),
DUMMY_SP,
true,
@ -2286,7 +2286,8 @@ fn module_to_string(mut module: Module<'_>) -> Option<String> {
loop {
if let ModuleKind::Def(.., name) = module.kind {
if let Some(parent) = module.parent {
names.push(name);
// `unwrap` is safe: the presence of a parent means it's not the crate root.
names.push(name.unwrap());
module = parent
} else {
break;

View file

@ -168,7 +168,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
hygiene::update_dollar_crate_names(|ctxt| {
let ident = Ident::new(kw::DollarCrate, DUMMY_SP.with_ctxt(ctxt));
match self.resolve_crate_root(ident).kind {
ModuleKind::Def(.., name) if name != kw::Empty => name,
ModuleKind::Def(.., name) if let Some(name) = name => name,
_ => kw::Crate,
}
});
@ -1067,11 +1067,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
);
if fallback_binding.ok().and_then(|b| b.res().opt_def_id()) != Some(def_id) {
let location = match parent_scope.module.kind {
ModuleKind::Def(_, _, name) if name == kw::Empty => {
"the crate root".to_string()
}
ModuleKind::Def(kind, def_id, name) => {
format!("{} `{name}`", kind.descr(def_id))
if let Some(name) = name {
format!("{} `{name}`", kind.descr(def_id))
} else {
"the crate root".to_string()
}
}
ModuleKind::Block => "this scope".to_string(),
};