Skip to content

Commit 8992db0

Browse files
committed
Remove '_ from the keyword list.
The anonymous lifetime `'_` is not a keyword according to the Reference and the Ferrocene spec. This commit changes it to a symbol. This changes the behaviour of the `is_any_keyword` predicate, but that has no effect in practice because nothing depended on that behaviour. (Rust-analyzer calls its equivalent constant `tick_underscore`. I don't have a strong preference for either name, I just went with `underscore_lifetime` because it is similar to the old keyword name.)
1 parent 7eb1f37 commit 8992db0

File tree

19 files changed

+44
-41
lines changed

19 files changed

+44
-41
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
762762
let _def_id = self.create_def(
763763
self.current_hir_id_owner.def_id,
764764
param,
765-
kw::UnderscoreLifetime,
765+
sym::underscore_lifetime,
766766
DefKind::LifetimeParam,
767767
ident.span,
768768
);
@@ -1194,7 +1194,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11941194
self.next_node_id()
11951195
};
11961196
let span = self.tcx.sess.source_map().start_point(t.span).shrink_to_hi();
1197-
Lifetime { ident: Ident::new(kw::UnderscoreLifetime, span), id }
1197+
Lifetime { ident: Ident::new(sym::underscore_lifetime, span), id }
11981198
});
11991199
let lifetime = self.lower_lifetime(&region);
12001200
hir::TyKind::Ref(lifetime, self.lower_mt(mt, itctx))
@@ -1210,7 +1210,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12101210
self.next_node_id()
12111211
};
12121212
let span = self.tcx.sess.source_map().start_point(t.span).shrink_to_hi();
1213-
Lifetime { ident: Ident::new(kw::UnderscoreLifetime, span), id }
1213+
Lifetime { ident: Ident::new(sym::underscore_lifetime, span), id }
12141214
});
12151215
let lifetime = self.lower_lifetime(&region);
12161216
let kind = hir::TyKind::Ref(lifetime, self.lower_mt(mt, itctx));

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::mir::{
1717
};
1818
use rustc_middle::ty::adjustment::PointerCoercion;
1919
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
20-
use rustc_span::symbol::{Symbol, kw};
20+
use rustc_span::symbol::Symbol;
2121
use rustc_span::{DesugaringKind, Span, sym};
2222
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
2323
use tracing::{debug, instrument};
@@ -418,7 +418,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
418418
}
419419
if let ConstraintCategory::OpaqueType = category {
420420
let suggestable_name =
421-
if region_name.was_named() { region_name.name } else { kw::UnderscoreLifetime };
421+
if region_name.was_named() { region_name.name } else { sym::underscore_lifetime };
422422

423423
let msg = format!(
424424
"you can add a bound to the {}to make it last less than `'static` and match `{region_name}`",

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_middle::hir::place::PlaceBase;
1616
use rustc_middle::mir::{ConstraintCategory, ReturnConstraint};
1717
use rustc_middle::ty::{self, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeVisitor};
1818
use rustc_span::Span;
19-
use rustc_span::symbol::{Ident, kw};
19+
use rustc_span::symbol::{Ident, kw, sym};
2020
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
2121
use rustc_trait_selection::error_reporting::infer::nice_region_error::{
2222
self, HirTraitObjectVisitor, NiceRegionError, TraitObjectVisitor, find_anon_type,
@@ -786,7 +786,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
786786
// These situations are bound to result in errors.
787787
// To prevent an immediate ICE, we opt to create a dummy name instead.
788788
let fr_name = self.give_region_a_name(*fr).unwrap_or(RegionName {
789-
name: kw::UnderscoreLifetime,
789+
name: sym::underscore_lifetime,
790790
source: RegionNameSource::Static,
791791
});
792792
fr_name.highlight_region_name(&mut diag);
@@ -858,7 +858,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
858858
return;
859859
};
860860

861-
let lifetime = if f.has_name() { fr_name.name } else { kw::UnderscoreLifetime };
861+
let lifetime = if f.has_name() { fr_name.name } else { sym::underscore_lifetime };
862862

863863
let arg = match param.param.pat.simple_ident() {
864864
Some(simple_ident) => format!("argument `{simple_ident}`"),

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl ParamName {
7575
pub fn ident(&self) -> Ident {
7676
match *self {
7777
ParamName::Plain(ident) => ident,
78-
ParamName::Fresh | ParamName::Error => Ident::with_dummy_span(kw::UnderscoreLifetime),
78+
ParamName::Fresh | ParamName::Error => Ident::with_dummy_span(sym::underscore_lifetime),
7979
}
8080
}
8181
}
@@ -149,7 +149,7 @@ impl Lifetime {
149149
}
150150

151151
pub fn is_anonymous(&self) -> bool {
152-
self.ident.name == kw::Empty || self.ident.name == kw::UnderscoreLifetime
152+
self.ident.name == kw::Empty || self.ident.name == sym::underscore_lifetime
153153
}
154154

155155
pub fn suggestion_position(&self) -> (LifetimeSuggestionPosition, Span) {

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use rustc_middle::ty::{
4545
use rustc_middle::{bug, span_bug};
4646
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
4747
use rustc_span::edit_distance::find_best_match_for_name;
48-
use rustc_span::symbol::{Ident, Symbol, kw};
48+
use rustc_span::symbol::{Ident, Symbol, kw, sym};
4949
use rustc_span::{DUMMY_SP, Span};
5050
use rustc_trait_selection::infer::InferCtxtExt;
5151
use rustc_trait_selection::traits::wf::object_region_bounds;
@@ -2688,15 +2688,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26882688
) {
26892689
for br in referenced_regions.difference(&constrained_regions) {
26902690
let br_name = match *br {
2691-
ty::BoundRegionKind::Named(_, kw::UnderscoreLifetime)
2691+
ty::BoundRegionKind::Named(_, sym::underscore_lifetime)
26922692
| ty::BoundRegionKind::Anon
26932693
| ty::BoundRegionKind::ClosureEnv => "an anonymous lifetime".to_string(),
26942694
ty::BoundRegionKind::Named(_, name) => format!("lifetime `{name}`"),
26952695
};
26962696

26972697
let mut err = generate_err(&br_name);
26982698

2699-
if let ty::BoundRegionKind::Named(_, kw::UnderscoreLifetime)
2699+
if let ty::BoundRegionKind::Named(_, sym::underscore_lifetime)
27002700
| ty::BoundRegionKind::Anon = *br
27012701
{
27022702
// The only way for an anonymous lifetime to wind up

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_data_structures::fx::FxHashMap;
33
use rustc_hir::def_id::DefId;
44
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
55
use rustc_span::Span;
6-
use rustc_span::symbol::{Symbol, kw};
6+
use rustc_span::symbol::{Symbol, kw, sym};
77
use tracing::instrument;
88

99
use super::{Clause, InstantiatedPredicates, ParamConst, ParamTy, Ty, TyCtxt};
@@ -75,7 +75,7 @@ impl GenericParamDef {
7575
pub fn is_anonymous_lifetime(&self) -> bool {
7676
match self.kind {
7777
GenericParamDefKind::Lifetime => {
78-
self.name == kw::UnderscoreLifetime || self.name == kw::Empty
78+
self.name == sym::underscore_lifetime || self.name == kw::Empty
7979
}
8080
_ => false,
8181
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl EarlyParamRegion {
453453
/// Does this early bound region have a name? Early bound regions normally
454454
/// always have names except when using anonymous lifetimes (`'_`).
455455
pub fn has_name(&self) -> bool {
456-
self.name != kw::UnderscoreLifetime && self.name != kw::Empty
456+
self.name != sym::underscore_lifetime && self.name != kw::Empty
457457
}
458458
}
459459

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2676,7 +2676,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
26762676

26772677
(name, ty::BoundRegionKind::Named(CRATE_DEF_ID.to_def_id(), name))
26782678
}
2679-
ty::BoundRegionKind::Named(def_id, kw::UnderscoreLifetime | kw::Empty) => {
2679+
ty::BoundRegionKind::Named(def_id, sym::underscore_lifetime | kw::Empty) => {
26802680
let name = next_name(self);
26812681

26822682
if let Some(lt_idx) = lifetime_idx {

compiler/rustc_middle/src/ty/region.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ impl core::fmt::Debug for BoundRegion {
411411
impl BoundRegionKind {
412412
pub fn is_named(&self) -> bool {
413413
match *self {
414-
BoundRegionKind::Named(_, name) => name != kw::UnderscoreLifetime && name != kw::Empty,
414+
BoundRegionKind::Named(_, name) => {
415+
name != sym::underscore_lifetime && name != kw::Empty
416+
}
415417
_ => false,
416418
}
417419
}

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ impl<'a> Parser<'a> {
12901290
if let Some((ident, is_raw)) = self.token.lifetime() {
12911291
if matches!(is_raw, IdentIsRaw::No)
12921292
&& ident.without_first_quote().is_reserved()
1293-
&& ![kw::UnderscoreLifetime, kw::StaticLifetime].contains(&ident.name)
1293+
&& ![sym::underscore_lifetime, kw::StaticLifetime].contains(&ident.name)
12941294
{
12951295
self.dcx().emit_err(errors::KeywordLifetime { span: ident.span });
12961296
}

0 commit comments

Comments
 (0)