diff options
| author | bors <bors@rust-lang.org> | 2014-08-05 17:21:23 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-08-05 17:21:23 +0000 |
| commit | 6da38890f1f734799dc06460edf26c560db59a8e (patch) | |
| tree | 5dea8a018e59172d058fea6963c40453c2bcb5b2 /src/libsyntax/parse/mod.rs | |
| parent | ce83301f8c64f77c876ecfed65962b7dc407f093 (diff) | |
| parent | 0dc215741b34236c310e409c437600ba0165c97c (diff) | |
| download | rust-6da38890f1f734799dc06460edf26c560db59a8e.tar.gz rust-6da38890f1f734799dc06460edf26c560db59a8e.zip | |
auto merge of #15709 : hirschenberger/rust/issue-14269, r=cmr
Fixes missing overflow lint for i64 #14269 The `type_overflow` lint, doesn't catch the overflow for `i64` because the overflow happens earlier in the parse phase when the `u64` as biggest possible int gets casted to `i64` , without checking the for overflows. We can't lint in the parse phase, so we emit a compiler error, as we do for overflowing `u64` Perhaps a consistent behaviour would be to emit a parse error for *all* overflowing integer types. See #14269
Diffstat (limited to 'src/libsyntax/parse/mod.rs')
| -rw-r--r-- | src/libsyntax/parse/mod.rs | 57 |
1 files changed, 20 insertions, 37 deletions
diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index a3e169cd511..8f960e37de2 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -515,31 +515,13 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { debug!("parse_integer_lit: {}", s); if s.len() == 1 { - return ast::LitIntUnsuffixed((s.char_at(0)).to_digit(10).unwrap() as i64); + let n = (s.char_at(0)).to_digit(10).unwrap(); + return ast::LitInt(n as u64, ast::UnsuffixedIntLit(ast::Sign::new(n))); } let mut base = 10; let orig = s; - - #[deriving(Show)] - enum Result { - Nothing, - Signed(ast::IntTy), - Unsigned(ast::UintTy) - } - - impl Result { - fn suffix_len(&self) -> uint { - match *self { - Nothing => 0, - Signed(s) => s.suffix_len(), - Unsigned(u) => u.suffix_len() - } - } - } - - let mut ty = Nothing; - + let mut ty = ast::UnsuffixedIntLit(ast::Plus); if s.char_at(0) == '0' { match s.char_at(1) { @@ -556,13 +538,13 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { let last = s.len() - 1; match s.char_at(last) { - 'i' => ty = Signed(ast::TyI), - 'u' => ty = Unsigned(ast::TyU), + 'i' => ty = ast::SignedIntLit(ast::TyI, ast::Plus), + 'u' => ty = ast::UnsignedIntLit(ast::TyU), '8' => { if s.len() > 2 { match s.char_at(last - 1) { - 'i' => ty = Signed(ast::TyI8), - 'u' => ty = Unsigned(ast::TyU8), + 'i' => ty = ast::SignedIntLit(ast::TyI8, ast::Plus), + 'u' => ty = ast::UnsignedIntLit(ast::TyU8), _ => { } } } @@ -570,8 +552,8 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { '6' => { if s.len() > 3 && s.char_at(last - 1) == '1' { match s.char_at(last - 2) { - 'i' => ty = Signed(ast::TyI16), - 'u' => ty = Unsigned(ast::TyU16), + 'i' => ty = ast::SignedIntLit(ast::TyI16, ast::Plus), + 'u' => ty = ast::UnsignedIntLit(ast::TyU16), _ => { } } } @@ -579,8 +561,8 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { '2' => { if s.len() > 3 && s.char_at(last - 1) == '3' { match s.char_at(last - 2) { - 'i' => ty = Signed(ast::TyI32), - 'u' => ty = Unsigned(ast::TyU32), + 'i' => ty = ast::SignedIntLit(ast::TyI32, ast::Plus), + 'u' => ty = ast::UnsignedIntLit(ast::TyU32), _ => { } } } @@ -588,8 +570,8 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { '4' => { if s.len() > 3 && s.char_at(last - 1) == '6' { match s.char_at(last - 2) { - 'i' => ty = Signed(ast::TyI64), - 'u' => ty = Unsigned(ast::TyU64), + 'i' => ty = ast::SignedIntLit(ast::TyI64, ast::Plus), + 'u' => ty = ast::UnsignedIntLit(ast::TyU64), _ => { } } } @@ -597,21 +579,22 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ { _ => { } } - - s = s.slice_to(s.len() - ty.suffix_len()); - debug!("The suffix is {}, base {}, the new string is {}, the original \ string was {}", ty, base, s, orig); + s = s.slice_to(s.len() - ty.suffix_len()); + let res: u64 = match ::std::num::from_str_radix(s, base) { Some(r) => r, None => { sd.span_err(sp, "int literal is too large"); 0 } }; + // adjust the sign + let sign = ast::Sign::new(res); match ty { - Nothing => ast::LitIntUnsuffixed(res as i64), - Signed(t) => ast::LitInt(res as i64, t), - Unsigned(t) => ast::LitUint(res, t) + 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) } } |
