Skip to content

Commit

Permalink
Auto merge of #60740 - petrochenkov:kw, r=nnethercote
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed May 23, 2019
2 parents 11f01bf + a1885cd commit 15ccaf7
Show file tree
Hide file tree
Showing 63 changed files with 544 additions and 573 deletions.
22 changes: 11 additions & 11 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use syntax::ptr::P;
use syntax::source_map::{respan, CompilerDesugaringKind, Spanned};
use syntax::source_map::CompilerDesugaringKind::IfTemporary;
use syntax::std_inject;
use syntax::symbol::{keywords, Symbol, sym};
use syntax::symbol::{kw, sym, Symbol};
use syntax::tokenstream::{TokenStream, TokenTree};
use syntax::parse::token::Token;
use syntax::visit::{self, Visitor};
Expand Down Expand Up @@ -927,11 +927,11 @@ impl<'a> LoweringContext<'a> {
hir::LifetimeParamKind::InBand,
),
ParamName::Fresh(_) => (
keywords::UnderscoreLifetime.name().as_interned_str(),
kw::UnderscoreLifetime.as_interned_str(),
hir::LifetimeParamKind::Elided,
),
ParamName::Error => (
keywords::UnderscoreLifetime.name().as_interned_str(),
kw::UnderscoreLifetime.as_interned_str(),
hir::LifetimeParamKind::Error,
),
};
Expand Down Expand Up @@ -1415,7 +1415,7 @@ impl<'a> LoweringContext<'a> {
P(hir::Path {
res,
segments: hir_vec![hir::PathSegment::from_ident(
keywords::SelfUpper.ident()
Ident::with_empty_ctxt(kw::SelfUpper)
)],
span: t.span,
}),
Expand Down Expand Up @@ -1614,7 +1614,7 @@ impl<'a> LoweringContext<'a> {
trace!("registering existential type with id {:#?}", exist_ty_id);
let exist_ty_item = hir::Item {
hir_id: exist_ty_id,
ident: keywords::Invalid.ident(),
ident: Ident::invalid(),
attrs: Default::default(),
node: exist_ty_item_kind,
vis: respan(span.shrink_to_lo(), hir::VisibilityKind::Inherited),
Expand Down Expand Up @@ -1747,7 +1747,7 @@ impl<'a> LoweringContext<'a> {

let (name, kind) = match name {
hir::LifetimeName::Underscore => (
hir::ParamName::Plain(keywords::UnderscoreLifetime.ident()),
hir::ParamName::Plain(Ident::with_empty_ctxt(kw::UnderscoreLifetime)),
hir::LifetimeParamKind::Elided,
),
hir::LifetimeName::Param(param_name) => (
Expand Down Expand Up @@ -2296,7 +2296,7 @@ impl<'a> LoweringContext<'a> {
.iter()
.map(|arg| match arg.pat.node {
PatKind::Ident(_, ident, _) => ident,
_ => Ident::new(keywords::Invalid.name(), arg.pat.span),
_ => Ident::new(kw::Invalid, arg.pat.span),
})
.collect()
}
Expand Down Expand Up @@ -2585,9 +2585,9 @@ impl<'a> LoweringContext<'a> {
fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
let span = l.ident.span;
match l.ident {
ident if ident.name == keywords::StaticLifetime.name() =>
ident if ident.name == kw::StaticLifetime =>
self.new_named_lifetime(l.id, span, hir::LifetimeName::Static),
ident if ident.name == keywords::UnderscoreLifetime.name() =>
ident if ident.name == kw::UnderscoreLifetime =>
match self.anonymous_lifetime_mode {
AnonymousLifetimeMode::CreateParameter => {
let fresh_name = self.collect_fresh_in_band_lifetime(span);
Expand Down Expand Up @@ -2709,7 +2709,7 @@ impl<'a> LoweringContext<'a> {
// Don't expose `Self` (recovered "keyword used as ident" parse error).
// `rustc::ty` expects `Self` to be only used for a trait's `Self`.
// Instead, use `gensym("Self")` to create a distinct name that looks the same.
let ident = if param.ident.name == keywords::SelfUpper.name() {
let ident = if param.ident.name == kw::SelfUpper {
param.ident.gensym()
} else {
param.ident
Expand Down Expand Up @@ -3325,7 +3325,7 @@ impl<'a> LoweringContext<'a> {

// Correctly resolve `self` imports.
if path.segments.len() > 1
&& path.segments.last().unwrap().ident.name == keywords::SelfLower.name()
&& path.segments.last().unwrap().ident.name == kw::SelfLower
{
let _ = path.segments.pop();
if rename.is_none() {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::session::CrateDisambiguator;
use syntax::ast::*;
use syntax::ext::hygiene::Mark;
use syntax::visit;
use syntax::symbol::keywords;
use syntax::symbol::kw;
use syntax::symbol::Symbol;
use syntax::parse::token::{self, Token};
use syntax_pos::Span;
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
// information we encapsulate into, the better
let def_data = match i.node {
ItemKind::Impl(..) => DefPathData::Impl,
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
ItemKind::Mod(..) if i.ident.name == kw::Invalid => {
return visit::walk_item(self, i);
}
ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) |
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl<'hir> Map<'hir> {
pub fn ty_param_name(&self, id: HirId) -> Name {
match self.get_by_hir_id(id) {
Node::Item(&Item { node: ItemKind::Trait(..), .. }) |
Node::Item(&Item { node: ItemKind::TraitAlias(..), .. }) => keywords::SelfUpper.name(),
Node::Item(&Item { node: ItemKind::TraitAlias(..), .. }) => kw::SelfUpper,
Node::GenericParam(param) => param.name.ident().name,
_ => bug!("ty_param_name: {} not a type parameter", self.hir_to_string(id)),
}
Expand Down
16 changes: 8 additions & 8 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use syntax::ast::{Attribute, Label, LitKind, StrStyle, FloatTy, IntTy, UintTy};
use syntax::attr::{InlineAttr, OptimizeAttr};
use syntax::ext::hygiene::SyntaxContext;
use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
use syntax::symbol::{Symbol, kw};
use syntax::tokenstream::TokenStream;
use syntax::util::parser::ExprPrecedence;
use crate::ty::AdtKind;
Expand Down Expand Up @@ -160,7 +160,7 @@ pub struct Lifetime {
pub span: Span,

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

Expand Down Expand Up @@ -233,10 +234,9 @@ pub enum LifetimeName {
impl LifetimeName {
pub fn ident(&self) -> Ident {
match *self {
LifetimeName::Implicit => keywords::Invalid.ident(),
LifetimeName::Error => keywords::Invalid.ident(),
LifetimeName::Underscore => keywords::UnderscoreLifetime.ident(),
LifetimeName::Static => keywords::StaticLifetime.ident(),
LifetimeName::Implicit | LifetimeName::Error => Ident::invalid(),
LifetimeName::Underscore => Ident::with_empty_ctxt(kw::UnderscoreLifetime),
LifetimeName::Static => Ident::with_empty_ctxt(kw::StaticLifetime),
LifetimeName::Param(param_name) => param_name.ident(),
}
}
Expand Down Expand Up @@ -305,7 +305,7 @@ pub struct Path {

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

Expand Down
10 changes: 5 additions & 5 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use syntax::print::pp::{self, Breaks};
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
use syntax::print::pprust::{self, PrintState};
use syntax::ptr::P;
use syntax::symbol::keywords;
use syntax::symbol::kw;
use syntax::util::parser::{self, AssocOp, Fixity};
use syntax_pos::{self, BytePos, FileName};

Expand Down Expand Up @@ -798,7 +798,7 @@ impl<'a> State<'a> {
hir::VisibilityKind::Restricted { ref path, .. } => {
self.s.word("pub(")?;
if path.segments.len() == 1 &&
path.segments[0].ident.name == keywords::Super.name() {
path.segments[0].ident.name == kw::Super {
// Special case: `super` can print like `pub(super)`.
self.s.word("super")?;
} else {
Expand Down Expand Up @@ -1559,7 +1559,7 @@ impl<'a> State<'a> {
if i > 0 {
self.s.word("::")?
}
if segment.ident.name != keywords::PathRoot.name() {
if segment.ident.name != kw::PathRoot {
self.print_ident(segment.ident)?;
segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args, segment.infer_types,
Expand All @@ -1572,7 +1572,7 @@ impl<'a> State<'a> {
}

pub fn print_path_segment(&mut self, segment: &hir::PathSegment) -> io::Result<()> {
if segment.ident.name != keywords::PathRoot.name() {
if segment.ident.name != kw::PathRoot {
self.print_ident(segment.ident)?;
segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args, segment.infer_types, false)
Expand All @@ -1599,7 +1599,7 @@ impl<'a> State<'a> {
if i > 0 {
self.s.word("::")?
}
if segment.ident.name != keywords::PathRoot.name() {
if segment.ident.name != kw::PathRoot {
self.print_ident(segment.ident)?;
segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ use std::io;
use std::rc::Rc;
use syntax::ast::{self, NodeId};
use syntax::ptr::P;
use syntax::symbol::{keywords, sym};
use syntax::symbol::{kw, sym};
use syntax_pos::Span;

use crate::hir;
Expand Down Expand Up @@ -1552,7 +1552,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
let sp = ident.span;
let var = self.variable(hir_id, sp);
// Ignore unused self.
if ident.name != keywords::SelfLower.name() {
if ident.name != kw::SelfLower {
if !self.warn_about_unused(vec![sp], hir_id, entry_ln, var) {
if self.live_on_entry(entry_ln, var).is_none() {
self.report_dead_assign(hir_id, sp, var, true);
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::mem::replace;
use syntax::ast;
use syntax::attr;
use syntax::ptr::P;
use syntax::symbol::{keywords, sym};
use syntax::symbol::{kw, sym};
use syntax_pos::Span;

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

if name == keywords::UnderscoreLifetime.ident() {
if name.name == kw::UnderscoreLifetime {
continue;
}

Expand Down Expand Up @@ -2525,8 +2525,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
for (i, (lifetime_i, lifetime_i_name)) in lifetimes.iter().enumerate() {
if let hir::ParamName::Plain(_) = lifetime_i_name {
let name = lifetime_i_name.ident().name;
if name == keywords::UnderscoreLifetime.name()
|| name == keywords::StaticLifetime.name()
if name == kw::UnderscoreLifetime
|| name == kw::StaticLifetime
{
let mut err = struct_span_err!(
self.tcx.sess,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use syntax::ast;
use syntax::attr;
use syntax::source_map::MultiSpan;
use syntax::feature_gate;
use syntax::symbol::{Symbol, keywords, InternedString, sym};
use syntax::symbol::{Symbol, InternedString, kw, sym};
use syntax_pos::Span;

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

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

pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> Kind<'tcx> {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use std::ops::Range;
use syntax::ast::{self, Name, Ident, NodeId};
use syntax::attr;
use syntax::ext::hygiene::Mark;
use syntax::symbol::{keywords, sym, Symbol, LocalInternedString, InternedString};
use syntax::symbol::{kw, sym, Symbol, LocalInternedString, InternedString};
use syntax_pos::Span;

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

Expand Down
18 changes: 9 additions & 9 deletions src/librustc/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
use crate::ty::subst::{Kind, Subst, UnpackedKind};
use crate::mir::interpret::ConstValue;
use rustc_target::spec::abi::Abi;
use syntax::symbol::{keywords, InternedString};
use syntax::symbol::{kw, InternedString};

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

match *region {
ty::ReEarlyBound(ref data) => {
data.name.as_symbol() != keywords::Invalid.name() &&
data.name.as_symbol() != keywords::UnderscoreLifetime.name()
data.name.as_symbol() != kw::Invalid &&
data.name.as_symbol() != kw::UnderscoreLifetime
}

ty::ReLateBound(_, br) |
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
if let ty::BrNamed(_, name) = br {
if name.as_symbol() != keywords::Invalid.name() &&
name.as_symbol() != keywords::UnderscoreLifetime.name() {
if name.as_symbol() != kw::Invalid &&
name.as_symbol() != kw::UnderscoreLifetime {
return true;
}
}
Expand Down Expand Up @@ -1205,7 +1205,7 @@ impl<F: fmt::Write> FmtPrinter<'_, '_, '_, F> {
// `explain_region()` or `note_and_explain_region()`.
match *region {
ty::ReEarlyBound(ref data) => {
if data.name.as_symbol() != keywords::Invalid.name() {
if data.name.as_symbol() != kw::Invalid {
p!(write("{}", data.name));
return Ok(self);
}
Expand All @@ -1214,8 +1214,8 @@ impl<F: fmt::Write> FmtPrinter<'_, '_, '_, F> {
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
if let ty::BrNamed(_, name) = br {
if name.as_symbol() != keywords::Invalid.name() &&
name.as_symbol() != keywords::UnderscoreLifetime.name() {
if name.as_symbol() != kw::Invalid &&
name.as_symbol() != kw::UnderscoreLifetime {
p!(write("{}", name));
return Ok(self);
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::marker::PhantomData;
use std::ops::Range;
use rustc_target::spec::abi;
use syntax::ast::{self, Ident};
use syntax::symbol::{keywords, InternedString};
use syntax::symbol::{kw, InternedString};

use serialize;
use self::InferTy::*;
Expand Down Expand Up @@ -1121,7 +1121,7 @@ impl<'a, 'gcx, 'tcx> ParamTy {
}

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

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

Expand Down
Loading

0 comments on commit 15ccaf7

Please sign in to comment.