Skip to content

Commit 7e0c6db

Browse files
committed
Remove LitKind::synthesize_token_lit.
It has a single call site in the HIR pretty printer, where the resulting token lit is immediately converted to a string. This commit replaces `LitKind::synthesize_token_lit` with a `Display` impl for `LitKind`, which can be used by the HIR pretty printer.
1 parent 568e647 commit 7e0c6db

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

compiler/rustc_ast/src/util/literal.rs

+44-39
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use crate::token::{self, Token};
55
use rustc_lexer::unescape::{byte_from_char, unescape_byte, unescape_char, unescape_literal, Mode};
66
use rustc_span::symbol::{kw, sym, Symbol};
77
use rustc_span::Span;
8-
use std::ascii;
9-
use std::str;
8+
use std::{ascii, fmt, str};
109

1110
// Escapes a string, represented as a symbol. Reuses the original symbol,
1211
// avoiding interning, if no changes are required.
@@ -162,54 +161,60 @@ impl LitKind {
162161
token::Err => LitKind::Err,
163162
})
164163
}
164+
}
165165

166-
/// Synthesizes a token from a semantic literal.
167-
/// This function is used when the original token doesn't exist (e.g. the literal is created
168-
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
169-
pub fn synthesize_token_lit(&self) -> token::Lit {
170-
let (kind, symbol, suffix) = match *self {
171-
LitKind::Str(symbol, ast::StrStyle::Cooked) => {
172-
(token::Str, escape_string_symbol(symbol), None)
166+
impl fmt::Display for LitKind {
167+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168+
match *self {
169+
LitKind::Byte(b) => {
170+
let b: String = ascii::escape_default(b).map(Into::<char>::into).collect();
171+
write!(f, "b'{}'", b)?;
173172
}
174-
LitKind::Str(symbol, ast::StrStyle::Raw(n)) => (token::StrRaw(n), symbol, None),
175-
LitKind::ByteStr(ref bytes, ast::StrStyle::Cooked) => {
176-
(token::ByteStr, escape_byte_str_symbol(bytes), None)
173+
LitKind::Char(ch) => write!(f, "'{}'", escape_char_symbol(ch))?,
174+
LitKind::Str(sym, StrStyle::Cooked) => write!(f, "\"{}\"", escape_string_symbol(sym))?,
175+
LitKind::Str(sym, StrStyle::Raw(n)) => write!(
176+
f,
177+
"r{delim}\"{string}\"{delim}",
178+
delim = "#".repeat(n as usize),
179+
string = sym
180+
)?,
181+
LitKind::ByteStr(ref bytes, StrStyle::Cooked) => {
182+
write!(f, "b\"{}\"", escape_byte_str_symbol(bytes))?
177183
}
178-
LitKind::ByteStr(ref bytes, ast::StrStyle::Raw(n)) => {
184+
LitKind::ByteStr(ref bytes, StrStyle::Raw(n)) => {
179185
// Unwrap because raw byte string literals can only contain ASCII.
180-
let string = str::from_utf8(bytes).unwrap();
181-
(token::ByteStrRaw(n), Symbol::intern(&string), None)
182-
}
183-
LitKind::Byte(byte) => {
184-
let string: String = ascii::escape_default(byte).map(Into::<char>::into).collect();
185-
(token::Byte, Symbol::intern(&string), None)
186+
let symbol = str::from_utf8(bytes).unwrap();
187+
write!(
188+
f,
189+
"br{delim}\"{string}\"{delim}",
190+
delim = "#".repeat(n as usize),
191+
string = symbol
192+
)?;
186193
}
187-
LitKind::Char(ch) => (token::Char, escape_char_symbol(ch), None),
188194
LitKind::Int(n, ty) => {
189-
let suffix = match ty {
190-
ast::LitIntType::Unsigned(ty) => Some(ty.name()),
191-
ast::LitIntType::Signed(ty) => Some(ty.name()),
192-
ast::LitIntType::Unsuffixed => None,
193-
};
194-
(token::Integer, sym::integer(n), suffix)
195+
write!(f, "{}", n)?;
196+
match ty {
197+
ast::LitIntType::Unsigned(ty) => write!(f, "{}", ty.name())?,
198+
ast::LitIntType::Signed(ty) => write!(f, "{}", ty.name())?,
199+
ast::LitIntType::Unsuffixed => {}
200+
}
195201
}
196202
LitKind::Float(symbol, ty) => {
197-
let suffix = match ty {
198-
ast::LitFloatType::Suffixed(ty) => Some(ty.name()),
199-
ast::LitFloatType::Unsuffixed => None,
200-
};
201-
(token::Float, symbol, suffix)
203+
write!(f, "{}", symbol)?;
204+
match ty {
205+
ast::LitFloatType::Suffixed(ty) => write!(f, "{}", ty.name())?,
206+
ast::LitFloatType::Unsuffixed => {}
207+
}
202208
}
203-
LitKind::Bool(value) => {
204-
let symbol = if value { kw::True } else { kw::False };
205-
(token::Bool, symbol, None)
209+
LitKind::Bool(b) => write!(f, "{}", if b { "true" } else { "false" })?,
210+
LitKind::Err => {
211+
// This only shows up in places like `-Zunpretty=hir` output, so we
212+
// don't bother to produce something useful.
213+
write!(f, "<bad-literal>")?;
206214
}
207-
// This only shows up in places like `-Zunpretty=hir` output, so we
208-
// don't bother to produce something useful.
209-
LitKind::Err => (token::Err, Symbol::intern("<bad-literal>"), None),
210-
};
215+
}
211216

212-
token::Lit::new(kind, symbol, suffix)
217+
Ok(())
213218
}
214219
}
215220

compiler/rustc_hir_pretty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ impl<'a> State<'a> {
12561256

12571257
fn print_literal(&mut self, lit: &hir::Lit) {
12581258
self.maybe_print_comment(lit.span.lo());
1259-
self.word(lit.node.synthesize_token_lit().to_string())
1259+
self.word(lit.node.to_string())
12601260
}
12611261

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

0 commit comments

Comments
 (0)