Skip to content

Commit 15ccaf7

Browse files
committed
Auto merge of #60740 - petrochenkov:kw, r=nnethercote
Simplify use of keyword symbols They mirror non-keyword symbols now (see #60630). `keywords::MyKeyword.name()` -> `kw::MyKeyword` `keywords::MyKeyword.ident()` -> `Ident::with_empty_ctxt(kw::MyKeyword)` (not common) `keywords::Invalid.ident()` -> `Ident::invalid()` (more common) Keywords are simply `Symbol` constants now, the `Keyword` struct is eliminated. This means `kw::MyKeyword` can now be used in `match` in particular.
2 parents 11f01bf + a1885cd commit 15ccaf7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+544
-573
lines changed

src/librustc/hir/lowering.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use syntax::ptr::P;
6464
use syntax::source_map::{respan, CompilerDesugaringKind, Spanned};
6565
use syntax::source_map::CompilerDesugaringKind::IfTemporary;
6666
use syntax::std_inject;
67-
use syntax::symbol::{keywords, Symbol, sym};
67+
use syntax::symbol::{kw, sym, Symbol};
6868
use syntax::tokenstream::{TokenStream, TokenTree};
6969
use syntax::parse::token::Token;
7070
use syntax::visit::{self, Visitor};
@@ -927,11 +927,11 @@ impl<'a> LoweringContext<'a> {
927927
hir::LifetimeParamKind::InBand,
928928
),
929929
ParamName::Fresh(_) => (
930-
keywords::UnderscoreLifetime.name().as_interned_str(),
930+
kw::UnderscoreLifetime.as_interned_str(),
931931
hir::LifetimeParamKind::Elided,
932932
),
933933
ParamName::Error => (
934-
keywords::UnderscoreLifetime.name().as_interned_str(),
934+
kw::UnderscoreLifetime.as_interned_str(),
935935
hir::LifetimeParamKind::Error,
936936
),
937937
};
@@ -1415,7 +1415,7 @@ impl<'a> LoweringContext<'a> {
14151415
P(hir::Path {
14161416
res,
14171417
segments: hir_vec![hir::PathSegment::from_ident(
1418-
keywords::SelfUpper.ident()
1418+
Ident::with_empty_ctxt(kw::SelfUpper)
14191419
)],
14201420
span: t.span,
14211421
}),
@@ -1614,7 +1614,7 @@ impl<'a> LoweringContext<'a> {
16141614
trace!("registering existential type with id {:#?}", exist_ty_id);
16151615
let exist_ty_item = hir::Item {
16161616
hir_id: exist_ty_id,
1617-
ident: keywords::Invalid.ident(),
1617+
ident: Ident::invalid(),
16181618
attrs: Default::default(),
16191619
node: exist_ty_item_kind,
16201620
vis: respan(span.shrink_to_lo(), hir::VisibilityKind::Inherited),
@@ -1747,7 +1747,7 @@ impl<'a> LoweringContext<'a> {
17471747

17481748
let (name, kind) = match name {
17491749
hir::LifetimeName::Underscore => (
1750-
hir::ParamName::Plain(keywords::UnderscoreLifetime.ident()),
1750+
hir::ParamName::Plain(Ident::with_empty_ctxt(kw::UnderscoreLifetime)),
17511751
hir::LifetimeParamKind::Elided,
17521752
),
17531753
hir::LifetimeName::Param(param_name) => (
@@ -2296,7 +2296,7 @@ impl<'a> LoweringContext<'a> {
22962296
.iter()
22972297
.map(|arg| match arg.pat.node {
22982298
PatKind::Ident(_, ident, _) => ident,
2299-
_ => Ident::new(keywords::Invalid.name(), arg.pat.span),
2299+
_ => Ident::new(kw::Invalid, arg.pat.span),
23002300
})
23012301
.collect()
23022302
}
@@ -2585,9 +2585,9 @@ impl<'a> LoweringContext<'a> {
25852585
fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
25862586
let span = l.ident.span;
25872587
match l.ident {
2588-
ident if ident.name == keywords::StaticLifetime.name() =>
2588+
ident if ident.name == kw::StaticLifetime =>
25892589
self.new_named_lifetime(l.id, span, hir::LifetimeName::Static),
2590-
ident if ident.name == keywords::UnderscoreLifetime.name() =>
2590+
ident if ident.name == kw::UnderscoreLifetime =>
25912591
match self.anonymous_lifetime_mode {
25922592
AnonymousLifetimeMode::CreateParameter => {
25932593
let fresh_name = self.collect_fresh_in_band_lifetime(span);
@@ -2709,7 +2709,7 @@ impl<'a> LoweringContext<'a> {
27092709
// Don't expose `Self` (recovered "keyword used as ident" parse error).
27102710
// `rustc::ty` expects `Self` to be only used for a trait's `Self`.
27112711
// Instead, use `gensym("Self")` to create a distinct name that looks the same.
2712-
let ident = if param.ident.name == keywords::SelfUpper.name() {
2712+
let ident = if param.ident.name == kw::SelfUpper {
27132713
param.ident.gensym()
27142714
} else {
27152715
param.ident
@@ -3325,7 +3325,7 @@ impl<'a> LoweringContext<'a> {
33253325

33263326
// Correctly resolve `self` imports.
33273327
if path.segments.len() > 1
3328-
&& path.segments.last().unwrap().ident.name == keywords::SelfLower.name()
3328+
&& path.segments.last().unwrap().ident.name == kw::SelfLower
33293329
{
33303330
let _ = path.segments.pop();
33313331
if rename.is_none() {

src/librustc/hir/map/def_collector.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::session::CrateDisambiguator;
55
use syntax::ast::*;
66
use syntax::ext::hygiene::Mark;
77
use syntax::visit;
8-
use syntax::symbol::keywords;
8+
use syntax::symbol::kw;
99
use syntax::symbol::Symbol;
1010
use syntax::parse::token::{self, Token};
1111
use syntax_pos::Span;
@@ -138,7 +138,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
138138
// information we encapsulate into, the better
139139
let def_data = match i.node {
140140
ItemKind::Impl(..) => DefPathData::Impl,
141-
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
141+
ItemKind::Mod(..) if i.ident.name == kw::Invalid => {
142142
return visit::walk_item(self, i);
143143
}
144144
ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) |

src/librustc/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl<'hir> Map<'hir> {
519519
pub fn ty_param_name(&self, id: HirId) -> Name {
520520
match self.get_by_hir_id(id) {
521521
Node::Item(&Item { node: ItemKind::Trait(..), .. }) |
522-
Node::Item(&Item { node: ItemKind::TraitAlias(..), .. }) => keywords::SelfUpper.name(),
522+
Node::Item(&Item { node: ItemKind::TraitAlias(..), .. }) => kw::SelfUpper,
523523
Node::GenericParam(param) => param.name.ident().name,
524524
_ => bug!("ty_param_name: {} not a type parameter", self.hir_to_string(id)),
525525
}

src/librustc/hir/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use syntax::ast::{Attribute, Label, LitKind, StrStyle, FloatTy, IntTy, UintTy};
2424
use syntax::attr::{InlineAttr, OptimizeAttr};
2525
use syntax::ext::hygiene::SyntaxContext;
2626
use syntax::ptr::P;
27-
use syntax::symbol::{Symbol, keywords};
27+
use syntax::symbol::{Symbol, kw};
2828
use syntax::tokenstream::TokenStream;
2929
use syntax::util::parser::ExprPrecedence;
3030
use crate::ty::AdtKind;
@@ -160,7 +160,7 @@ pub struct Lifetime {
160160
pub span: Span,
161161

162162
/// Either "`'a`", referring to a named lifetime definition,
163-
/// or "``" (i.e., `keywords::Invalid`), for elision placeholders.
163+
/// or "``" (i.e., `kw::Invalid`), for elision placeholders.
164164
///
165165
/// HIR lowering inserts these placeholders in type paths that
166166
/// refer to type definitions needing lifetime parameters,
@@ -199,7 +199,8 @@ impl ParamName {
199199
pub fn ident(&self) -> Ident {
200200
match *self {
201201
ParamName::Plain(ident) => ident,
202-
ParamName::Error | ParamName::Fresh(_) => keywords::UnderscoreLifetime.ident(),
202+
ParamName::Fresh(_) |
203+
ParamName::Error => Ident::with_empty_ctxt(kw::UnderscoreLifetime),
203204
}
204205
}
205206

@@ -233,10 +234,9 @@ pub enum LifetimeName {
233234
impl LifetimeName {
234235
pub fn ident(&self) -> Ident {
235236
match *self {
236-
LifetimeName::Implicit => keywords::Invalid.ident(),
237-
LifetimeName::Error => keywords::Invalid.ident(),
238-
LifetimeName::Underscore => keywords::UnderscoreLifetime.ident(),
239-
LifetimeName::Static => keywords::StaticLifetime.ident(),
237+
LifetimeName::Implicit | LifetimeName::Error => Ident::invalid(),
238+
LifetimeName::Underscore => Ident::with_empty_ctxt(kw::UnderscoreLifetime),
239+
LifetimeName::Static => Ident::with_empty_ctxt(kw::StaticLifetime),
240240
LifetimeName::Param(param_name) => param_name.ident(),
241241
}
242242
}
@@ -305,7 +305,7 @@ pub struct Path {
305305

306306
impl Path {
307307
pub fn is_global(&self) -> bool {
308-
!self.segments.is_empty() && self.segments[0].ident.name == keywords::PathRoot.name()
308+
!self.segments.is_empty() && self.segments[0].ident.name == kw::PathRoot
309309
}
310310
}
311311

src/librustc/hir/print.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syntax::print::pp::{self, Breaks};
77
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
88
use syntax::print::pprust::{self, PrintState};
99
use syntax::ptr::P;
10-
use syntax::symbol::keywords;
10+
use syntax::symbol::kw;
1111
use syntax::util::parser::{self, AssocOp, Fixity};
1212
use syntax_pos::{self, BytePos, FileName};
1313

@@ -798,7 +798,7 @@ impl<'a> State<'a> {
798798
hir::VisibilityKind::Restricted { ref path, .. } => {
799799
self.s.word("pub(")?;
800800
if path.segments.len() == 1 &&
801-
path.segments[0].ident.name == keywords::Super.name() {
801+
path.segments[0].ident.name == kw::Super {
802802
// Special case: `super` can print like `pub(super)`.
803803
self.s.word("super")?;
804804
} else {
@@ -1559,7 +1559,7 @@ impl<'a> State<'a> {
15591559
if i > 0 {
15601560
self.s.word("::")?
15611561
}
1562-
if segment.ident.name != keywords::PathRoot.name() {
1562+
if segment.ident.name != kw::PathRoot {
15631563
self.print_ident(segment.ident)?;
15641564
segment.with_generic_args(|generic_args| {
15651565
self.print_generic_args(generic_args, segment.infer_types,
@@ -1572,7 +1572,7 @@ impl<'a> State<'a> {
15721572
}
15731573

15741574
pub fn print_path_segment(&mut self, segment: &hir::PathSegment) -> io::Result<()> {
1575-
if segment.ident.name != keywords::PathRoot.name() {
1575+
if segment.ident.name != kw::PathRoot {
15761576
self.print_ident(segment.ident)?;
15771577
segment.with_generic_args(|generic_args| {
15781578
self.print_generic_args(generic_args, segment.infer_types, false)
@@ -1599,7 +1599,7 @@ impl<'a> State<'a> {
15991599
if i > 0 {
16001600
self.s.word("::")?
16011601
}
1602-
if segment.ident.name != keywords::PathRoot.name() {
1602+
if segment.ident.name != kw::PathRoot {
16031603
self.print_ident(segment.ident)?;
16041604
segment.with_generic_args(|generic_args| {
16051605
self.print_generic_args(generic_args,

src/librustc/middle/liveness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ use std::io;
112112
use std::rc::Rc;
113113
use syntax::ast::{self, NodeId};
114114
use syntax::ptr::P;
115-
use syntax::symbol::{keywords, sym};
115+
use syntax::symbol::{kw, sym};
116116
use syntax_pos::Span;
117117

118118
use crate::hir;
@@ -1552,7 +1552,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15521552
let sp = ident.span;
15531553
let var = self.variable(hir_id, sp);
15541554
// Ignore unused self.
1555-
if ident.name != keywords::SelfLower.name() {
1555+
if ident.name != kw::SelfLower {
15561556
if !self.warn_about_unused(vec![sp], hir_id, entry_ln, var) {
15571557
if self.live_on_entry(entry_ln, var).is_none() {
15581558
self.report_dead_assign(hir_id, sp, var, true);

src/librustc/middle/resolve_lifetime.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::mem::replace;
2323
use syntax::ast;
2424
use syntax::attr;
2525
use syntax::ptr::P;
26-
use syntax::symbol::{keywords, sym};
26+
use syntax::symbol::{kw, sym};
2727
use syntax_pos::Span;
2828

2929
use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
@@ -711,7 +711,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
711711
GenericParamKind::Lifetime { .. } => {
712712
let (name, reg) = Region::early(&self.tcx.hir(), &mut index, &param);
713713
if let hir::ParamName::Plain(param_name) = name {
714-
if param_name.name == keywords::UnderscoreLifetime.name() {
714+
if param_name.name == kw::UnderscoreLifetime {
715715
// Pick the elided lifetime "definition" if one exists
716716
// and use it to make an elision scope.
717717
elision = Some(reg);
@@ -1602,7 +1602,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
16021602
} {
16031603
debug!("id = {:?} span = {:?} name = {:?}", id, span, name);
16041604

1605-
if name == keywords::UnderscoreLifetime.ident() {
1605+
if name.name == kw::UnderscoreLifetime {
16061606
continue;
16071607
}
16081608

@@ -2525,8 +2525,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
25252525
for (i, (lifetime_i, lifetime_i_name)) in lifetimes.iter().enumerate() {
25262526
if let hir::ParamName::Plain(_) = lifetime_i_name {
25272527
let name = lifetime_i_name.ident().name;
2528-
if name == keywords::UnderscoreLifetime.name()
2529-
|| name == keywords::StaticLifetime.name()
2528+
if name == kw::UnderscoreLifetime
2529+
|| name == kw::StaticLifetime
25302530
{
25312531
let mut err = struct_span_err!(
25322532
self.tcx.sess,

src/librustc/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use syntax::ast;
7474
use syntax::attr;
7575
use syntax::source_map::MultiSpan;
7676
use syntax::feature_gate;
77-
use syntax::symbol::{Symbol, keywords, InternedString, sym};
77+
use syntax::symbol::{Symbol, InternedString, kw, sym};
7878
use syntax_pos::Span;
7979

8080
use crate::hir;
@@ -2735,7 +2735,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
27352735

27362736
#[inline]
27372737
pub fn mk_self_type(self) -> Ty<'tcx> {
2738-
self.mk_ty_param(0, keywords::SelfUpper.name().as_interned_str())
2738+
self.mk_ty_param(0, kw::SelfUpper.as_interned_str())
27392739
}
27402740

27412741
pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> Kind<'tcx> {

src/librustc/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use std::ops::Range;
4747
use syntax::ast::{self, Name, Ident, NodeId};
4848
use syntax::attr;
4949
use syntax::ext::hygiene::Mark;
50-
use syntax::symbol::{keywords, sym, Symbol, LocalInternedString, InternedString};
50+
use syntax::symbol::{kw, sym, Symbol, LocalInternedString, InternedString};
5151
use syntax_pos::Span;
5252

5353
use smallvec;
@@ -835,7 +835,7 @@ impl ty::EarlyBoundRegion {
835835
/// Does this early bound region have a name? Early bound regions normally
836836
/// always have names except when using anonymous lifetimes (`'_`).
837837
pub fn has_name(&self) -> bool {
838-
self.name != keywords::UnderscoreLifetime.name().as_interned_str()
838+
self.name != kw::UnderscoreLifetime.as_interned_str()
839839
}
840840
}
841841

src/librustc/ty/print/pretty.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
88
use crate::ty::subst::{Kind, Subst, UnpackedKind};
99
use crate::mir::interpret::ConstValue;
1010
use rustc_target::spec::abi::Abi;
11-
use syntax::symbol::{keywords, InternedString};
11+
use syntax::symbol::{kw, InternedString};
1212

1313
use std::cell::Cell;
1414
use std::fmt::{self, Write as _};
@@ -980,7 +980,7 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
980980
if self.tcx.sess.rust_2018() {
981981
// We add the `crate::` keyword on Rust 2018, only when desired.
982982
if SHOULD_PREFIX_WITH_CRATE.with(|flag| flag.get()) {
983-
write!(self, "{}", keywords::Crate.name())?;
983+
write!(self, "{}", kw::Crate)?;
984984
self.empty_path = false;
985985
}
986986
}
@@ -1140,16 +1140,16 @@ impl<F: fmt::Write> PrettyPrinter<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F>
11401140

11411141
match *region {
11421142
ty::ReEarlyBound(ref data) => {
1143-
data.name.as_symbol() != keywords::Invalid.name() &&
1144-
data.name.as_symbol() != keywords::UnderscoreLifetime.name()
1143+
data.name.as_symbol() != kw::Invalid &&
1144+
data.name.as_symbol() != kw::UnderscoreLifetime
11451145
}
11461146

11471147
ty::ReLateBound(_, br) |
11481148
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
11491149
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
11501150
if let ty::BrNamed(_, name) = br {
1151-
if name.as_symbol() != keywords::Invalid.name() &&
1152-
name.as_symbol() != keywords::UnderscoreLifetime.name() {
1151+
if name.as_symbol() != kw::Invalid &&
1152+
name.as_symbol() != kw::UnderscoreLifetime {
11531153
return true;
11541154
}
11551155
}
@@ -1205,7 +1205,7 @@ impl<F: fmt::Write> FmtPrinter<'_, '_, '_, F> {
12051205
// `explain_region()` or `note_and_explain_region()`.
12061206
match *region {
12071207
ty::ReEarlyBound(ref data) => {
1208-
if data.name.as_symbol() != keywords::Invalid.name() {
1208+
if data.name.as_symbol() != kw::Invalid {
12091209
p!(write("{}", data.name));
12101210
return Ok(self);
12111211
}
@@ -1214,8 +1214,8 @@ impl<F: fmt::Write> FmtPrinter<'_, '_, '_, F> {
12141214
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
12151215
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
12161216
if let ty::BrNamed(_, name) = br {
1217-
if name.as_symbol() != keywords::Invalid.name() &&
1218-
name.as_symbol() != keywords::UnderscoreLifetime.name() {
1217+
if name.as_symbol() != kw::Invalid &&
1218+
name.as_symbol() != kw::UnderscoreLifetime {
12191219
p!(write("{}", name));
12201220
return Ok(self);
12211221
}

src/librustc/ty/sty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::marker::PhantomData;
2222
use std::ops::Range;
2323
use rustc_target::spec::abi;
2424
use syntax::ast::{self, Ident};
25-
use syntax::symbol::{keywords, InternedString};
25+
use syntax::symbol::{kw, InternedString};
2626

2727
use serialize;
2828
use self::InferTy::*;
@@ -1121,7 +1121,7 @@ impl<'a, 'gcx, 'tcx> ParamTy {
11211121
}
11221122

11231123
pub fn for_self() -> ParamTy {
1124-
ParamTy::new(0, keywords::SelfUpper.name().as_interned_str())
1124+
ParamTy::new(0, kw::SelfUpper.as_interned_str())
11251125
}
11261126

11271127
pub fn for_def(def: &ty::GenericParamDef) -> ParamTy {
@@ -1136,7 +1136,7 @@ impl<'a, 'gcx, 'tcx> ParamTy {
11361136
// FIXME(#50125): Ignoring `Self` with `index != 0` might lead to weird behavior elsewhere,
11371137
// but this should only be possible when using `-Z continue-parse-after-error` like
11381138
// `compile-fail/issue-36638.rs`.
1139-
self.name.as_symbol() == keywords::SelfUpper.name() && self.index == 0
1139+
self.name.as_symbol() == kw::SelfUpper && self.index == 0
11401140
}
11411141
}
11421142

0 commit comments

Comments
 (0)