Skip to content

Commit

Permalink
libsyntax: update helper to stringify TyU* and TyI* to take into acco…
Browse files Browse the repository at this point in the history
…unt having a value.

Fixes #13359.
  • Loading branch information
Ryman committed Apr 13, 2014
1 parent ab0d847 commit 888517d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 28 deletions.
6 changes: 2 additions & 4 deletions src/librustc/util/ppaux.rs
Expand Up @@ -367,10 +367,8 @@ pub fn ty_to_str(cx: &ctxt, typ: t) -> ~str {
ty_bot => ~"!",
ty_bool => ~"bool",
ty_char => ~"char",
ty_int(ast::TyI) => ~"int",
ty_int(t) => ast_util::int_ty_to_str(t),
ty_uint(ast::TyU) => ~"uint",
ty_uint(t) => ast_util::uint_ty_to_str(t),
ty_int(t) => ast_util::int_ty_to_str(t, None),
ty_uint(t) => ast_util::uint_ty_to_str(t, None),
ty_float(t) => ast_util::float_ty_to_str(t),
ty_box(typ) => ~"@" + ty_to_str(cx, typ),
ty_uniq(typ) => ~"~" + ty_to_str(cx, typ),
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ast.rs
Expand Up @@ -724,7 +724,7 @@ pub enum IntTy {

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

Expand All @@ -739,7 +739,7 @@ pub enum UintTy {

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

Expand Down
44 changes: 30 additions & 14 deletions src/libsyntax/ast_util.rs
Expand Up @@ -132,13 +132,21 @@ pub fn is_path(e: @Expr) -> bool {
return match e.node { ExprPath(_) => true, _ => false };
}

pub fn int_ty_to_str(t: IntTy) -> ~str {
match t {
TyI => ~"",
TyI8 => ~"i8",
TyI16 => ~"i16",
TyI32 => ~"i32",
TyI64 => ~"i64"
// Get a string representation of a signed int type, with its value.
// We want to avoid "45int" and "-3int" in favor of "45" and "-3"
pub fn int_ty_to_str(t: IntTy, val: Option<i64>) -> ~str {
let s = match t {
TyI if val.is_some() => "",
TyI => "int",
TyI8 => "i8",
TyI16 => "i16",
TyI32 => "i32",
TyI64 => "i64"
};

match val {
Some(n) => format!("{}{}", n, s),
None => s.to_owned()
}
}

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

pub fn uint_ty_to_str(t: UintTy) -> ~str {
match t {
TyU => ~"u",
TyU8 => ~"u8",
TyU16 => ~"u16",
TyU32 => ~"u32",
TyU64 => ~"u64"
// Get a string representation of an unsigned int type, with its value.
// We want to avoid "42uint" in favor of "42u"
pub fn uint_ty_to_str(t: UintTy, val: Option<u64>) -> ~str {
let s = match t {
TyU if val.is_some() => "u",
TyU => "uint",
TyU8 => "u8",
TyU16 => "u16",
TyU32 => "u32",
TyU64 => "u64"
};

match val {
Some(n) => format!("{}{}", n, s),
None => s.to_owned()
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/libsyntax/parse/token.rs
Expand Up @@ -201,12 +201,8 @@ pub fn to_str(t: &Token) -> ~str {
res.push_char('\'');
res.into_owned()
}
LIT_INT(i, t) => {
i.to_str() + ast_util::int_ty_to_str(t)
}
LIT_UINT(u, t) => {
u.to_str() + ast_util::uint_ty_to_str(t)
}
LIT_INT(i, t) => ast_util::int_ty_to_str(t, Some(i)),
LIT_UINT(u, t) => ast_util::uint_ty_to_str(t, Some(u)),
LIT_INT_UNSUFFIXED(i) => { i.to_str() }
LIT_FLOAT(s, t) => {
let mut body = StrBuf::from_str(get_ident(s).get());
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/print/pprust.rs
Expand Up @@ -2163,10 +2163,10 @@ impl<'a> State<'a> {
word(&mut self.s, res.into_owned())
}
ast::LitInt(i, t) => {
word(&mut self.s, format!("{}{}", i, ast_util::int_ty_to_str(t)))
word(&mut self.s, ast_util::int_ty_to_str(t, Some(i)))
}
ast::LitUint(u, t) => {
word(&mut self.s, format!("{}{}", u, ast_util::uint_ty_to_str(t)))
word(&mut self.s, ast_util::uint_ty_to_str(t, Some(u)))
}
ast::LitIntUnsuffixed(i) => {
word(&mut self.s, format!("{}", i))
Expand Down
21 changes: 21 additions & 0 deletions src/test/compile-fail/issue13359.rs
@@ -0,0 +1,21 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn foo(_s: i16) { }

fn bar(_s: u32) { }

fn main() {
foo(1*(1 as int));
//~^ ERROR: mismatched types: expected `i16` but found `int` (expected `i16` but found `int`)

bar(1*(1 as uint));
//~^ ERROR: mismatched types: expected `u32` but found `uint` (expected `u32` but found `uint`)
}

9 comments on commit 888517d

@bors
Copy link
Contributor

@bors bors commented on 888517d Apr 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at Ryman@888517d

@bors
Copy link
Contributor

@bors bors commented on 888517d Apr 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Ryman/rust/fix_uint_as_u = 888517d into auto

@bors
Copy link
Contributor

@bors bors commented on 888517d Apr 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ryman/rust/fix_uint_as_u = 888517d merged ok, testing candidate = c56d5170

@bors
Copy link
Contributor

@bors bors commented on 888517d Apr 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at Ryman@888517d

@bors
Copy link
Contributor

@bors bors commented on 888517d Apr 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Ryman/rust/fix_uint_as_u = 888517d into auto

@bors
Copy link
Contributor

@bors bors commented on 888517d Apr 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ryman/rust/fix_uint_as_u = 888517d merged ok, testing candidate = 465109d

@bors
Copy link
Contributor

@bors bors commented on 888517d Apr 13, 2014

@bors
Copy link
Contributor

@bors bors commented on 888517d Apr 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 465109d

Please sign in to comment.