Skip to content

Commit 888517d

Browse files
committed
libsyntax: update helper to stringify TyU* and TyI* to take into account having a value.
Fixes #13359.
1 parent ab0d847 commit 888517d

File tree

6 files changed

+59
-28
lines changed

6 files changed

+59
-28
lines changed

src/librustc/util/ppaux.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,8 @@ pub fn ty_to_str(cx: &ctxt, typ: t) -> ~str {
367367
ty_bot => ~"!",
368368
ty_bool => ~"bool",
369369
ty_char => ~"char",
370-
ty_int(ast::TyI) => ~"int",
371-
ty_int(t) => ast_util::int_ty_to_str(t),
372-
ty_uint(ast::TyU) => ~"uint",
373-
ty_uint(t) => ast_util::uint_ty_to_str(t),
370+
ty_int(t) => ast_util::int_ty_to_str(t, None),
371+
ty_uint(t) => ast_util::uint_ty_to_str(t, None),
374372
ty_float(t) => ast_util::float_ty_to_str(t),
375373
ty_box(typ) => ~"@" + ty_to_str(cx, typ),
376374
ty_uniq(typ) => ~"~" + ty_to_str(cx, typ),

src/libsyntax/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ pub enum IntTy {
724724

725725
impl fmt::Show for IntTy {
726726
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
727-
write!(f.buf, "{}", ast_util::int_ty_to_str(*self))
727+
write!(f.buf, "{}", ast_util::int_ty_to_str(*self, None))
728728
}
729729
}
730730

@@ -739,7 +739,7 @@ pub enum UintTy {
739739

740740
impl fmt::Show for UintTy {
741741
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
742-
write!(f.buf, "{}", ast_util::uint_ty_to_str(*self))
742+
write!(f.buf, "{}", ast_util::uint_ty_to_str(*self, None))
743743
}
744744
}
745745

src/libsyntax/ast_util.rs

Lines changed: 30 additions & 14 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,10 +2163,10 @@ impl<'a> State<'a> {
21632163
word(&mut self.s, res.into_owned())
21642164
}
21652165
ast::LitInt(i, t) => {
2166-
word(&mut self.s, format!("{}{}", i, ast_util::int_ty_to_str(t)))
2166+
word(&mut self.s, ast_util::int_ty_to_str(t, Some(i)))
21672167
}
21682168
ast::LitUint(u, t) => {
2169-
word(&mut self.s, format!("{}{}", u, ast_util::uint_ty_to_str(t)))
2169+
word(&mut self.s, ast_util::uint_ty_to_str(t, Some(u)))
21702170
}
21712171
ast::LitIntUnsuffixed(i) => {
21722172
word(&mut self.s, format!("{}", i))

src/test/compile-fail/issue13359.rs

Lines changed: 21 additions & 0 deletions
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)