]> git.lizzy.rs Git - rust.git/commitdiff
Move literal_to_string to fmt::Display
authorMark Rousskov <mark.simulacrum@gmail.com>
Wed, 26 Jun 2019 11:23:27 +0000 (07:23 -0400)
committerMark Rousskov <mark.simulacrum@gmail.com>
Wed, 10 Jul 2019 11:11:29 +0000 (07:11 -0400)
src/librustc/hir/print.rs
src/libsyntax/parse/literal.rs
src/libsyntax/parse/token.rs
src/libsyntax/print/pprust.rs

index 6817107635a606cd33e0c8fc0d00a33f1a63c0ff..827bbea7bd2ab1bf2beb434cba156e8b38110fe4 100644 (file)
@@ -5,7 +5,7 @@
 use syntax::parse::lexer::comments;
 use syntax::print::pp::{self, Breaks};
 use syntax::print::pp::Breaks::{Consistent, Inconsistent};
-use syntax::print::pprust::{self, PrintState};
+use syntax::print::pprust::PrintState;
 use syntax::symbol::kw;
 use syntax::util::parser::{self, AssocOp, Fixity};
 use syntax_pos::{self, BytePos, FileName};
@@ -1226,7 +1226,7 @@ fn print_expr_addr_of(&mut self,
 
     fn print_literal(&mut self, lit: &hir::Lit) {
         self.maybe_print_comment(lit.span.lo());
-        self.writer().word(pprust::literal_to_string(lit.node.to_lit_token()))
+        self.writer().word(lit.node.to_lit_token().to_string())
     }
 
     pub fn print_expr(&mut self, expr: &hir::Expr) {
index ef55bf6b929336511e4dc5f3fb5d0317e500535f..683d164156540a31cbc486fbe77e49792a25aabd 100644 (file)
@@ -344,7 +344,7 @@ impl<'a> Parser<'a> {
                 // Pack possible quotes and prefixes from the original literal into
                 // the error literal's symbol so they can be pretty-printed faithfully.
                 let suffixless_lit = token::Lit::new(lit.kind, lit.symbol, None);
-                let symbol = Symbol::intern(&pprust::literal_to_string(suffixless_lit));
+                let symbol = Symbol::intern(&suffixless_lit.to_string());
                 let lit = token::Lit::new(token::Err, symbol, lit.suffix);
                 Lit::from_lit_token(lit, span).map_err(|_| unreachable!())
             }
index ebd0decacb59fff2032d61444af8d801f78a6cc4..fccb556b95ad8acf37fb36894323bf3459e955b7 100644 (file)
@@ -80,6 +80,34 @@ pub struct Lit {
     pub suffix: Option<Symbol>,
 }
 
+impl fmt::Display for Lit {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        let Lit { kind, symbol, suffix } = *self;
+        match kind {
+            Byte          => write!(f, "b'{}'", symbol)?,
+            Char          => write!(f, "'{}'", symbol)?,
+            Str           => write!(f, "\"{}\"", symbol)?,
+            StrRaw(n)     => write!(f, "r{delim}\"{string}\"{delim}",
+                                     delim="#".repeat(n as usize),
+                                     string=symbol)?,
+            ByteStr       => write!(f, "b\"{}\"", symbol)?,
+            ByteStrRaw(n) => write!(f, "br{delim}\"{string}\"{delim}",
+                                     delim="#".repeat(n as usize),
+                                     string=symbol)?,
+            Integer       |
+            Float         |
+            Bool          |
+            Err           => write!(f, "{}", symbol)?,
+        }
+
+        if let Some(suffix) = suffix {
+            write!(f, "{}", suffix)?;
+        }
+
+        Ok(())
+    }
+}
+
 impl LitKind {
     /// An English article for the literal token kind.
     crate fn article(self) -> &'static str {
index 7e099bc4d509510eb56a65e1c4a4d89e9bd67d73..a69dd94cfb69bd3cbf5abd7c5da6ca5dd8c45bd0 100644 (file)
@@ -426,10 +426,6 @@ pub fn attribute_to_string(attr: &ast::Attribute) -> String {
     to_string(|s| s.print_attribute(attr))
 }
 
-pub fn lit_to_string(l: &ast::Lit) -> String {
-    to_string(|s| s.print_literal(l))
-}
-
 pub fn variant_to_string(var: &ast::Variant) -> String {
     to_string(|s| s.print_variant(var))
 }
@@ -597,7 +593,7 @@ fn next_comment(&mut self) -> Option<comments::Comment> {
 
     fn print_literal(&mut self, lit: &ast::Lit) {
         self.maybe_print_comment(lit.span.lo());
-        self.writer().word(literal_to_string(lit.token))
+        self.writer().word(lit.token.to_string())
     }
 
     fn print_string(&mut self, st: &str,