about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>2016-02-11 09:52:55 +0100
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>2016-02-11 12:34:48 +0100
commitbfa66bb389ce1c7ce4aff09d1842b3428015bd4d (patch)
treef6252627aac7945dec962bacb43ed50186c8ab9b /src/libsyntax
parent625e78b7001c6e20f29928a5da8c9d21e9aed6c5 (diff)
downloadrust-bfa66bb389ce1c7ce4aff09d1842b3428015bd4d.tar.gz
rust-bfa66bb389ce1c7ce4aff09d1842b3428015bd4d.zip
[breaking-change] remove the sign from integer literals in the ast
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ast.rs30
-rw-r--r--src/libsyntax/ext/build.rs9
-rw-r--r--src/libsyntax/ext/quote.rs24
-rw-r--r--src/libsyntax/parse/mod.rs28
-rw-r--r--src/libsyntax/print/pprust.rs12
5 files changed, 42 insertions, 61 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index dbc73f01bc2..464e9da3724 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -21,7 +21,6 @@ pub use self::Mutability::*;
 pub use self::Pat_::*;
 pub use self::PathListItem_::*;
 pub use self::PrimTy::*;
-pub use self::Sign::*;
 pub use self::Stmt_::*;
 pub use self::StrStyle::*;
 pub use self::StructFieldKind::*;
@@ -1270,35 +1269,10 @@ pub enum StrStyle {
 pub type Lit = Spanned<Lit_>;
 
 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
-pub enum Sign {
-    Minus,
-    Plus
-}
-
-impl Sign {
-    pub fn new<T: IntSign>(n: T) -> Sign {
-        n.sign()
-    }
-}
-
-pub trait IntSign {
-    fn sign(&self) -> Sign;
-}
-macro_rules! doit {
-    ($($t:ident)*) => ($(impl IntSign for $t {
-        #[allow(unused_comparisons)]
-        fn sign(&self) -> Sign {
-            if *self < 0 {Minus} else {Plus}
-        }
-    })*)
-}
-doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
-
-#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
 pub enum LitIntType {
-    SignedIntLit(IntTy, Sign),
+    SignedIntLit(IntTy),
     UnsignedIntLit(UintTy),
-    UnsuffixedIntLit(Sign)
+    UnsuffixedIntLit,
 }
 
 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs
index 1af29f2f93a..1c2d1cebf3d 100644
--- a/src/libsyntax/ext/build.rs
+++ b/src/libsyntax/ext/build.rs
@@ -683,8 +683,13 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
         self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::UintTy::Us)))
     }
     fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
-        self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::IntTy::Is,
-                                                                  ast::Sign::new(i))))
+        if i < 0 {
+            let i = (-i) as u64;
+            let lit = self.expr_lit(sp, ast::LitInt(i, ast::SignedIntLit(ast::IntTy::Is)));
+            self.expr_unary(sp, ast::UnOp::Neg, lit)
+        } else {
+            self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::IntTy::Is)))
+        }
     }
     fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
         self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::UintTy::U32)))
diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs
index a9f480daa0e..dfe3f8e3c54 100644
--- a/src/libsyntax/ext/quote.rs
+++ b/src/libsyntax/ext/quote.rs
@@ -263,9 +263,27 @@ pub mod rt {
         (signed, $t:ty, $tag:expr) => (
             impl ToTokens for $t {
                 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
-                    let lit = ast::LitInt(*self as u64, ast::SignedIntLit($tag,
-                                                                          ast::Sign::new(*self)));
-                    dummy_spanned(lit).to_tokens(cx)
+                    let val = if *self < 0 {
+                        -self
+                    } else {
+                        *self
+                    };
+                    let lit = ast::LitInt(val as u64, ast::SignedIntLit($tag));
+                    let lit = P(ast::Expr {
+                        id: ast::DUMMY_NODE_ID,
+                        node: ast::ExprKind::Lit(P(dummy_spanned(lit))),
+                        span: DUMMY_SP,
+                        attrs: None,
+                    });
+                    if *self >= 0 {
+                        return lit.to_tokens(cx);
+                    }
+                    P(ast::Expr {
+                        id: ast::DUMMY_NODE_ID,
+                        node: ast::ExprKind::Unary(ast::UnOp::Neg, lit),
+                        span: DUMMY_SP,
+                        attrs: None,
+                    }).to_tokens(cx)
                 }
             }
         );
diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs
index 2472da36d52..d800b6925c0 100644
--- a/src/libsyntax/parse/mod.rs
+++ b/src/libsyntax/parse/mod.rs
@@ -586,7 +586,7 @@ pub fn integer_lit(s: &str,
 
     let mut base = 10;
     let orig = s;
-    let mut ty = ast::UnsuffixedIntLit(ast::Plus);
+    let mut ty = ast::UnsuffixedIntLit;
 
     if char_at(s, 0) == '0' && s.len() > 1 {
         match char_at(s, 1) {
@@ -618,11 +618,11 @@ pub fn integer_lit(s: &str,
     if let Some(ref suf) = suffix {
         if suf.is_empty() { sd.span_bug(sp, "found empty literal suffix in Some")}
         ty = match &**suf {
-            "isize" => ast::SignedIntLit(ast::IntTy::Is, ast::Plus),
-            "i8"  => ast::SignedIntLit(ast::IntTy::I8, ast::Plus),
-            "i16" => ast::SignedIntLit(ast::IntTy::I16, ast::Plus),
-            "i32" => ast::SignedIntLit(ast::IntTy::I32, ast::Plus),
-            "i64" => ast::SignedIntLit(ast::IntTy::I64, ast::Plus),
+            "isize" => ast::SignedIntLit(ast::IntTy::Is),
+            "i8"  => ast::SignedIntLit(ast::IntTy::I8),
+            "i16" => ast::SignedIntLit(ast::IntTy::I16),
+            "i32" => ast::SignedIntLit(ast::IntTy::I32),
+            "i64" => ast::SignedIntLit(ast::IntTy::I64),
             "usize" => ast::UnsignedIntLit(ast::UintTy::Us),
             "u8"  => ast::UnsignedIntLit(ast::UintTy::U8),
             "u16" => ast::UnsignedIntLit(ast::UintTy::U16),
@@ -651,9 +651,9 @@ pub fn integer_lit(s: &str,
     debug!("integer_lit: the type is {:?}, base {:?}, the new string is {:?}, the original \
            string was {:?}, the original suffix was {:?}", ty, base, s, orig, suffix);
 
-    let res = match u64::from_str_radix(s, base).ok() {
-        Some(r) => r,
-        None => {
+    match u64::from_str_radix(s, base) {
+        Ok(r) => ast::LitInt(r, ty),
+        Err(_) => {
             // small bases are lexed as if they were base 10, e.g, the string
             // might be `0b10201`. This will cause the conversion above to fail,
             // but these cases have errors in the lexer: we don't want to emit
@@ -665,16 +665,8 @@ pub fn integer_lit(s: &str,
             if !already_errored {
                 sd.span_err(sp, "int literal is too large");
             }
-            0
+            ast::LitInt(0, ty)
         }
-    };
-
-    // adjust the sign
-    let sign = ast::Sign::new(res);
-    match ty {
-        ast::SignedIntLit(t, _) => ast::LitInt(res, ast::SignedIntLit(t, sign)),
-        ast::UnsuffixedIntLit(_) => ast::LitInt(res, ast::UnsuffixedIntLit(sign)),
-        us@ast::UnsignedIntLit(_) => ast::LitInt(res, us)
     }
 }
 
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 2054c0fc9bd..a02a7d47d32 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -645,24 +645,16 @@ pub trait PrintState<'a> {
             }
             ast::LitInt(i, t) => {
                 match t {
-                    ast::SignedIntLit(st, ast::Plus) => {
+                    ast::SignedIntLit(st) => {
                         word(self.writer(),
                              &st.val_to_string(i as i64))
                     }
-                    ast::SignedIntLit(st, ast::Minus) => {
-                        let istr = st.val_to_string(-(i as i64));
-                        word(self.writer(),
-                             &format!("-{}", istr))
-                    }
                     ast::UnsignedIntLit(ut) => {
                         word(self.writer(), &ut.val_to_string(i))
                     }
-                    ast::UnsuffixedIntLit(ast::Plus) => {
+                    ast::UnsuffixedIntLit => {
                         word(self.writer(), &format!("{}", i))
                     }
-                    ast::UnsuffixedIntLit(ast::Minus) => {
-                        word(self.writer(), &format!("-{}", i))
-                    }
                 }
             }
             ast::LitFloat(ref f, t) => {