about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2015-11-24 09:43:46 -0500
committerSteve Klabnik <steve@steveklabnik.com>2015-11-24 09:43:46 -0500
commit731ff93a7654ffe4f093341bf1be230d47ce56b3 (patch)
tree7d9bad56599233cad2a95c72af71ecebfcff18c3 /src/libsyntax
parentff8859880ba0cda2ba814e7ba5c17f2ac454c26b (diff)
parent3be1d8ca7d9d9df60a38106d6c8f5d12597cbafc (diff)
downloadrust-731ff93a7654ffe4f093341bf1be230d47ce56b3.tar.gz
rust-731ff93a7654ffe4f093341bf1be230d47ce56b3.zip
Rollup merge of #30004 - michaelwoerister:primitive-ty-to-str, r=alexcrichton
Good candidate for a rollup, this one.
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ast.rs4
-rw-r--r--src/libsyntax/ast_util.rs42
-rw-r--r--src/libsyntax/print/pprust.rs6
3 files changed, 23 insertions, 29 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index e4697a7fd91..ab62c8d9421 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -1258,7 +1258,7 @@ impl fmt::Debug for IntTy {
 
 impl fmt::Display for IntTy {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "{}", ast_util::int_ty_to_string(*self, None))
+        write!(f, "{}", ast_util::int_ty_to_string(*self))
     }
 }
 
@@ -1303,7 +1303,7 @@ impl fmt::Debug for UintTy {
 
 impl fmt::Display for UintTy {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "{}", ast_util::uint_ty_to_string(*self, None))
+        write!(f, "{}", ast_util::uint_ty_to_string(*self))
     }
 }
 
diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs
index 44334762d90..489c61b83da 100644
--- a/src/libsyntax/ast_util.rs
+++ b/src/libsyntax/ast_util.rs
@@ -111,26 +111,23 @@ pub fn is_path(e: P<Expr>) -> bool {
     match e.node { ExprPath(..) => true, _ => false }
 }
 
-/// Get a string representation of a signed int type, with its value.
-/// We want to avoid "45int" and "-3int" in favor of "45" and "-3"
-pub fn int_ty_to_string(t: IntTy, val: Option<i64>) -> String {
-    let s = match t {
+pub fn int_ty_to_string(t: IntTy) -> &'static str {
+    match t {
         TyIs => "isize",
         TyI8 => "i8",
         TyI16 => "i16",
         TyI32 => "i32",
         TyI64 => "i64"
-    };
-
-    match val {
-        // cast to a u64 so we can correctly print INT64_MIN. All integral types
-        // are parsed as u64, so we wouldn't want to print an extra negative
-        // sign.
-        Some(n) => format!("{}{}", n as u64, s),
-        None => s.to_string()
     }
 }
 
+pub fn int_val_to_string(t: IntTy, val: i64) -> String {
+    // cast to a u64 so we can correctly print INT64_MIN. All integral types
+    // are parsed as u64, so we wouldn't want to print an extra negative
+    // sign.
+    format!("{}{}", val as u64, int_ty_to_string(t))
+}
+
 pub fn int_ty_max(t: IntTy) -> u64 {
     match t {
         TyI8 => 0x80,
@@ -140,23 +137,20 @@ pub fn int_ty_max(t: IntTy) -> u64 {
     }
 }
 
-/// Get a string representation of an unsigned int type, with its value.
-/// We want to avoid "42u" in favor of "42us". "42uint" is right out.
-pub fn uint_ty_to_string(t: UintTy, val: Option<u64>) -> String {
-    let s = match t {
+pub fn uint_ty_to_string(t: UintTy) -> &'static str {
+    match t {
         TyUs => "usize",
         TyU8 => "u8",
         TyU16 => "u16",
         TyU32 => "u32",
         TyU64 => "u64"
-    };
-
-    match val {
-        Some(n) => format!("{}{}", n, s),
-        None => s.to_string()
     }
 }
 
+pub fn uint_val_to_string(t: UintTy, val: u64) -> String {
+    format!("{}{}", val, uint_ty_to_string(t))
+}
+
 pub fn uint_ty_max(t: UintTy) -> u64 {
     match t {
         TyU8 => 0xff,
@@ -166,10 +160,10 @@ pub fn uint_ty_max(t: UintTy) -> u64 {
     }
 }
 
-pub fn float_ty_to_string(t: FloatTy) -> String {
+pub fn float_ty_to_string(t: FloatTy) -> &'static str {
     match t {
-        TyF32 => "f32".to_string(),
-        TyF64 => "f64".to_string(),
+        TyF32 => "f32",
+        TyF64 => "f64",
     }
 }
 
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index e9c716017c0..6de86de9c54 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -651,15 +651,15 @@ pub trait PrintState<'a> {
                 match t {
                     ast::SignedIntLit(st, ast::Plus) => {
                         word(self.writer(),
-                             &ast_util::int_ty_to_string(st, Some(i as i64)))
+                             &ast_util::int_val_to_string(st, i as i64))
                     }
                     ast::SignedIntLit(st, ast::Minus) => {
-                        let istr = ast_util::int_ty_to_string(st, Some(-(i as i64)));
+                        let istr = ast_util::int_val_to_string(st, -(i as i64));
                         word(self.writer(),
                              &format!("-{}", istr))
                     }
                     ast::UnsignedIntLit(ut) => {
-                        word(self.writer(), &ast_util::uint_ty_to_string(ut, Some(i)))
+                        word(self.writer(), &ast_util::uint_val_to_string(ut, i))
                     }
                     ast::UnsuffixedIntLit(ast::Plus) => {
                         word(self.writer(), &format!("{}", i))