Skip to content

Commit d5dca26

Browse files
authored
Rollup merge of #100018 - nnethercote:clean-up-LitKind, r=petrochenkov
Clean up `LitKind` r? ``@petrochenkov``
2 parents bb77336 + 5d3cc17 commit d5dca26

File tree

19 files changed

+73
-66
lines changed

19 files changed

+73
-66
lines changed

compiler/rustc_ast/src/ast.rs

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

compiler/rustc_ast/src/attr/mod.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,7 @@ impl MetaItem {
184184
}
185185

186186
pub fn value_str(&self) -> Option<Symbol> {
187-
match self.kind {
188-
MetaItemKind::NameValue(ref v) => match v.kind {
189-
LitKind::Str(ref s, _) => Some(*s),
190-
_ => None,
191-
},
192-
_ => None,
193-
}
187+
self.kind.value_str()
194188
}
195189

196190
pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {

compiler/rustc_ast/src/util/literal.rs

+9-9
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
927927
lit.clone()
928928
} else {
929929
Lit {
930-
token: token::Lit::new(token::LitKind::Err, kw::Empty, None),
930+
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
931931
kind: LitKind::Err(kw::Empty),
932932
span: DUMMY_SP,
933933
}

compiler/rustc_ast_pretty/src/pprust/state.rs

+1-1
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/concat_bytes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_ast as ast;
22
use rustc_ast::{ptr::P, tokenstream::TokenStream};
3-
use rustc_data_structures::sync::Lrc;
43
use rustc_errors::Applicability;
54
use rustc_expand::base::{self, DummyResult};
65

@@ -185,5 +184,5 @@ pub fn expand_concat_bytes(
185184
return base::MacEager::expr(DummyResult::raw_expr(sp, true));
186185
}
187186
let sp = cx.with_def_site_ctxt(sp);
188-
base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::from(accumulator))))
187+
base::MacEager::expr(cx.expr_byte_str(sp, accumulator))
189188
}

compiler/rustc_builtin_macros/src/derive.rs

+3-3
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_builtin_macros/src/deriving/debug.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
5252

5353
// We want to make sure we have the ctxt set so that we can use unstable methods
5454
let span = cx.with_def_site_ctxt(span);
55-
let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
55+
let name = cx.expr_str(span, ident.name);
5656
let fmt = substr.nonselflike_args[0].clone();
5757

5858
// Struct and tuples are similar enough that we use the same code for both,
@@ -89,10 +89,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
8989
for i in 0..fields.len() {
9090
let field = &fields[i];
9191
if is_struct {
92-
let name = cx.expr_lit(
93-
field.span,
94-
ast::LitKind::Str(field.name.unwrap().name, ast::StrStyle::Cooked),
95-
);
92+
let name = cx.expr_str(field.span, field.name.unwrap().name);
9693
args.push(name);
9794
}
9895
// Use an extra indirection to make sure this works for unsized types.
@@ -108,10 +105,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
108105

109106
for field in fields {
110107
if is_struct {
111-
name_exprs.push(cx.expr_lit(
112-
field.span,
113-
ast::LitKind::Str(field.name.unwrap().name, ast::StrStyle::Cooked),
114-
));
108+
name_exprs.push(cx.expr_str(field.span, field.name.unwrap().name));
115109
}
116110

117111
// Use an extra indirection to make sure this works for unsized types.

compiler/rustc_builtin_macros/src/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ impl<'a, 'b> Context<'a, 'b> {
923923
}
924924

925925
// Build the format
926-
let fill = self.ecx.expr_lit(sp, ast::LitKind::Char(fill));
926+
let fill = self.ecx.expr_char(sp, fill);
927927
let align = |name| {
928928
let mut p = Context::rtpath(self.ecx, sym::Alignment);
929929
p.push(Ident::new(name, sp));

compiler/rustc_builtin_macros/src/source_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub fn expand_include_bytes(
216216
}
217217
};
218218
match cx.source_map().load_binary_file(&file) {
219-
Ok(bytes) => base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(bytes.into()))),
219+
Ok(bytes) => base::MacEager::expr(cx.expr_byte_str(sp, bytes)),
220220
Err(e) => {
221221
cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
222222
DummyResult::any(sp)

compiler/rustc_expand/src/build.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::base::ExtCtxt;
33
use rustc_ast::attr;
44
use rustc_ast::ptr::P;
55
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, PatKind, UnOp};
6+
use rustc_data_structures::sync::Lrc;
67
use rustc_span::source_map::Spanned;
78
use rustc_span::symbol::{kw, sym, Ident, Symbol};
89

@@ -330,23 +331,38 @@ impl<'a> ExtCtxt<'a> {
330331
self.expr_struct(span, self.path_ident(span, id), fields)
331332
}
332333

333-
pub fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
334+
fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
334335
let lit = ast::Lit::from_lit_kind(lit_kind, span);
335336
self.expr(span, ast::ExprKind::Lit(lit))
336337
}
338+
337339
pub fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
338340
self.expr_lit(
339341
span,
340342
ast::LitKind::Int(i as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)),
341343
)
342344
}
345+
343346
pub fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
344347
self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U32)))
345348
}
349+
346350
pub fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
347351
self.expr_lit(sp, ast::LitKind::Bool(value))
348352
}
349353

354+
pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
355+
self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
356+
}
357+
358+
pub fn expr_char(&self, sp: Span, ch: char) -> P<ast::Expr> {
359+
self.expr_lit(sp, ast::LitKind::Char(ch))
360+
}
361+
362+
pub fn expr_byte_str(&self, sp: Span, bytes: Vec<u8>) -> P<ast::Expr> {
363+
self.expr_lit(sp, ast::LitKind::ByteStr(Lrc::from(bytes)))
364+
}
365+
350366
/// `[expr1, expr2, ...]`
351367
pub fn expr_array(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
352368
self.expr(sp, ast::ExprKind::Array(exprs))
@@ -357,10 +373,6 @@ impl<'a> ExtCtxt<'a> {
357373
self.expr_addr_of(sp, self.expr_array(sp, exprs))
358374
}
359375

360-
pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
361-
self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
362-
}
363-
364376
pub fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
365377
self.expr(sp, ast::ExprKind::Cast(expr, ty))
366378
}

compiler/rustc_expand/src/mbe/metavar_expr.rs

+1-1
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

+12-6
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

+1-1
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

+2-2
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

+3-3
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

+4-4
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

+5-5
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() {

0 commit comments

Comments
 (0)