Skip to content

Commit 042c8ae

Browse files
committed
syntax: Fix printing INT64_MIN
Integers are always parsed as a u64 in libsyntax, but they're stored as i64. The parser and pretty printer both printed an i64 instead of u64, sometimes introducing an extra negative sign.
1 parent 1237530 commit 042c8ae

File tree

6 files changed

+39
-25
lines changed

6 files changed

+39
-25
lines changed

src/librustc/util/ppaux.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,26 +355,22 @@ pub fn ty_to_str(cx: &ctxt, typ: t) -> StrBuf {
355355
ty_bot => "!".to_strbuf(),
356356
ty_bool => "bool".to_strbuf(),
357357
ty_char => "char".to_strbuf(),
358-
ty_int(t) => ast_util::int_ty_to_str(t, None),
359-
ty_uint(t) => ast_util::uint_ty_to_str(t, None),
360-
ty_float(t) => ast_util::float_ty_to_str(t),
361-
ty_box(typ) => {
362-
("@".to_owned() + ty_to_str(cx, typ).as_slice()).to_strbuf()
363-
}
364-
ty_uniq(typ) => {
365-
("~".to_owned() + ty_to_str(cx, typ).as_slice()).to_strbuf()
366-
}
367-
ty_ptr(ref tm) => {
368-
("*".to_owned() + mt_to_str(cx, tm).as_slice()).to_strbuf()
369-
}
358+
ty_int(t) => ast_util::int_ty_to_str(t, None,
359+
ast_util::AutoSuffix).to_strbuf(),
360+
ty_uint(t) => ast_util::uint_ty_to_str(t, None,
361+
ast_util::AutoSuffix).to_strbuf(),
362+
ty_float(t) => ast_util::float_ty_to_str(t).to_strbuf(),
363+
ty_box(typ) => "@".to_strbuf() + ty_to_str(cx, typ),
364+
ty_uniq(typ) => "~".to_strbuf() + ty_to_str(cx, typ),
365+
ty_ptr(ref tm) => "*".to_strbuf() + mt_to_str(cx, tm),
370366
ty_rptr(r, ref tm) => {
371367
let mut buf = region_ptr_to_str(cx, r);
372368
buf.push_str(mt_to_str(cx, tm).as_slice());
373369
buf
374370
}
375371
ty_tup(ref elems) => {
376372
let strs: Vec<StrBuf> = elems.iter().map(|elem| ty_to_str(cx, *elem)).collect();
377-
("(".to_owned() + strs.connect(",") + ")").to_strbuf()
373+
("(".to_strbuf() + strs.connect(",") + ")").to_strbuf()
378374
}
379375
ty_closure(ref f) => {
380376
closure_to_str(cx, *f)

src/libsyntax/ast.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,8 @@ pub enum IntTy {
711711

712712
impl fmt::Show for IntTy {
713713
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
714-
write!(f.buf, "{}", ast_util::int_ty_to_str(*self, None))
714+
write!(f.buf, "{}",
715+
ast_util::int_ty_to_str(*self, None, ast_util::AutoSuffix))
715716
}
716717
}
717718

@@ -726,7 +727,8 @@ pub enum UintTy {
726727

727728
impl fmt::Show for UintTy {
728729
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
729-
write!(f.buf, "{}", ast_util::uint_ty_to_str(*self, None))
730+
write!(f.buf, "{}",
731+
ast_util::uint_ty_to_str(*self, None, ast_util::AutoSuffix))
730732
}
731733
}
732734

src/libsyntax/ast_util.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,19 @@ pub fn is_path(e: @Expr) -> bool {
132132
return match e.node { ExprPath(_) => true, _ => false };
133133
}
134134

135+
pub enum SuffixMode {
136+
ForceSuffix,
137+
AutoSuffix,
138+
}
139+
135140
// Get a string representation of a signed int type, with its value.
136141
// We want to avoid "45int" and "-3int" in favor of "45" and "-3"
137-
pub fn int_ty_to_str(t: IntTy, val: Option<i64>) -> StrBuf {
142+
pub fn int_ty_to_str(t: IntTy, val: Option<i64>, mode: SuffixMode) -> StrBuf {
138143
let s = match t {
139-
TyI if val.is_some() => "",
144+
TyI if val.is_some() => match mode {
145+
AutoSuffix => "",
146+
ForceSuffix => "i",
147+
},
140148
TyI => "int",
141149
TyI8 => "i8",
142150
TyI16 => "i16",
@@ -145,7 +153,7 @@ pub fn int_ty_to_str(t: IntTy, val: Option<i64>) -> StrBuf {
145153
};
146154

147155
match val {
148-
Some(n) => format!("{}{}", n, s).to_strbuf(),
156+
Some(n) => format!("{}{}", n as u64, s).to_strbuf(),
149157
None => s.to_strbuf()
150158
}
151159
}
@@ -161,9 +169,12 @@ pub fn int_ty_max(t: IntTy) -> u64 {
161169

162170
// Get a string representation of an unsigned int type, with its value.
163171
// We want to avoid "42uint" in favor of "42u"
164-
pub fn uint_ty_to_str(t: UintTy, val: Option<u64>) -> StrBuf {
172+
pub fn uint_ty_to_str(t: UintTy, val: Option<u64>, mode: SuffixMode) -> StrBuf {
165173
let s = match t {
166-
TyU if val.is_some() => "u",
174+
TyU if val.is_some() => match mode {
175+
AutoSuffix => "",
176+
ForceSuffix => "u",
177+
},
167178
TyU => "uint",
168179
TyU8 => "u8",
169180
TyU16 => "u16",

src/libsyntax/parse/token.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,11 @@ pub fn to_str(t: &Token) -> StrBuf {
203203
res.push_char('\'');
204204
res
205205
}
206-
LIT_INT(i, t) => ast_util::int_ty_to_str(t, Some(i)),
207-
LIT_UINT(u, t) => ast_util::uint_ty_to_str(t, Some(u)),
208-
LIT_INT_UNSUFFIXED(i) => { i.to_str().to_strbuf() }
206+
LIT_INT(i, t) => ast_util::int_ty_to_str(t, Some(i),
207+
ast_util::ForceSuffix),
208+
LIT_UINT(u, t) => ast_util::uint_ty_to_str(t, Some(u),
209+
ast_util::ForceSuffix),
210+
LIT_INT_UNSUFFIXED(i) => { (i as u64).to_str().to_strbuf() }
209211
LIT_FLOAT(s, t) => {
210212
let mut body = StrBuf::from_str(get_ident(s).get());
211213
if body.as_slice().ends_with(".") {

src/libsyntax/print/pprust.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,11 +2232,13 @@ impl<'a> State<'a> {
22322232
}
22332233
ast::LitInt(i, t) => {
22342234
word(&mut self.s,
2235-
ast_util::int_ty_to_str(t, Some(i)).as_slice())
2235+
ast_util::int_ty_to_str(t, Some(i),
2236+
ast_util::AutoSuffix).as_slice())
22362237
}
22372238
ast::LitUint(u, t) => {
22382239
word(&mut self.s,
2239-
ast_util::uint_ty_to_str(t, Some(u)).as_slice())
2240+
ast_util::uint_ty_to_str(t, Some(u),
2241+
ast_util::AutoSuffix).as_slice())
22402242
}
22412243
ast::LitIntUnsuffixed(i) => {
22422244
word(&mut self.s, format!("{}", i))

src/test/run-pass/big-literals.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ pub fn main() {
1616

1717
assert_eq!(-2147483648i32 - 1i32, 2147483647i32);
1818
assert_eq!(-9223372036854775808i64 - 1i64, 9223372036854775807i64);
19+
assert_eq!(-9223372036854775808 - 1, 9223372036854775807);
1920
}

0 commit comments

Comments
 (0)