Skip to content

Commit 5d3cc17

Browse files
committed
Rename some things related to literals.
- Rename `ast::Lit::token` as `ast::Lit::token_lit`, because its type is `token::Lit`, which is not a token. (This has been confusing me for a long time.) reasonable because we have an `ast::token::Lit` inside an `ast::Lit`. - Rename `LitKind::{from,to}_lit_token` as `LitKind::{from,to}_token_lit`, to match the above change and `token::Lit`.
1 parent d7a041f commit 5d3cc17

File tree

13 files changed

+49
-41
lines changed

13 files changed

+49
-41
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ pub enum StrStyle {
16901690
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
16911691
pub struct Lit {
16921692
/// The original literal token as written in source code.
1693-
pub token: token::Lit,
1693+
pub token_lit: token::Lit,
16941694
/// The "semantic" representation of the literal lowered from the original tokens.
16951695
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
16961696
/// FIXME: Remove this and only create the semantic representation during lowering to HIR.
@@ -1718,7 +1718,7 @@ impl StrLit {
17181718
StrStyle::Raw(n) => token::StrRaw(n),
17191719
};
17201720
Lit {
1721-
token: token::Lit::new(token_kind, self.symbol, self.suffix),
1721+
token_lit: token::Lit::new(token_kind, self.symbol, self.suffix),
17221722
span: self.span,
17231723
kind: LitKind::Str(self.symbol_unescaped, self.style),
17241724
}

compiler/rustc_ast/src/util/literal.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub enum LitError {
2323

2424
impl LitKind {
2525
/// Converts literal token into a semantic literal.
26-
pub fn from_lit_token(lit: token::Lit) -> Result<LitKind, LitError> {
26+
pub fn from_token_lit(lit: token::Lit) -> Result<LitKind, LitError> {
2727
let token::Lit { kind, symbol, suffix } = lit;
2828
if suffix.is_some() && !kind.may_have_suffix() {
2929
return Err(LitError::InvalidSuffix);
@@ -153,7 +153,7 @@ impl LitKind {
153153
/// Attempts to recover a token from semantic literal.
154154
/// This function is used when the original token doesn't exist (e.g. the literal is created
155155
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
156-
pub fn to_lit_token(&self) -> token::Lit {
156+
pub fn to_token_lit(&self) -> token::Lit {
157157
let (kind, symbol, suffix) = match *self {
158158
LitKind::Str(symbol, ast::StrStyle::Cooked) => {
159159
// Don't re-intern unless the escaped string is different.
@@ -208,8 +208,8 @@ impl LitKind {
208208

209209
impl Lit {
210210
/// Converts literal token into an AST literal.
211-
pub fn from_lit_token(token: token::Lit, span: Span) -> Result<Lit, LitError> {
212-
Ok(Lit { token, kind: LitKind::from_lit_token(token)?, span })
211+
pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result<Lit, LitError> {
212+
Ok(Lit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span })
213213
}
214214

215215
/// Converts arbitrary token into an AST literal.
@@ -232,21 +232,21 @@ impl Lit {
232232
_ => return Err(LitError::NotLiteral),
233233
};
234234

235-
Lit::from_lit_token(lit, token.span)
235+
Lit::from_token_lit(lit, token.span)
236236
}
237237

238238
/// Attempts to recover an AST literal from semantic literal.
239239
/// This function is used when the original token doesn't exist (e.g. the literal is created
240240
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
241241
pub fn from_lit_kind(kind: LitKind, span: Span) -> Lit {
242-
Lit { token: kind.to_lit_token(), kind, span }
242+
Lit { token_lit: kind.to_token_lit(), kind, span }
243243
}
244244

245245
/// Losslessly convert an AST literal into a token.
246246
pub fn to_token(&self) -> Token {
247-
let kind = match self.token.kind {
248-
token::Bool => token::Ident(self.token.symbol, false),
249-
_ => token::Literal(self.token),
247+
let kind = match self.token_lit.kind {
248+
token::Bool => token::Ident(self.token_lit.symbol, false),
249+
_ => token::Literal(self.token_lit),
250250
};
251251
Token::new(kind, self.span)
252252
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
928928
lit.clone()
929929
} else {
930930
Lit {
931-
token: token::Lit::new(token::LitKind::Err, kw::Empty, None),
931+
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
932932
kind: LitKind::Err(kw::Empty),
933933
span: DUMMY_SP,
934934
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
372372

373373
fn print_literal(&mut self, lit: &ast::Lit) {
374374
self.maybe_print_comment(lit.span.lo());
375-
self.word(lit.token.to_string())
375+
self.word(lit.token_lit.to_string())
376376
}
377377

378378
fn print_string(&mut self, st: &str, style: ast::StrStyle) {

compiler/rustc_builtin_macros/src/derive.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ fn report_bad_target(sess: &Session, item: &Annotatable, span: Span) -> bool {
126126
}
127127

128128
fn report_unexpected_literal(sess: &Session, lit: &ast::Lit) {
129-
let help_msg = match lit.token.kind {
130-
token::Str if rustc_lexer::is_ident(lit.token.symbol.as_str()) => {
131-
format!("try using `#[derive({})]`", lit.token.symbol)
129+
let help_msg = match lit.token_lit.kind {
130+
token::Str if rustc_lexer::is_ident(lit.token_lit.symbol.as_str()) => {
131+
format!("try using `#[derive({})]`", lit.token_lit.symbol)
132132
}
133133
_ => "for example, write `#[derive(Debug)]` for `Debug`".to_string(),
134134
};

compiler/rustc_expand/src/mbe/metavar_expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn parse_depth<'sess>(
112112
"meta-variable expression depth must be a literal"
113113
));
114114
};
115-
if let Ok(lit_kind) = LitKind::from_lit_token(*lit)
115+
if let Ok(lit_kind) = LitKind::from_token_lit(*lit)
116116
&& let LitKind::Int(n_u128, LitIntType::Unsuffixed) = lit_kind
117117
&& let Ok(n_usize) = usize::try_from(n_u128)
118118
{

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,20 +486,26 @@ impl server::TokenStream for Rustc<'_, '_> {
486486
// We don't use `TokenStream::from_ast` as the tokenstream currently cannot
487487
// be recovered in the general case.
488488
match &expr.kind {
489-
ast::ExprKind::Lit(l) if l.token.kind == token::Bool => Ok(
490-
tokenstream::TokenStream::token_alone(token::Ident(l.token.symbol, false), l.span),
491-
),
489+
ast::ExprKind::Lit(l) if l.token_lit.kind == token::Bool => {
490+
Ok(tokenstream::TokenStream::token_alone(
491+
token::Ident(l.token_lit.symbol, false),
492+
l.span,
493+
))
494+
}
492495
ast::ExprKind::Lit(l) => {
493-
Ok(tokenstream::TokenStream::token_alone(token::Literal(l.token), l.span))
496+
Ok(tokenstream::TokenStream::token_alone(token::Literal(l.token_lit), l.span))
494497
}
495498
ast::ExprKind::Unary(ast::UnOp::Neg, e) => match &e.kind {
496-
ast::ExprKind::Lit(l) => match l.token {
499+
ast::ExprKind::Lit(l) => match l.token_lit {
497500
token::Lit { kind: token::Integer | token::Float, .. } => {
498501
Ok(Self::TokenStream::from_iter([
499502
// FIXME: The span of the `-` token is lost when
500503
// parsing, so we cannot faithfully recover it here.
501504
tokenstream::TokenTree::token_alone(token::BinOp(token::Minus), e.span),
502-
tokenstream::TokenTree::token_alone(token::Literal(l.token), l.span),
505+
tokenstream::TokenTree::token_alone(
506+
token::Literal(l.token_lit),
507+
l.span,
508+
),
503509
]))
504510
}
505511
_ => Err(()),

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ impl<'a> State<'a> {
12471247

12481248
fn print_literal(&mut self, lit: &hir::Lit) {
12491249
self.maybe_print_comment(lit.span.lo());
1250-
self.word(lit.node.to_lit_token().to_string())
1250+
self.word(lit.node.to_token_lit().to_string())
12511251
}
12521252

12531253
fn print_inline_asm(&mut self, asm: &hir::InlineAsm<'_>) {

compiler/rustc_lint/src/hidden_unicode_codepoints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ impl EarlyLintPass for HiddenUnicodeCodepoints {
120120
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
121121
// byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
122122
let (text, span, padding) = match &expr.kind {
123-
ast::ExprKind::Lit(ast::Lit { token, kind, span }) => {
124-
let text = token.symbol;
123+
ast::ExprKind::Lit(ast::Lit { token_lit, kind, span }) => {
124+
let text = token_lit.symbol;
125125
if !contains_text_flow_control_chars(text.as_str()) {
126126
return;
127127
}

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,8 +1750,8 @@ impl<'a> Parser<'a> {
17501750
Some(lit) => match lit.kind {
17511751
ast::LitKind::Str(symbol_unescaped, style) => Ok(ast::StrLit {
17521752
style,
1753-
symbol: lit.token.symbol,
1754-
suffix: lit.token.suffix,
1753+
symbol: lit.token_lit.symbol,
1754+
suffix: lit.token_lit.suffix,
17551755
span: lit.span,
17561756
symbol_unescaped,
17571757
}),
@@ -1828,7 +1828,7 @@ impl<'a> Parser<'a> {
18281828
let suffixless_lit = token::Lit::new(lit.kind, lit.symbol, None);
18291829
let symbol = Symbol::intern(&suffixless_lit.to_string());
18301830
let lit = token::Lit::new(token::Err, symbol, lit.suffix);
1831-
Some(Lit::from_lit_token(lit, span).unwrap_or_else(|_| unreachable!()))
1831+
Some(Lit::from_token_lit(lit, span).unwrap_or_else(|_| unreachable!()))
18321832
}
18331833
}
18341834
}

src/tools/clippy/clippy_lints/src/octal_escapes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ impl EarlyLintPass for OctalEscapes {
5757
}
5858

5959
if let ExprKind::Lit(lit) = &expr.kind {
60-
if matches!(lit.token.kind, LitKind::Str) {
61-
check_lit(cx, &lit.token, lit.span, true);
62-
} else if matches!(lit.token.kind, LitKind::ByteStr) {
63-
check_lit(cx, &lit.token, lit.span, false);
60+
if matches!(lit.token_lit.kind, LitKind::Str) {
61+
check_lit(cx, &lit.token_lit, lit.span, true);
62+
} else if matches!(lit.token_lit.kind, LitKind::ByteStr) {
63+
check_lit(cx, &lit.token_lit, lit.span, false);
6464
}
6565
}
6666
}

src/tools/clippy/clippy_lints/src/write.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,12 @@ impl Write {
589589
},
590590
};
591591

592-
let replacement: String = match lit.token.kind {
592+
let replacement: String = match lit.token_lit.kind {
593593
LitKind::StrRaw(_) | LitKind::ByteStrRaw(_) if matches!(fmtstr.style, StrStyle::Raw(_)) => {
594-
lit.token.symbol.as_str().replace('{', "{{").replace('}', "}}")
594+
lit.token_lit.symbol.as_str().replace('{', "{{").replace('}', "}}")
595595
},
596596
LitKind::Str | LitKind::ByteStr if matches!(fmtstr.style, StrStyle::Cooked) => {
597-
lit.token.symbol.as_str().replace('{', "{{").replace('}', "}}")
597+
lit.token_lit.symbol.as_str().replace('{', "{{").replace('}', "}}")
598598
},
599599
LitKind::StrRaw(_)
600600
| LitKind::Str
@@ -603,7 +603,7 @@ impl Write {
603603
| LitKind::Integer
604604
| LitKind::Float
605605
| LitKind::Err => continue,
606-
LitKind::Byte | LitKind::Char => match lit.token.symbol.as_str() {
606+
LitKind::Byte | LitKind::Char => match lit.token_lit.symbol.as_str() {
607607
"\"" if matches!(fmtstr.style, StrStyle::Cooked) => "\\\"",
608608
"\"" if matches!(fmtstr.style, StrStyle::Raw(0)) => continue,
609609
"\\\\" if matches!(fmtstr.style, StrStyle::Raw(_)) => "\\",
@@ -614,7 +614,7 @@ impl Write {
614614
x => x,
615615
}
616616
.into(),
617-
LitKind::Bool => lit.token.symbol.as_str().deref().into(),
617+
LitKind::Bool => lit.token_lit.symbol.as_str().deref().into(),
618618
};
619619

620620
if !fmt_spans.is_empty() {

src/tools/rustfmt/src/expr.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub(crate) fn format_expr(
7979
if let Some(expr_rw) = rewrite_literal(context, l, shape) {
8080
Some(expr_rw)
8181
} else {
82-
if let LitKind::StrRaw(_) = l.token.kind {
82+
if let LitKind::StrRaw(_) = l.token_lit.kind {
8383
Some(context.snippet(l.span).trim().into())
8484
} else {
8585
None
@@ -1226,7 +1226,7 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
12261226

12271227
fn rewrite_int_lit(context: &RewriteContext<'_>, lit: &ast::Lit, shape: Shape) -> Option<String> {
12281228
let span = lit.span;
1229-
let symbol = lit.token.symbol.as_str();
1229+
let symbol = lit.token_lit.symbol.as_str();
12301230

12311231
if let Some(symbol_stripped) = symbol.strip_prefix("0x") {
12321232
let hex_lit = match context.config.hex_literal_case() {
@@ -1239,7 +1239,9 @@ fn rewrite_int_lit(context: &RewriteContext<'_>, lit: &ast::Lit, shape: Shape) -
12391239
format!(
12401240
"0x{}{}",
12411241
hex_lit,
1242-
lit.token.suffix.map_or(String::new(), |s| s.to_string())
1242+
lit.token_lit
1243+
.suffix
1244+
.map_or(String::new(), |s| s.to_string())
12431245
),
12441246
context.config.max_width(),
12451247
shape,

0 commit comments

Comments
 (0)