Skip to content

Commit e1b44f6

Browse files
committed
Introduce sym::dummy and Ident::dummy.
The idea is to identify cases of symbols/identifiers that are not expected to be used. There isn't a perfectly sharp line between "dummy" and "not dummy", but I think it's useful nonetheless.
1 parent cdaf180 commit e1b44f6

File tree

8 files changed

+31
-10
lines changed

8 files changed

+31
-10
lines changed

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@ impl DummyAstNode for Item {
19361936
span: Default::default(),
19371937
tokens: Default::default(),
19381938
},
1939-
ident: Ident::empty(),
1939+
ident: Ident::dummy(),
19401940
kind: ItemKind::ExternCrate(None),
19411941
tokens: Default::default(),
19421942
}

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_resolve/src/diagnostics.rs

+1-1
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
_ => return None,
22722272
}

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 gets used.
3645
Empty: "",
3746
PathRoot: "{{root}}",
3847
DollarCrate: "$crate",
@@ -833,6 +842,7 @@ symbols! {
833842
drop_types_in_const,
834843
dropck_eyepatch,
835844
dropck_parametricity,
845+
dummy: "<!dummy!>", // use this instead of `kw::Empty` for symbols that won't be used
836846
dummy_cgu_name,
837847
dylib,
838848
dyn_compatible_for_dispatch,
@@ -2303,11 +2313,23 @@ impl Ident {
23032313
Ident::new(name, DUMMY_SP)
23042314
}
23052315

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

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

src/tools/clippy/clippy_lints/src/significant_drop_tightening.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,10 @@ impl Default for AuxParamsAttr {
395395
has_expensive_expr_after_last_attr: false,
396396
first_block_hir_id: HirId::INVALID,
397397
first_block_span: DUMMY_SP,
398-
first_bind_ident: Ident::empty(),
398+
first_bind_ident: Ident::dummy(),
399399
first_method_span: DUMMY_SP,
400400
first_stmt_span: DUMMY_SP,
401-
last_bind_ident: Ident::empty(),
401+
last_bind_ident: Ident::dummy(),
402402
last_method_span: DUMMY_SP,
403403
last_stmt_span: DUMMY_SP,
404404
}

0 commit comments

Comments
 (0)