Skip to content

Commit 5833dfa

Browse files
authored
Rollup merge of #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 53e4e6f + 4eadaff commit 5833dfa

10 files changed

Lines changed: 41 additions & 21 deletions

File tree

‎compiler/rustc_ast/src/mut_visit.rs‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 1 addition & 2 deletions
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
usecrate::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‎

Lines changed: 2 additions & 2 deletions
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::{selfas 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
pubstructSegmentIterator<'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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'hir> PathSegment<'hir> {
243243
}
244244

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

249249
pubfnargs(&self) -> &GenericArgs<'hir>{

‎compiler/rustc_hir_typeck/src/expr.rs‎

Lines changed: 1 addition & 1 deletion
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
}elseifself.method_exists_for_diagnostic(
29382938
field,
29392939
base_ty,

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -832,15 +832,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
832832

833833
let trait_missing_method =
834834
matches!(error, method::MethodError::NoMatch(_)) && ty.normalized.is_trait();
835-
if item_name.name != kw::Empty{
836-
self.report_method_error(
837-
hir_id,
838-
ty.normalized,
839-
error,
840-
Expectation::NoExpectation,
841-
trait_missing_method && span.edition().at_least_rust_2021(),// emits missing method for trait only after edition 2021
842-
);
843-
}
835+
assert_ne!(item_name.name, kw::Empty);
836+
self.report_method_error(
837+
hir_id,
838+
ty.normalized,
839+
error,
840+
Expectation::NoExpectation,
841+
trait_missing_method && span.edition().at_least_rust_2021(),// emits missing method for trait only after edition 2021
842+
);
844843

845844
result
846845
});

‎compiler/rustc_privacy/src/lib.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_middle::ty::{
4040
use rustc_middle::{bug, span_bug};
4141
use rustc_session::lint;
4242
use rustc_span::hygiene::Transparency;
43-
use rustc_span::{Ident,Span,Symbol,kw,sym};
43+
use rustc_span::{Ident,Span,Symbol, sym};
4444
use tracing::debug;
4545
use{rustc_attr_parsing as attr, rustc_hir as hir};
4646

@@ -935,8 +935,8 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
935935
}
936936

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

‎compiler/rustc_resolve/src/diagnostics.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2266,7 +2266,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22662266
&& !first.ident.is_path_segment_keyword() =>
22672267
{
22682268
// Insert a placeholder that's later replaced by `self`/`super`/etc.
2269-
path.insert(0,Segment::from_ident(Ident::empty()));
2269+
path.insert(0,Segment::from_ident(Ident::dummy()));
22702270
}
22712271
_ => returnNone,
22722272
}

‎compiler/rustc_span/src/symbol.rs‎

Lines changed: 22 additions & 0 deletions
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
pubfnempty() -> 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+
pubfndummy() -> Ident{
2332+
Ident::with_dummy_span(sym::dummy)
2333+
}
2334+
23132335
/// Maps a string to an identifier with a dummy span.
23142336
pubfnfrom_str(string:&str) -> Ident{
23152337
Ident::with_dummy_span(Symbol::intern(string))

0 commit comments

Comments
 (0)