Skip to content

Commit 465109d

Browse files
committed
auto merge of #13452 : Ryman/rust/fix_uint_as_u, r=alexcrichton
Fixes #13359.
2 parents 7240fad + 888517d commit 465109d

File tree

6 files changed

+59
-28
lines changed

6 files changed

+59
-28
lines changed

src/librustc/util/ppaux.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,8 @@ pub fn ty_to_str(cx: &ctxt, typ: t) -> ~str {
341341
ty_bot => ~"!",
342342
ty_bool => ~"bool",
343343
ty_char => ~"char",
344-
ty_int(ast::TyI) => ~"int",
345-
ty_int(t) => ast_util::int_ty_to_str(t),
346-
ty_uint(ast::TyU) => ~"uint",
347-
ty_uint(t) => ast_util::uint_ty_to_str(t),
344+
ty_int(t) => ast_util::int_ty_to_str(t, None),
345+
ty_uint(t) => ast_util::uint_ty_to_str(t, None),
348346
ty_float(t) => ast_util::float_ty_to_str(t),
349347
ty_box(typ) => ~"@" + ty_to_str(cx, typ),
350348
ty_uniq(typ) => ~"~" + ty_to_str(cx, typ),

src/libsyntax/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ pub enum IntTy {
707707

708708
impl fmt::Show for IntTy {
709709
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
710-
write!(f.buf, "{}", ast_util::int_ty_to_str(*self))
710+
write!(f.buf, "{}", ast_util::int_ty_to_str(*self, None))
711711
}
712712
}
713713

@@ -722,7 +722,7 @@ pub enum UintTy {
722722

723723
impl fmt::Show for UintTy {
724724
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
725-
write!(f.buf, "{}", ast_util::uint_ty_to_str(*self))
725+
write!(f.buf, "{}", ast_util::uint_ty_to_str(*self, None))
726726
}
727727
}
728728

src/libsyntax/ast_util.rs

+30-14
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,21 @@ pub fn is_path(e: @Expr) -> bool {
132132
return match e.node { ExprPath(_) => true, _ => false };
133133
}
134134

135-
pub fn int_ty_to_str(t: IntTy) -> ~str {
136-
match t {
137-
TyI => ~"",
138-
TyI8 => ~"i8",
139-
TyI16 => ~"i16",
140-
TyI32 => ~"i32",
141-
TyI64 => ~"i64"
135+
// Get a string representation of a signed int type, with its value.
136+
// 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>) -> ~str {
138+
let s = match t {
139+
TyI if val.is_some() => "",
140+
TyI => "int",
141+
TyI8 => "i8",
142+
TyI16 => "i16",
143+
TyI32 => "i32",
144+
TyI64 => "i64"
145+
};
146+
147+
match val {
148+
Some(n) => format!("{}{}", n, s),
149+
None => s.to_owned()
142150
}
143151
}
144152

@@ -151,13 +159,21 @@ pub fn int_ty_max(t: IntTy) -> u64 {
151159
}
152160
}
153161

154-
pub fn uint_ty_to_str(t: UintTy) -> ~str {
155-
match t {
156-
TyU => ~"u",
157-
TyU8 => ~"u8",
158-
TyU16 => ~"u16",
159-
TyU32 => ~"u32",
160-
TyU64 => ~"u64"
162+
// Get a string representation of an unsigned int type, with its value.
163+
// We want to avoid "42uint" in favor of "42u"
164+
pub fn uint_ty_to_str(t: UintTy, val: Option<u64>) -> ~str {
165+
let s = match t {
166+
TyU if val.is_some() => "u",
167+
TyU => "uint",
168+
TyU8 => "u8",
169+
TyU16 => "u16",
170+
TyU32 => "u32",
171+
TyU64 => "u64"
172+
};
173+
174+
match val {
175+
Some(n) => format!("{}{}", n, s),
176+
None => s.to_owned()
161177
}
162178
}
163179

src/libsyntax/parse/token.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,8 @@ pub fn to_str(t: &Token) -> ~str {
201201
res.push_char('\'');
202202
res.into_owned()
203203
}
204-
LIT_INT(i, t) => {
205-
i.to_str() + ast_util::int_ty_to_str(t)
206-
}
207-
LIT_UINT(u, t) => {
208-
u.to_str() + ast_util::uint_ty_to_str(t)
209-
}
204+
LIT_INT(i, t) => ast_util::int_ty_to_str(t, Some(i)),
205+
LIT_UINT(u, t) => ast_util::uint_ty_to_str(t, Some(u)),
210206
LIT_INT_UNSUFFIXED(i) => { i.to_str() }
211207
LIT_FLOAT(s, t) => {
212208
let mut body = StrBuf::from_str(get_ident(s).get());

src/libsyntax/print/pprust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2171,10 +2171,10 @@ impl<'a> State<'a> {
21712171
word(&mut self.s, res.into_owned())
21722172
}
21732173
ast::LitInt(i, t) => {
2174-
word(&mut self.s, format!("{}{}", i, ast_util::int_ty_to_str(t)))
2174+
word(&mut self.s, ast_util::int_ty_to_str(t, Some(i)))
21752175
}
21762176
ast::LitUint(u, t) => {
2177-
word(&mut self.s, format!("{}{}", u, ast_util::uint_ty_to_str(t)))
2177+
word(&mut self.s, ast_util::uint_ty_to_str(t, Some(u)))
21782178
}
21792179
ast::LitIntUnsuffixed(i) => {
21802180
word(&mut self.s, format!("{}", i))

src/test/compile-fail/issue13359.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo(_s: i16) { }
12+
13+
fn bar(_s: u32) { }
14+
15+
fn main() {
16+
foo(1*(1 as int));
17+
//~^ ERROR: mismatched types: expected `i16` but found `int` (expected `i16` but found `int`)
18+
19+
bar(1*(1 as uint));
20+
//~^ ERROR: mismatched types: expected `u32` but found `uint` (expected `u32` but found `uint`)
21+
}

0 commit comments

Comments
 (0)