Skip to content

Commit 9b0ebfa

Browse files
Move literal_to_string to fmt::Display
1 parent 0324a2b commit 9b0ebfa

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/librustc/hir/print.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syntax::parse::ParseSess;
55
use syntax::parse::lexer::comments;
66
use syntax::print::pp::{self, Breaks};
77
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
8-
use syntax::print::pprust::{self, PrintState};
8+
use syntax::print::pprust::PrintState;
99
use syntax::symbol::kw;
1010
use syntax::util::parser::{self, AssocOp, Fixity};
1111
use syntax_pos::{self, BytePos, FileName};
@@ -1226,7 +1226,7 @@ impl<'a> State<'a> {
12261226

12271227
fn print_literal(&mut self, lit: &hir::Lit) {
12281228
self.maybe_print_comment(lit.span.lo());
1229-
self.writer().word(pprust::literal_to_string(lit.node.to_lit_token()))
1229+
self.writer().word(lit.node.to_lit_token().to_string())
12301230
}
12311231

12321232
pub fn print_expr(&mut self, expr: &hir::Expr) {

src/libsyntax/parse/literal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<'a> Parser<'a> {
344344
// Pack possible quotes and prefixes from the original literal into
345345
// the error literal's symbol so they can be pretty-printed faithfully.
346346
let suffixless_lit = token::Lit::new(lit.kind, lit.symbol, None);
347-
let symbol = Symbol::intern(&pprust::literal_to_string(suffixless_lit));
347+
let symbol = Symbol::intern(&suffixless_lit.to_string());
348348
let lit = token::Lit::new(token::Err, symbol, lit.suffix);
349349
Lit::from_lit_token(lit, span).map_err(|_| unreachable!())
350350
}

src/libsyntax/parse/token.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ pub struct Lit {
8080
pub suffix: Option<Symbol>,
8181
}
8282

83+
impl fmt::Display for Lit {
84+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85+
let Lit { kind, symbol, suffix } = *self;
86+
match kind {
87+
Byte => write!(f, "b'{}'", symbol)?,
88+
Char => write!(f, "'{}'", symbol)?,
89+
Str => write!(f, "\"{}\"", symbol)?,
90+
StrRaw(n) => write!(f, "r{delim}\"{string}\"{delim}",
91+
delim="#".repeat(n as usize),
92+
string=symbol)?,
93+
ByteStr => write!(f, "b\"{}\"", symbol)?,
94+
ByteStrRaw(n) => write!(f, "br{delim}\"{string}\"{delim}",
95+
delim="#".repeat(n as usize),
96+
string=symbol)?,
97+
Integer |
98+
Float |
99+
Bool |
100+
Err => write!(f, "{}", symbol)?,
101+
}
102+
103+
if let Some(suffix) = suffix {
104+
write!(f, "{}", suffix)?;
105+
}
106+
107+
Ok(())
108+
}
109+
}
110+
83111
impl LitKind {
84112
/// An English article for the literal token kind.
85113
crate fn article(self) -> &'static str {

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,6 @@ pub fn attribute_to_string(attr: &ast::Attribute) -> String {
426426
to_string(|s| s.print_attribute(attr))
427427
}
428428

429-
pub fn lit_to_string(l: &ast::Lit) -> String {
430-
to_string(|s| s.print_literal(l))
431-
}
432-
433429
pub fn variant_to_string(var: &ast::Variant) -> String {
434430
to_string(|s| s.print_variant(var))
435431
}
@@ -597,7 +593,7 @@ pub trait PrintState<'a> {
597593

598594
fn print_literal(&mut self, lit: &ast::Lit) {
599595
self.maybe_print_comment(lit.span.lo());
600-
self.writer().word(literal_to_string(lit.token))
596+
self.writer().word(lit.token.to_string())
601597
}
602598

603599
fn print_string(&mut self, st: &str,

0 commit comments

Comments
 (0)