Skip to content

Reduce kw::Empty usage, part 2 #138347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1948,7 +1948,7 @@ impl DummyAstNode for Item {
span: Default::default(),
tokens: Default::default(),
},
ident: Ident::empty(),
ident: Ident::dummy(),
kind: ItemKind::ExternCrate(None),
tokens: Default::default(),
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use rustc_errors::{DiagCtxtHandle, Diagnostic};
use rustc_feature::Features;
use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId};
use rustc_session::Session;
use rustc_span::symbol::kw;
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};

use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
Expand Down Expand Up @@ -338,7 +337,7 @@ impl<'sess> AttributeParser<'sess> {
"expr in place where literal is expected (builtin attr parsing)",
);
ast::MetaItemLit {
symbol: kw::Empty,
symbol: sym::dummy,
suffix: None,
kind: ast::LitKind::Err(guar),
span: DUMMY_SP,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_attr_parsing/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_ast::{AttrArgs, DelimArgs, Expr, ExprKind, LitKind, MetaItemLit, Norma
use rustc_ast_pretty::pprust;
use rustc_errors::DiagCtxtHandle;
use rustc_hir::{self as hir, AttrPath};
use rustc_span::symbol::{Ident, kw};
use rustc_span::symbol::{Ident, kw, sym};
use rustc_span::{ErrorGuaranteed, Span, Symbol};

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

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ fn parse_tree<'a>(
let msg =
format!("expected identifier, found `{}`", pprust::token_to_string(token),);
sess.dcx().span_err(token.span, msg);
TokenTree::MetaVar(token.span, Ident::empty())
TokenTree::MetaVar(token.span, Ident::dummy())
}

// There are no more tokens. Just return the `$` we already have.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<'hir> PathSegment<'hir> {
}

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

pub fn args(&self) -> &GenericArgs<'hir> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2933,7 +2933,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

let guar = if field.name == kw::Empty {
self.dcx().span_delayed_bug(field.span, "field name with no name")
self.dcx().span_bug(field.span, "field name with no name")
} else if self.method_exists_for_diagnostic(
field,
base_ty,
Expand Down
17 changes: 8 additions & 9 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,15 +825,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let trait_missing_method =
matches!(error, method::MethodError::NoMatch(_)) && ty.normalized.is_trait();
if item_name.name != kw::Empty {
self.report_method_error(
hir_id,
ty.normalized,
error,
Expectation::NoExpectation,
trait_missing_method && span.edition().at_least_rust_2021(), // emits missing method for trait only after edition 2021
);
}
assert_ne!(item_name.name, kw::Empty);
self.report_method_error(
hir_id,
ty.normalized,
error,
Expectation::NoExpectation,
trait_missing_method && span.edition().at_least_rust_2021(), // emits missing method for trait only after edition 2021
);

result
});
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use rustc_middle::ty::{
use rustc_middle::{bug, span_bug};
use rustc_session::lint;
use rustc_span::hygiene::Transparency;
use rustc_span::{Ident, Span, Symbol, kw, sym};
use rustc_span::{Ident, Span, Symbol, sym};
use tracing::debug;
use {rustc_attr_parsing as attr, rustc_hir as hir};

Expand Down Expand Up @@ -935,8 +935,8 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
}

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

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2266,7 +2266,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
&& !first.ident.is_path_segment_keyword() =>
{
// Insert a placeholder that's later replaced by `self`/`super`/etc.
path.insert(0, Segment::from_ident(Ident::empty()));
path.insert(0, Segment::from_ident(Ident::dummy()));
}
_ => return None,
}
Expand Down
22 changes: 22 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ symbols! {
// Special reserved identifiers used internally for elided lifetimes,
// unnamed method parameters, crate root module, error recovery etc.
// Matching predicates: `is_any_keyword`, `is_special`/`is_reserved`
//
// Notes about `kw::Empty`:
// - Its use can blur the lines between "empty symbol" and "no symbol".
// Using `Option<Symbol>` is preferable, where possible, because that
// is unambiguous.
// - For dummy symbols that are never used and absolutely must be
// present, it's better to use `sym::dummy` than `kw::Empty`, because
// it's clearer that it's intended as a dummy value, and more likely
// to be detected if it accidentally does get used.
Empty: "",
PathRoot: "{{root}}",
DollarCrate: "$crate",
Expand Down Expand Up @@ -834,6 +843,7 @@ symbols! {
drop_types_in_const,
dropck_eyepatch,
dropck_parametricity,
dummy: "<!dummy!>", // use this instead of `kw::Empty` for symbols that won't be used
dummy_cgu_name,
dylib,
dyn_compatible_for_dispatch,
Expand Down Expand Up @@ -2305,11 +2315,23 @@ impl Ident {
Ident::new(name, DUMMY_SP)
}

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

// For dummy identifiers that are never used and absolutely must be
// present, it's better to use `Ident::dummy` than `Ident::Empty`, because
// it's clearer that it's intended as a dummy value, and more likely to be
// detected if it accidentally does get used.
#[inline]
pub fn dummy() -> Ident {
Ident::with_dummy_span(sym::dummy)
}

/// Maps a string to an identifier with a dummy span.
pub fn from_str(string: &str) -> Ident {
Ident::with_dummy_span(Symbol::intern(string))
Expand Down
Loading