Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libsyntax: update helper to stringify TyU* and TyI* to take value presence into account. #13452

Merged
merged 1 commit into from
Apr 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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`)
}