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 a refactoring of the `LitInt` type was necessary. The types `LitInt`, `LitUint` and `LitIntUnsuffixed` where merged to one type `LitInt` which stores it's value as `u64`. An additional parameter was added which indicate the signedness of the type and the sign of the value.
This commit is contained in:
@@ -18,6 +18,7 @@ use parse::token::{InternedString, str_to_ident};
|
||||
use parse::token;
|
||||
|
||||
use std::fmt;
|
||||
use std::num::Zero;
|
||||
use std::fmt::Show;
|
||||
use std::option::Option;
|
||||
use std::rc::Rc;
|
||||
@@ -656,15 +657,46 @@ pub enum StrStyle {
|
||||
|
||||
pub type Lit = Spanned<Lit_>;
|
||||
|
||||
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
|
||||
pub enum Sign {
|
||||
Minus,
|
||||
Plus
|
||||
}
|
||||
|
||||
impl<T: PartialOrd+Zero> Sign {
|
||||
pub fn new(n: T) -> Sign {
|
||||
if n < Zero::zero() {
|
||||
Minus
|
||||
} else {
|
||||
Plus
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
|
||||
pub enum LitIntType {
|
||||
SignedIntLit(IntTy, Sign),
|
||||
UnsignedIntLit(UintTy),
|
||||
UnsuffixedIntLit(Sign)
|
||||
}
|
||||
|
||||
impl LitIntType {
|
||||
pub fn suffix_len(&self) -> uint {
|
||||
match *self {
|
||||
UnsuffixedIntLit(_) => 0,
|
||||
SignedIntLit(s, _) => s.suffix_len(),
|
||||
UnsignedIntLit(u) => u.suffix_len()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
|
||||
pub enum Lit_ {
|
||||
LitStr(InternedString, StrStyle),
|
||||
LitBinary(Rc<Vec<u8> >),
|
||||
LitByte(u8),
|
||||
LitChar(char),
|
||||
LitInt(i64, IntTy),
|
||||
LitUint(u64, UintTy),
|
||||
LitIntUnsuffixed(i64),
|
||||
LitInt(u64, LitIntType),
|
||||
LitFloat(InternedString, FloatTy),
|
||||
LitFloatUnsuffixed(InternedString),
|
||||
LitNil,
|
||||
|
||||
Reference in New Issue
Block a user