Skip to content

Commit 6951cbb

Browse files
author
Oliver 'ker' Schneider
committed
remove the sign from integer literals
1 parent 8760874 commit 6951cbb

File tree

10 files changed

+54
-82
lines changed

10 files changed

+54
-82
lines changed

src/librustc/middle/const_eval.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1339,15 +1339,13 @@ fn lit_to_const(lit: &ast::Lit, ty_hint: Option<Ty>) -> ConstVal {
13391339
}
13401340
ast::LitByte(n) => Uint(n as u64),
13411341
ast::LitChar(n) => Uint(n as u64),
1342-
ast::LitInt(n, ast::SignedIntLit(_, ast::Plus)) => Int(n as i64),
1343-
ast::LitInt(n, ast::UnsuffixedIntLit(ast::Plus)) => {
1342+
ast::LitInt(n, ast::SignedIntLit(_)) => Int(n as i64),
1343+
ast::LitInt(n, ast::UnsuffixedIntLit) => {
13441344
match ty_hint.map(|ty| &ty.sty) {
13451345
Some(&ty::TyUint(_)) => Uint(n),
13461346
_ => Int(n as i64)
13471347
}
13481348
}
1349-
ast::LitInt(n, ast::SignedIntLit(_, ast::Minus)) |
1350-
ast::LitInt(n, ast::UnsuffixedIntLit(ast::Minus)) => Int(-(n as i64)),
13511349
ast::LitInt(n, ast::UnsignedIntLit(_)) => Uint(n),
13521350
ast::LitFloat(ref n, _) |
13531351
ast::LitFloatUnsuffixed(ref n) => {

src/librustc_lint/types.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl LateLintPass for TypeLimits {
106106
ast::LitInt(_, ast::UnsignedIntLit(_)) => {
107107
forbid_unsigned_negation(cx, e.span);
108108
},
109-
ast::LitInt(_, ast::UnsuffixedIntLit(_)) => {
109+
ast::LitInt(_, ast::UnsuffixedIntLit) => {
110110
if let ty::TyUint(_) = cx.tcx.node_id_to_type(e.id).sty {
111111
forbid_unsigned_negation(cx, e.span);
112112
}
@@ -159,8 +159,8 @@ impl LateLintPass for TypeLimits {
159159
match cx.tcx.node_id_to_type(e.id).sty {
160160
ty::TyInt(t) => {
161161
match lit.node {
162-
ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
163-
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => {
162+
ast::LitInt(v, ast::SignedIntLit(_)) |
163+
ast::LitInt(v, ast::UnsuffixedIntLit) => {
164164
let int_type = if let ast::TyIs = t {
165165
cx.sess().target.int_type
166166
} else {
@@ -311,10 +311,8 @@ impl LateLintPass for TypeLimits {
311311
let (min, max) = int_ty_range(int_ty);
312312
let lit_val: i64 = match lit.node {
313313
hir::ExprLit(ref li) => match li.node {
314-
ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
315-
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => v as i64,
316-
ast::LitInt(v, ast::SignedIntLit(_, ast::Minus)) |
317-
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Minus)) => -(v as i64),
314+
ast::LitInt(v, ast::SignedIntLit(_)) |
315+
ast::LitInt(v, ast::UnsuffixedIntLit) => v as i64,
318316
_ => return true
319317
},
320318
_ => panic!()

src/librustc_trans/trans/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ pub fn const_lit(cx: &CrateContext, e: &hir::Expr, lit: &ast::Lit)
6363
match lit.node {
6464
ast::LitByte(b) => C_integral(Type::uint_from_ty(cx, ast::TyU8), b as u64, false),
6565
ast::LitChar(i) => C_integral(Type::char(cx), i as u64, false),
66-
ast::LitInt(i, ast::SignedIntLit(t, _)) => {
66+
ast::LitInt(i, ast::SignedIntLit(t)) => {
6767
C_integral(Type::int_from_ty(cx, t), i, true)
6868
}
6969
ast::LitInt(u, ast::UnsignedIntLit(t)) => {
7070
C_integral(Type::uint_from_ty(cx, t), u, false)
7171
}
72-
ast::LitInt(i, ast::UnsuffixedIntLit(_)) => {
72+
ast::LitInt(i, ast::UnsuffixedIntLit) => {
7373
let lit_int_ty = cx.tcx().node_id_to_type(e.id);
7474
match lit_int_ty.sty {
7575
ty::TyInt(t) => {

src/librustc_typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2613,9 +2613,9 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
26132613
}
26142614
ast::LitByte(_) => tcx.types.u8,
26152615
ast::LitChar(_) => tcx.types.char,
2616-
ast::LitInt(_, ast::SignedIntLit(t, _)) => tcx.mk_mach_int(t),
2616+
ast::LitInt(_, ast::SignedIntLit(t)) => tcx.mk_mach_int(t),
26172617
ast::LitInt(_, ast::UnsignedIntLit(t)) => tcx.mk_mach_uint(t),
2618-
ast::LitInt(_, ast::UnsuffixedIntLit(_)) => {
2618+
ast::LitInt(_, ast::UnsuffixedIntLit) => {
26192619
let opt_ty = expected.to_option(fcx).and_then(|ty| {
26202620
match ty.sty {
26212621
ty::TyInt(_) | ty::TyUint(_) => Some(ty),

src/libsyntax/ast.rs

+2-28
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub use self::Mutability::*;
3030
pub use self::Pat_::*;
3131
pub use self::PathListItem_::*;
3232
pub use self::PrimTy::*;
33-
pub use self::Sign::*;
3433
pub use self::Stmt_::*;
3534
pub use self::StrStyle::*;
3635
pub use self::StructFieldKind::*;
@@ -1263,36 +1262,11 @@ pub enum StrStyle {
12631262
/// A literal
12641263
pub type Lit = Spanned<Lit_>;
12651264

1266-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1267-
pub enum Sign {
1268-
Minus,
1269-
Plus
1270-
}
1271-
1272-
impl Sign {
1273-
pub fn new<T: IntSign>(n: T) -> Sign {
1274-
n.sign()
1275-
}
1276-
}
1277-
1278-
pub trait IntSign {
1279-
fn sign(&self) -> Sign;
1280-
}
1281-
macro_rules! doit {
1282-
($($t:ident)*) => ($(impl IntSign for $t {
1283-
#[allow(unused_comparisons)]
1284-
fn sign(&self) -> Sign {
1285-
if *self < 0 {Minus} else {Plus}
1286-
}
1287-
})*)
1288-
}
1289-
doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
1290-
12911265
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
12921266
pub enum LitIntType {
1293-
SignedIntLit(IntTy, Sign),
1267+
SignedIntLit(IntTy),
12941268
UnsignedIntLit(UintTy),
1295-
UnsuffixedIntLit(Sign)
1269+
UnsuffixedIntLit,
12961270
}
12971271

12981272
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]

src/libsyntax/ext/build.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,12 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
684684
self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyUs)))
685685
}
686686
fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
687-
self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyIs,
688-
ast::Sign::new(i))))
687+
if i < 0 {
688+
let lit = self.expr_lit(sp, ast::LitInt((-i) as u64, ast::SignedIntLit(ast::TyIs)));
689+
self.expr_unary(sp, ast::UnNot, lit)
690+
} else {
691+
self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyIs)))
692+
}
689693
}
690694
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
691695
self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU32)))

src/libsyntax/ext/quote.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,27 @@ pub mod rt {
263263
(signed, $t:ty, $tag:expr) => (
264264
impl ToTokens for $t {
265265
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
266-
let lit = ast::LitInt(*self as u64, ast::SignedIntLit($tag,
267-
ast::Sign::new(*self)));
268-
dummy_spanned(lit).to_tokens(cx)
266+
let val = if *self < 0 {
267+
-self
268+
} else {
269+
*self
270+
};
271+
let lit = ast::LitInt(val as u64, ast::SignedIntLit($tag));
272+
let lit = P(ast::Expr {
273+
id: ast::DUMMY_NODE_ID,
274+
node: ast::ExprLit(P(dummy_spanned(lit))),
275+
span: DUMMY_SP,
276+
attrs: None,
277+
});
278+
if *self >= 0 {
279+
return lit.to_tokens(cx);
280+
}
281+
P(ast::Expr {
282+
id: ast::DUMMY_NODE_ID,
283+
node: ast::ExprUnary(ast::UnNeg, lit),
284+
span: DUMMY_SP,
285+
attrs: None,
286+
}).to_tokens(cx)
269287
}
270288
}
271289
);

src/libsyntax/parse/mod.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ pub fn integer_lit(s: &str,
592592

593593
let mut base = 10;
594594
let orig = s;
595-
let mut ty = ast::UnsuffixedIntLit(ast::Plus);
595+
let mut ty = ast::UnsuffixedIntLit;
596596

597597
if char_at(s, 0) == '0' && s.len() > 1 {
598598
match char_at(s, 1) {
@@ -624,11 +624,11 @@ pub fn integer_lit(s: &str,
624624
if let Some(ref suf) = suffix {
625625
if suf.is_empty() { sd.span_bug(sp, "found empty literal suffix in Some")}
626626
ty = match &**suf {
627-
"isize" => ast::SignedIntLit(ast::TyIs, ast::Plus),
628-
"i8" => ast::SignedIntLit(ast::TyI8, ast::Plus),
629-
"i16" => ast::SignedIntLit(ast::TyI16, ast::Plus),
630-
"i32" => ast::SignedIntLit(ast::TyI32, ast::Plus),
631-
"i64" => ast::SignedIntLit(ast::TyI64, ast::Plus),
627+
"isize" => ast::SignedIntLit(ast::TyIs),
628+
"i8" => ast::SignedIntLit(ast::TyI8),
629+
"i16" => ast::SignedIntLit(ast::TyI16),
630+
"i32" => ast::SignedIntLit(ast::TyI32),
631+
"i64" => ast::SignedIntLit(ast::TyI64),
632632
"usize" => ast::UnsignedIntLit(ast::TyUs),
633633
"u8" => ast::UnsignedIntLit(ast::TyU8),
634634
"u16" => ast::UnsignedIntLit(ast::TyU16),
@@ -657,9 +657,9 @@ pub fn integer_lit(s: &str,
657657
debug!("integer_lit: the type is {:?}, base {:?}, the new string is {:?}, the original \
658658
string was {:?}, the original suffix was {:?}", ty, base, s, orig, suffix);
659659

660-
let res = match u64::from_str_radix(s, base).ok() {
661-
Some(r) => r,
662-
None => {
660+
match u64::from_str_radix(s, base) {
661+
Ok(r) => ast::LitInt(r, ty),
662+
Err(_) => {
663663
// small bases are lexed as if they were base 10, e.g, the string
664664
// might be `0b10201`. This will cause the conversion above to fail,
665665
// but these cases have errors in the lexer: we don't want to emit
@@ -671,16 +671,8 @@ pub fn integer_lit(s: &str,
671671
if !already_errored {
672672
sd.span_err(sp, "int literal is too large");
673673
}
674-
0
674+
ast::LitInt(0, ty)
675675
}
676-
};
677-
678-
// adjust the sign
679-
let sign = ast::Sign::new(res);
680-
match ty {
681-
ast::SignedIntLit(t, _) => ast::LitInt(res, ast::SignedIntLit(t, sign)),
682-
ast::UnsuffixedIntLit(_) => ast::LitInt(res, ast::UnsuffixedIntLit(sign)),
683-
us@ast::UnsignedIntLit(_) => ast::LitInt(res, us)
684676
}
685677
}
686678

src/libsyntax/print/pprust.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -645,24 +645,16 @@ pub trait PrintState<'a> {
645645
}
646646
ast::LitInt(i, t) => {
647647
match t {
648-
ast::SignedIntLit(st, ast::Plus) => {
648+
ast::SignedIntLit(st) => {
649649
word(self.writer(),
650650
&st.val_to_string(i as i64))
651651
}
652-
ast::SignedIntLit(st, ast::Minus) => {
653-
let istr = st.val_to_string(-(i as i64));
654-
word(self.writer(),
655-
&format!("-{}", istr))
656-
}
657652
ast::UnsignedIntLit(ut) => {
658653
word(self.writer(), &ut.val_to_string(i))
659654
}
660-
ast::UnsuffixedIntLit(ast::Plus) => {
655+
ast::UnsuffixedIntLit => {
661656
word(self.writer(), &format!("{}", i))
662657
}
663-
ast::UnsuffixedIntLit(ast::Minus) => {
664-
word(self.writer(), &format!("-{}", i))
665-
}
666658
}
667659
}
668660
ast::LitFloat(ref f, t) => {

src/libsyntax_ext/concat.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,10 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
3838
accumulator.push(c);
3939
}
4040
ast::LitInt(i, ast::UnsignedIntLit(_)) |
41-
ast::LitInt(i, ast::SignedIntLit(_, ast::Plus)) |
42-
ast::LitInt(i, ast::UnsuffixedIntLit(ast::Plus)) => {
41+
ast::LitInt(i, ast::SignedIntLit(_)) |
42+
ast::LitInt(i, ast::UnsuffixedIntLit) => {
4343
accumulator.push_str(&format!("{}", i));
4444
}
45-
ast::LitInt(i, ast::SignedIntLit(_, ast::Minus)) |
46-
ast::LitInt(i, ast::UnsuffixedIntLit(ast::Minus)) => {
47-
accumulator.push_str(&format!("-{}", i));
48-
}
4945
ast::LitBool(b) => {
5046
accumulator.push_str(&format!("{}", b));
5147
}

0 commit comments

Comments
 (0)