Skip to content

[breaking batch] remove the sign from integer literals in the ast #30508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,15 +1339,13 @@ fn lit_to_const(lit: &ast::Lit, ty_hint: Option<Ty>) -> ConstVal {
}
ast::LitByte(n) => Uint(n as u64),
ast::LitChar(n) => Uint(n as u64),
ast::LitInt(n, ast::SignedIntLit(_, ast::Plus)) => Int(n as i64),
ast::LitInt(n, ast::UnsuffixedIntLit(ast::Plus)) => {
ast::LitInt(n, ast::SignedIntLit(_)) => Int(n as i64),
ast::LitInt(n, ast::UnsuffixedIntLit) => {
match ty_hint.map(|ty| &ty.sty) {
Some(&ty::TyUint(_)) => Uint(n),
_ => Int(n as i64)
}
}
ast::LitInt(n, ast::SignedIntLit(_, ast::Minus)) |
ast::LitInt(n, ast::UnsuffixedIntLit(ast::Minus)) => Int(-(n as i64)),
ast::LitInt(n, ast::UnsignedIntLit(_)) => Uint(n),
ast::LitFloat(ref n, _) |
ast::LitFloatUnsuffixed(ref n) => {
Expand Down
12 changes: 5 additions & 7 deletions src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl LateLintPass for TypeLimits {
ast::LitInt(_, ast::UnsignedIntLit(_)) => {
forbid_unsigned_negation(cx, e.span);
},
ast::LitInt(_, ast::UnsuffixedIntLit(_)) => {
ast::LitInt(_, ast::UnsuffixedIntLit) => {
if let ty::TyUint(_) = cx.tcx.node_id_to_type(e.id).sty {
forbid_unsigned_negation(cx, e.span);
}
Expand Down Expand Up @@ -159,8 +159,8 @@ impl LateLintPass for TypeLimits {
match cx.tcx.node_id_to_type(e.id).sty {
ty::TyInt(t) => {
match lit.node {
ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => {
ast::LitInt(v, ast::SignedIntLit(_)) |
ast::LitInt(v, ast::UnsuffixedIntLit) => {
let int_type = if let ast::TyIs = t {
cx.sess().target.int_type
} else {
Expand Down Expand Up @@ -311,10 +311,8 @@ impl LateLintPass for TypeLimits {
let (min, max) = int_ty_range(int_ty);
let lit_val: i64 = match lit.node {
hir::ExprLit(ref li) => match li.node {
ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => v as i64,
ast::LitInt(v, ast::SignedIntLit(_, ast::Minus)) |
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Minus)) => -(v as i64),
ast::LitInt(v, ast::SignedIntLit(_)) |
ast::LitInt(v, ast::UnsuffixedIntLit) => v as i64,
_ => return true
},
_ => panic!()
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ pub fn const_lit(cx: &CrateContext, e: &hir::Expr, lit: &ast::Lit)
match lit.node {
ast::LitByte(b) => C_integral(Type::uint_from_ty(cx, ast::TyU8), b as u64, false),
ast::LitChar(i) => C_integral(Type::char(cx), i as u64, false),
ast::LitInt(i, ast::SignedIntLit(t, _)) => {
ast::LitInt(i, ast::SignedIntLit(t)) => {
C_integral(Type::int_from_ty(cx, t), i, true)
}
ast::LitInt(u, ast::UnsignedIntLit(t)) => {
C_integral(Type::uint_from_ty(cx, t), u, false)
}
ast::LitInt(i, ast::UnsuffixedIntLit(_)) => {
ast::LitInt(i, ast::UnsuffixedIntLit) => {
let lit_int_ty = cx.tcx().node_id_to_type(e.id);
match lit_int_ty.sty {
ty::TyInt(t) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2613,9 +2613,9 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
}
ast::LitByte(_) => tcx.types.u8,
ast::LitChar(_) => tcx.types.char,
ast::LitInt(_, ast::SignedIntLit(t, _)) => tcx.mk_mach_int(t),
ast::LitInt(_, ast::SignedIntLit(t)) => tcx.mk_mach_int(t),
ast::LitInt(_, ast::UnsignedIntLit(t)) => tcx.mk_mach_uint(t),
ast::LitInt(_, ast::UnsuffixedIntLit(_)) => {
ast::LitInt(_, ast::UnsuffixedIntLit) => {
let opt_ty = expected.to_option(fcx).and_then(|ty| {
match ty.sty {
ty::TyInt(_) | ty::TyUint(_) => Some(ty),
Expand Down
30 changes: 2 additions & 28 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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::*;
Expand Down Expand Up @@ -1263,36 +1262,11 @@ pub enum StrStyle {
/// A literal
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)]
Expand Down
8 changes: 6 additions & 2 deletions src/libsyntax/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,12 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyUs)))
}
fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyIs,
ast::Sign::new(i))))
if i < 0 {
let lit = self.expr_lit(sp, ast::LitInt((-i) as u64, ast::SignedIntLit(ast::TyIs)));
self.expr_unary(sp, ast::UnNot, lit)
} else {
self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyIs)))
}
}
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU32)))
Expand Down
24 changes: 21 additions & 3 deletions src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::ExprLit(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::ExprUnary(ast::UnNeg, lit),
span: DUMMY_SP,
attrs: None,
}).to_tokens(cx)
}
}
);
Expand Down
28 changes: 10 additions & 18 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,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) {
Expand Down Expand Up @@ -624,11 +624,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::TyIs, ast::Plus),
"i8" => ast::SignedIntLit(ast::TyI8, ast::Plus),
"i16" => ast::SignedIntLit(ast::TyI16, ast::Plus),
"i32" => ast::SignedIntLit(ast::TyI32, ast::Plus),
"i64" => ast::SignedIntLit(ast::TyI64, ast::Plus),
"isize" => ast::SignedIntLit(ast::TyIs),
"i8" => ast::SignedIntLit(ast::TyI8),
"i16" => ast::SignedIntLit(ast::TyI16),
"i32" => ast::SignedIntLit(ast::TyI32),
"i64" => ast::SignedIntLit(ast::TyI64),
"usize" => ast::UnsignedIntLit(ast::TyUs),
"u8" => ast::UnsignedIntLit(ast::TyU8),
"u16" => ast::UnsignedIntLit(ast::TyU16),
Expand Down Expand Up @@ -657,9 +657,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
Expand All @@ -671,16 +671,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)
}
}

Expand Down
20 changes: 2 additions & 18 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -3185,12 +3177,4 @@ mod tests {
let varstr = variant_to_string(&var);
assert_eq!(varstr, "principal_skinner");
}

#[test]
fn test_signed_int_to_string() {
let pos_int = ast::LitInt(42, ast::SignedIntLit(ast::TyI32, ast::Plus));
let neg_int = ast::LitInt((!42 + 1) as u64, ast::SignedIntLit(ast::TyI32, ast::Minus));
assert_eq!(format!("-{}", lit_to_string(&codemap::dummy_spanned(pos_int))),
lit_to_string(&codemap::dummy_spanned(neg_int)));
}
}
8 changes: 2 additions & 6 deletions src/libsyntax_ext/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
accumulator.push(c);
}
ast::LitInt(i, ast::UnsignedIntLit(_)) |
ast::LitInt(i, ast::SignedIntLit(_, ast::Plus)) |
ast::LitInt(i, ast::UnsuffixedIntLit(ast::Plus)) => {
ast::LitInt(i, ast::SignedIntLit(_)) |
ast::LitInt(i, ast::UnsuffixedIntLit) => {
accumulator.push_str(&format!("{}", i));
}
ast::LitInt(i, ast::SignedIntLit(_, ast::Minus)) |
ast::LitInt(i, ast::UnsuffixedIntLit(ast::Minus)) => {
accumulator.push_str(&format!("-{}", i));
}
ast::LitBool(b) => {
accumulator.push_str(&format!("{}", b));
}
Expand Down