Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 15148c5

Browse files
authoredMar 12, 2025
Rollup merge of rust-lang#138347 - nnethercote:less-kw-Empty-2, r=compiler-errors
Reduce `kw::Empty` usage, part 2 A few small `kw::Empty` removals, or steps toward removals. r? fmease
2 parents 82e4741 + 4eadaff commit 15148c5

File tree

10 files changed

+41
-21
lines changed

10 files changed

+41
-21
lines changed
 

‎compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,7 @@ impl DummyAstNode for Item {
19481948
span: Default::default(),
19491949
tokens: Default::default(),
19501950
},
1951-
ident: Ident::empty(),
1951+
ident: Ident::dummy(),
19521952
kind: ItemKind::ExternCrate(None),
19531953
tokens: Default::default(),
19541954
}

‎compiler/rustc_attr_parsing/src/context.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_errors::{DiagCtxtHandle, Diagnostic};
99
use rustc_feature::Features;
1010
use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId};
1111
use rustc_session::Session;
12-
use rustc_span::symbol::kw;
1312
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1413

1514
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
@@ -338,7 +337,7 @@ impl<'sess> AttributeParser<'sess> {
338337
"expr in place where literal is expected (builtin attr parsing)",
339338
);
340339
ast::MetaItemLit {
341-
symbol: kw::Empty,
340+
symbol: sym::dummy,
342341
suffix: None,
343342
kind: ast::LitKind::Err(guar),
344343
span: DUMMY_SP,

‎compiler/rustc_attr_parsing/src/parser.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_ast::{AttrArgs, DelimArgs, Expr, ExprKind, LitKind, MetaItemLit, Norma
1212
use rustc_ast_pretty::pprust;
1313
use rustc_errors::DiagCtxtHandle;
1414
use rustc_hir::{self as hir, AttrPath};
15-
use rustc_span::symbol::{Ident, kw};
15+
use rustc_span::symbol::{Ident, kw, sym};
1616
use rustc_span::{ErrorGuaranteed, Span, Symbol};
1717

1818
pub struct SegmentIterator<'a> {
@@ -360,7 +360,7 @@ fn expr_to_lit(dcx: DiagCtxtHandle<'_>, expr: &Expr, span: Span) -> MetaItemLit
360360
span,
361361
"expr in place where literal is expected (builtin attr parsing)",
362362
);
363-
MetaItemLit { symbol: kw::Empty, suffix: None, kind: LitKind::Err(guar), span }
363+
MetaItemLit { symbol: sym::dummy, suffix: None, kind: LitKind::Err(guar), span }
364364
}
365365
}
366366

‎compiler/rustc_expand/src/mbe/quoted.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ fn parse_tree<'a>(
274274
let msg =
275275
format!("expected identifier, found `{}`", pprust::token_to_string(token),);
276276
sess.dcx().span_err(token.span, msg);
277-
TokenTree::MetaVar(token.span, Ident::empty())
277+
TokenTree::MetaVar(token.span, Ident::dummy())
278278
}
279279

280280
// There are no more tokens. Just return the `$` we already have.

‎compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'hir> PathSegment<'hir> {
243243
}
244244

245245
pub fn invalid() -> Self {
246-
Self::new(Ident::empty(), HirId::INVALID, Res::Err)
246+
Self::new(Ident::dummy(), HirId::INVALID, Res::Err)
247247
}
248248

249249
pub fn args(&self) -> &GenericArgs<'hir> {

‎compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2933,7 +2933,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29332933
}
29342934

29352935
let guar = if field.name == kw::Empty {
2936-
self.dcx().span_delayed_bug(field.span, "field name with no name")
2936+
self.dcx().span_bug(field.span, "field name with no name")
29372937
} else if self.method_exists_for_diagnostic(
29382938
field,
29392939
base_ty,

‎compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -828,15 +828,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
828828

829829
let trait_missing_method =
830830
matches!(error, method::MethodError::NoMatch(_)) && ty.normalized.is_trait();
831-
if item_name.name != kw::Empty {
832-
self.report_method_error(
833-
hir_id,
834-
ty.normalized,
835-
error,
836-
Expectation::NoExpectation,
837-
trait_missing_method && span.edition().at_least_rust_2021(), // emits missing method for trait only after edition 2021
838-
);
839-
}
831+
assert_ne!(item_name.name, kw::Empty);
832+
self.report_method_error(
833+
hir_id,
834+
ty.normalized,
835+
error,
836+
Expectation::NoExpectation,
837+
trait_missing_method && span.edition().at_least_rust_2021(), // emits missing method for trait only after edition 2021
838+
);
840839

841840
result
842841
});

‎compiler/rustc_privacy/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc_middle::ty::{
3939
use rustc_middle::{bug, span_bug};
4040
use rustc_session::lint;
4141
use rustc_span::hygiene::Transparency;
42-
use rustc_span::{Ident, Span, Symbol, kw, sym};
42+
use rustc_span::{Ident, Span, Symbol, sym};
4343
use tracing::debug;
4444
use {rustc_attr_parsing as attr, rustc_hir as hir};
4545

@@ -934,8 +934,8 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
934934
}
935935

936936
// definition of the field
937-
let ident = Ident::new(kw::Empty, use_ctxt);
938-
let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did(), hir_id).1;
937+
let ident = Ident::new(sym::dummy, use_ctxt);
938+
let (_, def_id) = self.tcx.adjust_ident_and_get_scope(ident, def.did(), hir_id);
939939
!field.vis.is_accessible_from(def_id, self.tcx)
940940
}
941941

‎compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2276,7 +2276,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22762276
&& !first.ident.is_path_segment_keyword() =>
22772277
{
22782278
// Insert a placeholder that's later replaced by `self`/`super`/etc.
2279-
path.insert(0, Segment::from_ident(Ident::empty()));
2279+
path.insert(0, Segment::from_ident(Ident::dummy()));
22802280
}
22812281
_ => return None,
22822282
}

‎compiler/rustc_span/src/symbol.rs

+22
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ symbols! {
3333
// Special reserved identifiers used internally for elided lifetimes,
3434
// unnamed method parameters, crate root module, error recovery etc.
3535
// Matching predicates: `is_any_keyword`, `is_special`/`is_reserved`
36+
//
37+
// Notes about `kw::Empty`:
38+
// - Its use can blur the lines between "empty symbol" and "no symbol".
39+
// Using `Option<Symbol>` is preferable, where possible, because that
40+
// is unambiguous.
41+
// - For dummy symbols that are never used and absolutely must be
42+
// present, it's better to use `sym::dummy` than `kw::Empty`, because
43+
// it's clearer that it's intended as a dummy value, and more likely
44+
// to be detected if it accidentally does get used.
3645
Empty: "",
3746
PathRoot: "{{root}}",
3847
DollarCrate: "$crate",
@@ -834,6 +843,7 @@ symbols! {
834843
drop_types_in_const,
835844
dropck_eyepatch,
836845
dropck_parametricity,
846+
dummy: "<!dummy!>", // use this instead of `kw::Empty` for symbols that won't be used
837847
dummy_cgu_name,
838848
dylib,
839849
dyn_compatible_for_dispatch,
@@ -2305,11 +2315,23 @@ impl Ident {
23052315
Ident::new(name, DUMMY_SP)
23062316
}
23072317

2318+
/// This is best avoided, because it blurs the lines between "empty
2319+
/// identifier" and "no identifier". Using `Option<Ident>` is preferable,
2320+
/// where possible, because that is unambiguous.
23082321
#[inline]
23092322
pub fn empty() -> Ident {
23102323
Ident::with_dummy_span(kw::Empty)
23112324
}
23122325

2326+
// For dummy identifiers that are never used and absolutely must be
2327+
// present, it's better to use `Ident::dummy` than `Ident::Empty`, because
2328+
// it's clearer that it's intended as a dummy value, and more likely to be
2329+
// detected if it accidentally does get used.
2330+
#[inline]
2331+
pub fn dummy() -> Ident {
2332+
Ident::with_dummy_span(sym::dummy)
2333+
}
2334+
23132335
/// Maps a string to an identifier with a dummy span.
23142336
pub fn from_str(string: &str) -> Ident {
23152337
Ident::with_dummy_span(Symbol::intern(string))

0 commit comments

Comments
 (0)
Failed to load comments.