about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorKevin Butler <haqkrs@gmail.com>2014-04-11 01:21:01 +0100
committerKevin Butler <haqkrs@gmail.com>2014-04-13 02:39:19 +0100
commit888517df4d5c83423f9722b32c6fe1b113b58208 (patch)
tree272af603839a5c3ada415b5d43678b8d12c2aded /src/libsyntax
parentab0d8472777d2359492dfdee1d21230fbf144f70 (diff)
downloadrust-888517df4d5c83423f9722b32c6fe1b113b58208.tar.gz
rust-888517df4d5c83423f9722b32c6fe1b113b58208.zip
libsyntax: update helper to stringify TyU* and TyI* to take into account having a value.
Fixes #13359.
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ast.rs4
-rw-r--r--src/libsyntax/ast_util.rs44
-rw-r--r--src/libsyntax/parse/token.rs8
-rw-r--r--src/libsyntax/print/pprust.rs4
4 files changed, 36 insertions, 24 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index 1eb034a573a..cc607c6dcd1 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -724,7 +724,7 @@ pub enum IntTy {
 
 impl fmt::Show for IntTy {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", ast_util::int_ty_to_str(*self))
+        write!(f.buf, "{}", ast_util::int_ty_to_str(*self, None))
     }
 }
 
@@ -739,7 +739,7 @@ pub enum UintTy {
 
 impl fmt::Show for UintTy {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f.buf, "{}", ast_util::uint_ty_to_str(*self))
+        write!(f.buf, "{}", ast_util::uint_ty_to_str(*self, None))
     }
 }
 
diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs
index 5f669d5d613..e0b84438353 100644
--- a/src/libsyntax/ast_util.rs
+++ b/src/libsyntax/ast_util.rs
@@ -132,13 +132,21 @@ pub fn is_path(e: @Expr) -> bool {
     return match e.node { ExprPath(_) => true, _ => false };
 }
 
-pub fn int_ty_to_str(t: IntTy) -> ~str {
-    match t {
-        TyI => ~"",
-        TyI8 => ~"i8",
-        TyI16 => ~"i16",
-        TyI32 => ~"i32",
-        TyI64 => ~"i64"
+// 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_str(t: IntTy, val: Option<i64>) -> ~str {
+    let s = match t {
+        TyI if val.is_some() => "",
+        TyI => "int",
+        TyI8 => "i8",
+        TyI16 => "i16",
+        TyI32 => "i32",
+        TyI64 => "i64"
+    };
+
+    match val {
+        Some(n) => format!("{}{}", n, s),
+        None => s.to_owned()
     }
 }
 
@@ -151,13 +159,21 @@ pub fn int_ty_max(t: IntTy) -> u64 {
     }
 }
 
-pub fn uint_ty_to_str(t: UintTy) -> ~str {
-    match t {
-        TyU => ~"u",
-        TyU8 => ~"u8",
-        TyU16 => ~"u16",
-        TyU32 => ~"u32",
-        TyU64 => ~"u64"
+// Get a string representation of an unsigned int type, with its value.
+// We want to avoid "42uint" in favor of "42u"
+pub fn uint_ty_to_str(t: UintTy, val: Option<u64>) -> ~str {
+    let s = match t {
+        TyU if val.is_some() => "u",
+        TyU => "uint",
+        TyU8 => "u8",
+        TyU16 => "u16",
+        TyU32 => "u32",
+        TyU64 => "u64"
+    };
+
+    match val {
+        Some(n) => format!("{}{}", n, s),
+        None => s.to_owned()
     }
 }
 
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index baade21d942..101c748b1ec 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -201,12 +201,8 @@ pub fn to_str(t: &Token) -> ~str {
           res.push_char('\'');
           res.into_owned()
       }
-      LIT_INT(i, t) => {
-          i.to_str() + ast_util::int_ty_to_str(t)
-      }
-      LIT_UINT(u, t) => {
-          u.to_str() + ast_util::uint_ty_to_str(t)
-      }
+      LIT_INT(i, t) => ast_util::int_ty_to_str(t, Some(i)),
+      LIT_UINT(u, t) => ast_util::uint_ty_to_str(t, Some(u)),
       LIT_INT_UNSUFFIXED(i) => { i.to_str() }
       LIT_FLOAT(s, t) => {
         let mut body = StrBuf::from_str(get_ident(s).get());
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 3e2509f4f6e..2de5c8ef1b5 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -2163,10 +2163,10 @@ impl<'a> State<'a> {
                 word(&mut self.s, res.into_owned())
             }
             ast::LitInt(i, t) => {
-                word(&mut self.s, format!("{}{}", i, ast_util::int_ty_to_str(t)))
+                word(&mut self.s, ast_util::int_ty_to_str(t, Some(i)))
             }
             ast::LitUint(u, t) => {
-                word(&mut self.s, format!("{}{}", u, ast_util::uint_ty_to_str(t)))
+                word(&mut self.s, ast_util::uint_ty_to_str(t, Some(u)))
             }
             ast::LitIntUnsuffixed(i) => {
                 word(&mut self.s, format!("{}", i))