Skip to content

Commit ffc5717

Browse files
authored
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`
2 parents 946192b + 501945a commit ffc5717

File tree

13 files changed

+66
-46
lines changed

13 files changed

+66
-46
lines changed

compiler/rustc_attr_parsing/src/attributes/stability.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_attr_data_structures::{
55
StableSince, UnstableReason, VERSION_PLACEHOLDER,
66
};
77
use rustc_errors::ErrorGuaranteed;
8-
use rustc_span::{Span, Symbol, kw, sym};
8+
use rustc_span::{Span, Symbol, sym};
99

1010
use super::util::parse_version;
1111
use super::{AcceptMapping, AttributeParser, SingleAttributeParser};
@@ -61,11 +61,7 @@ impl AttributeParser for StabilityParser {
6161
}),
6262
(&[sym::rustc_allowed_through_unstable_modules], |this, cx, args| {
6363
reject_outside_std!(cx);
64-
this.allowed_through_unstable_modules =
65-
Some(match args.name_value().and_then(|i| i.value_as_str()) {
66-
Some(msg) => msg,
67-
None => kw::Empty,
68-
});
64+
this.allowed_through_unstable_modules = args.name_value().and_then(|i| i.value_as_str())
6965
}),
7066
];
7167

compiler/rustc_builtin_macros/src/derive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl MultiItemModifier for Expander {
103103
fn dummy_annotatable() -> Annotatable {
104104
Annotatable::GenericParam(ast::GenericParam {
105105
id: ast::DUMMY_NODE_ID,
106-
ident: Ident::empty(),
106+
ident: Ident::dummy(),
107107
attrs: Default::default(),
108108
bounds: Default::default(),
109109
is_placeholder: false,

compiler/rustc_error_codes/src/error_codes/E0789.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Erroneous code example:
1414
1515
#![unstable(feature = "foo_module", reason = "...", issue = "123")]
1616
17-
#[rustc_allowed_through_unstable_modules]
17+
#[rustc_allowed_through_unstable_modules = "deprecation message"]
1818
// #[stable(feature = "foo", since = "1.0")]
1919
struct Foo;
2020
// ^^^ error: `rustc_allowed_through_unstable_modules` attribute must be

compiler/rustc_lint/src/non_fmt_panic.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_parse_format::{ParseMode, Parser, Piece};
77
use rustc_session::lint::FutureIncompatibilityReason;
88
use rustc_session::{declare_lint, declare_lint_pass};
99
use rustc_span::edition::Edition;
10-
use rustc_span::{InnerSpan, Span, Symbol, hygiene, kw, sym};
10+
use rustc_span::{InnerSpan, Span, Symbol, hygiene, sym};
1111
use rustc_trait_selection::infer::InferCtxtExt;
1212

1313
use crate::lints::{NonFmtPanicBraces, NonFmtPanicUnused};
@@ -167,7 +167,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
167167
.get_diagnostic_item(sym::Debug)
168168
.is_some_and(|t| infcx.type_implements_trait(t, [ty], param_env).may_apply());
169169

170-
let suggest_panic_any = !is_str && panic == sym::std_panic_macro;
170+
let suggest_panic_any = !is_str && panic == Some(sym::std_panic_macro);
171171

172172
let fmt_applicability = if suggest_panic_any {
173173
// If we can use panic_any, use that as the MachineApplicable suggestion.
@@ -297,10 +297,13 @@ fn find_delimiters(cx: &LateContext<'_>, span: Span) -> Option<(Span, Span, char
297297
))
298298
}
299299

300-
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol, Symbol) {
300+
fn panic_call<'tcx>(
301+
cx: &LateContext<'tcx>,
302+
f: &'tcx hir::Expr<'tcx>,
303+
) -> (Span, Option<Symbol>, Symbol) {
301304
let mut expn = f.span.ctxt().outer_expn_data();
302305

303-
let mut panic_macro = kw::Empty;
306+
let mut panic_macro = None;
304307

305308
// Unwrap more levels of macro expansion, as panic_2015!()
306309
// was likely expanded from panic!() and possibly from
@@ -320,7 +323,7 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
320323
break;
321324
}
322325
expn = parent;
323-
panic_macro = name;
326+
panic_macro = Some(name);
324327
}
325328

326329
let macro_symbol =

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2871,7 +2871,7 @@ impl<'a> Parser<'a> {
28712871
// Skip every token until next possible arg or end.
28722872
p.eat_to_tokens(&[exp!(Comma), exp!(CloseParen)]);
28732873
// Create a placeholder argument for proper arg count (issue #34264).
2874-
Ok(dummy_arg(Ident::new(kw::Empty, lo.to(p.prev_token.span)), guar))
2874+
Ok(dummy_arg(Ident::new(sym::dummy, lo.to(p.prev_token.span)), guar))
28752875
});
28762876
// ...now that we've parsed the first argument, `self` is no longer allowed.
28772877
first_param = false;

compiler/rustc_passes/src/errors.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -1348,12 +1348,12 @@ pub(crate) struct DuplicateLangItem {
13481348
pub local_span: Option<Span>,
13491349
pub lang_item_name: Symbol,
13501350
pub crate_name: Symbol,
1351-
pub dependency_of: Symbol,
1351+
pub dependency_of: Option<Symbol>,
13521352
pub is_local: bool,
13531353
pub path: String,
13541354
pub first_defined_span: Option<Span>,
1355-
pub orig_crate_name: Symbol,
1356-
pub orig_dependency_of: Symbol,
1355+
pub orig_crate_name: Option<Symbol>,
1356+
pub orig_dependency_of: Option<Symbol>,
13571357
pub orig_is_local: bool,
13581358
pub orig_path: String,
13591359
pub(crate) duplicate: Duplicate,
@@ -1374,18 +1374,24 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for DuplicateLangItem {
13741374
diag.code(E0152);
13751375
diag.arg("lang_item_name", self.lang_item_name);
13761376
diag.arg("crate_name", self.crate_name);
1377-
diag.arg("dependency_of", self.dependency_of);
1377+
if let Some(dependency_of) = self.dependency_of {
1378+
diag.arg("dependency_of", dependency_of);
1379+
}
13781380
diag.arg("path", self.path);
1379-
diag.arg("orig_crate_name", self.orig_crate_name);
1380-
diag.arg("orig_dependency_of", self.orig_dependency_of);
1381+
if let Some(orig_crate_name) = self.orig_crate_name {
1382+
diag.arg("orig_crate_name", orig_crate_name);
1383+
}
1384+
if let Some(orig_dependency_of) = self.orig_dependency_of {
1385+
diag.arg("orig_dependency_of", orig_dependency_of);
1386+
}
13811387
diag.arg("orig_path", self.orig_path);
13821388
if let Some(span) = self.local_span {
13831389
diag.span(span);
13841390
}
13851391
if let Some(span) = self.first_defined_span {
13861392
diag.span_note(span, fluent::passes_first_defined_span);
13871393
} else {
1388-
if self.orig_dependency_of.is_empty() {
1394+
if self.orig_dependency_of.is_none() {
13891395
diag.note(fluent::passes_first_defined_crate);
13901396
} else {
13911397
diag.note(fluent::passes_first_defined_crate_depends);

compiler/rustc_passes/src/lang_items.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_hir::{LangItem, LanguageItems, MethodKind, Target};
1616
use rustc_middle::query::Providers;
1717
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1818
use rustc_session::cstore::ExternCrate;
19-
use rustc_span::{Span, kw};
19+
use rustc_span::Span;
2020

2121
use crate::errors::{
2222
DuplicateLangItem, IncorrectTarget, LangItemOnIncorrectTarget, UnknownLangItem,
@@ -98,7 +98,7 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
9898
{
9999
let lang_item_name = lang_item.name();
100100
let crate_name = self.tcx.crate_name(item_def_id.krate);
101-
let mut dependency_of = kw::Empty;
101+
let mut dependency_of = None;
102102
let is_local = item_def_id.is_local();
103103
let path = if is_local {
104104
String::new()
@@ -112,8 +112,8 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
112112
};
113113

114114
let first_defined_span = self.item_spans.get(&original_def_id).copied();
115-
let mut orig_crate_name = kw::Empty;
116-
let mut orig_dependency_of = kw::Empty;
115+
let mut orig_crate_name = None;
116+
let mut orig_dependency_of = None;
117117
let orig_is_local = original_def_id.is_local();
118118
let orig_path = if orig_is_local {
119119
String::new()
@@ -127,11 +127,11 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
127127
};
128128

129129
if first_defined_span.is_none() {
130-
orig_crate_name = self.tcx.crate_name(original_def_id.krate);
130+
orig_crate_name = Some(self.tcx.crate_name(original_def_id.krate));
131131
if let Some(ExternCrate { dependency_of: inner_dependency_of, .. }) =
132132
self.tcx.extern_crate(original_def_id.krate)
133133
{
134-
orig_dependency_of = self.tcx.crate_name(*inner_dependency_of);
134+
orig_dependency_of = Some(self.tcx.crate_name(*inner_dependency_of));
135135
}
136136
}
137137

@@ -140,7 +140,7 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
140140
} else {
141141
match self.tcx.extern_crate(item_def_id.krate) {
142142
Some(ExternCrate { dependency_of: inner_dependency_of, .. }) => {
143-
dependency_of = self.tcx.crate_name(*inner_dependency_of);
143+
dependency_of = Some(self.tcx.crate_name(*inner_dependency_of));
144144
Duplicate::CrateDepends
145145
}
146146
_ => Duplicate::Crate,

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,

compiler/rustc_resolve/src/diagnostics.rs

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

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;

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
});
@@ -1067,11 +1067,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10671067
);
10681068
if fallback_binding.ok().and_then(|b| b.res().opt_def_id()) != Some(def_id) {
10691069
let location = match parent_scope.module.kind {
1070-
ModuleKind::Def(_, _, name) if name == kw::Empty => {
1071-
"the crate root".to_string()
1072-
}
10731070
ModuleKind::Def(kind, def_id, name) => {
1074-
format!("{} `{name}`", kind.descr(def_id))
1071+
if let Some(name) = name {
1072+
format!("{} `{name}`", kind.descr(def_id))
1073+
} else {
1074+
"the crate root".to_string()
1075+
}
10751076
}
10761077
ModuleKind::Block => "this scope".to_string(),
10771078
};
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Ensure we reject `#![crate_name = ""]`.
2+
3+
#![crate_name = ""] //~ ERROR crate name must not be empty
4+
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: crate name must not be empty
2+
--> $DIR/crate-name-empty.rs:3:1
3+
|
4+
LL | #![crate_name = ""]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)