diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 3bb344548e12b..727e86c7b123a 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -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), diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 1eb034a573ac3..cc607c6dcd11f 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -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)) } } @@ -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)) } } diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 5f669d5d613e5..e0b84438353be 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -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) -> ~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() } } @@ -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) -> ~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() } } diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index baade21d942a6..101c748b1ec05 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -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()); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 3e2509f4f6eef..2de5c8ef1b56a 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -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)) diff --git a/src/test/compile-fail/issue13359.rs b/src/test/compile-fail/issue13359.rs new file mode 100644 index 0000000000000..07197bd3f3cae --- /dev/null +++ b/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 or the MIT license +// , 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`) +}