Skip to content

Commit 3be1d8c

Browse files
Avoid some code duplication around getting names of numeric types.
1 parent 4891c00 commit 3be1d8c

File tree

6 files changed

+44
-110
lines changed

6 files changed

+44
-110
lines changed

src/librustc/util/ppaux.rs

+4-46
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ use middle::ty::{self, TypeAndMut, Ty, HasTypeFlags};
2424
use middle::ty::fold::TypeFoldable;
2525

2626
use std::fmt;
27-
use syntax::abi;
28-
use syntax::ast;
27+
use syntax::{abi, ast_util};
2928
use syntax::parse::token;
3029
use syntax::ast::CRATE_NODE_ID;
3130
use rustc_front::hir;
@@ -774,55 +773,14 @@ impl<'tcx> fmt::Display for ty::TraitRef<'tcx> {
774773
}
775774
}
776775

777-
pub fn int_ty_to_string(t: ast::IntTy, val: Option<i64>) -> String {
778-
let s = match t {
779-
ast::TyIs => "isize",
780-
ast::TyI8 => "i8",
781-
ast::TyI16 => "i16",
782-
ast::TyI32 => "i32",
783-
ast::TyI64 => "i64"
784-
};
785-
786-
match val {
787-
// cast to a u64 so we can correctly print INT64_MIN. All integral types
788-
// are parsed as u64, so we wouldn't want to print an extra negative
789-
// sign.
790-
Some(n) => format!("{}{}", n as u64, s),
791-
None => s.to_string()
792-
}
793-
}
794-
795-
pub fn uint_ty_to_string(t: ast::UintTy, val: Option<u64>) -> String {
796-
let s = match t {
797-
ast::TyUs => "usize",
798-
ast::TyU8 => "u8",
799-
ast::TyU16 => "u16",
800-
ast::TyU32 => "u32",
801-
ast::TyU64 => "u64"
802-
};
803-
804-
match val {
805-
Some(n) => format!("{}{}", n, s),
806-
None => s.to_string()
807-
}
808-
}
809-
810-
811-
pub fn float_ty_to_string(t: ast::FloatTy) -> String {
812-
match t {
813-
ast::TyF32 => "f32".to_string(),
814-
ast::TyF64 => "f64".to_string(),
815-
}
816-
}
817-
818776
impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
819777
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
820778
match *self {
821779
TyBool => write!(f, "bool"),
822780
TyChar => write!(f, "char"),
823-
TyInt(t) => write!(f, "{}", int_ty_to_string(t, None)),
824-
TyUint(t) => write!(f, "{}", uint_ty_to_string(t, None)),
825-
TyFloat(t) => write!(f, "{}", float_ty_to_string(t)),
781+
TyInt(t) => write!(f, "{}", ast_util::int_ty_to_string(t)),
782+
TyUint(t) => write!(f, "{}", ast_util::uint_ty_to_string(t)),
783+
TyFloat(t) => write!(f, "{}", ast_util::float_ty_to_string(t)),
826784
TyBox(typ) => write!(f, "Box<{}>", typ),
827785
TyRawPtr(ref tm) => {
828786
write!(f, "*{} {}", match tm.mutbl {

src/librustc_trans/trans/debuginfo/metadata.rs

+10-19
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use std::rc::Rc;
4646
use syntax;
4747
use syntax::util::interner::Interner;
4848
use syntax::codemap::Span;
49-
use syntax::{ast, codemap};
49+
use syntax::{ast, ast_util, codemap};
5050
use syntax::parse::token;
5151

5252

@@ -932,26 +932,17 @@ fn basic_type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
932932

933933
let (name, encoding) = match t.sty {
934934
ty::TyTuple(ref elements) if elements.is_empty() =>
935-
("()".to_string(), DW_ATE_unsigned),
936-
ty::TyBool => ("bool".to_string(), DW_ATE_boolean),
937-
ty::TyChar => ("char".to_string(), DW_ATE_unsigned_char),
938-
ty::TyInt(int_ty) => match int_ty {
939-
ast::TyIs => ("isize".to_string(), DW_ATE_signed),
940-
ast::TyI8 => ("i8".to_string(), DW_ATE_signed),
941-
ast::TyI16 => ("i16".to_string(), DW_ATE_signed),
942-
ast::TyI32 => ("i32".to_string(), DW_ATE_signed),
943-
ast::TyI64 => ("i64".to_string(), DW_ATE_signed)
935+
("()", DW_ATE_unsigned),
936+
ty::TyBool => ("bool", DW_ATE_boolean),
937+
ty::TyChar => ("char", DW_ATE_unsigned_char),
938+
ty::TyInt(int_ty) => {
939+
(ast_util::int_ty_to_string(int_ty), DW_ATE_signed)
944940
},
945-
ty::TyUint(uint_ty) => match uint_ty {
946-
ast::TyUs => ("usize".to_string(), DW_ATE_unsigned),
947-
ast::TyU8 => ("u8".to_string(), DW_ATE_unsigned),
948-
ast::TyU16 => ("u16".to_string(), DW_ATE_unsigned),
949-
ast::TyU32 => ("u32".to_string(), DW_ATE_unsigned),
950-
ast::TyU64 => ("u64".to_string(), DW_ATE_unsigned)
941+
ty::TyUint(uint_ty) => {
942+
(ast_util::uint_ty_to_string(uint_ty), DW_ATE_unsigned)
951943
},
952-
ty::TyFloat(float_ty) => match float_ty {
953-
ast::TyF32 => ("f32".to_string(), DW_ATE_float),
954-
ast::TyF64 => ("f64".to_string(), DW_ATE_float),
944+
ty::TyFloat(float_ty) => {
945+
(ast_util::float_ty_to_string(float_ty), DW_ATE_float)
955946
},
956947
_ => cx.sess().bug("debuginfo::basic_type_metadata - t is invalid type")
957948
};

src/librustc_trans/trans/debuginfo/type_names.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use middle::subst::{self, Substs};
1919
use middle::ty::{self, Ty};
2020

2121
use rustc_front::hir;
22-
use syntax::ast;
22+
use syntax::ast_util;
2323

2424
// Compute the name of the type as it should be stored in debuginfo. Does not do
2525
// any caching, i.e. calling the function twice with the same type will also do
@@ -41,21 +41,12 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
4141
qualified: bool,
4242
output: &mut String) {
4343
match t.sty {
44-
ty::TyBool => output.push_str("bool"),
45-
ty::TyChar => output.push_str("char"),
46-
ty::TyStr => output.push_str("str"),
47-
ty::TyInt(ast::TyIs) => output.push_str("isize"),
48-
ty::TyInt(ast::TyI8) => output.push_str("i8"),
49-
ty::TyInt(ast::TyI16) => output.push_str("i16"),
50-
ty::TyInt(ast::TyI32) => output.push_str("i32"),
51-
ty::TyInt(ast::TyI64) => output.push_str("i64"),
52-
ty::TyUint(ast::TyUs) => output.push_str("usize"),
53-
ty::TyUint(ast::TyU8) => output.push_str("u8"),
54-
ty::TyUint(ast::TyU16) => output.push_str("u16"),
55-
ty::TyUint(ast::TyU32) => output.push_str("u32"),
56-
ty::TyUint(ast::TyU64) => output.push_str("u64"),
57-
ty::TyFloat(ast::TyF32) => output.push_str("f32"),
58-
ty::TyFloat(ast::TyF64) => output.push_str("f64"),
44+
ty::TyBool => output.push_str("bool"),
45+
ty::TyChar => output.push_str("char"),
46+
ty::TyStr => output.push_str("str"),
47+
ty::TyInt(int_ty) => output.push_str(ast_util::int_ty_to_string(int_ty)),
48+
ty::TyUint(uint_ty) => output.push_str(ast_util::uint_ty_to_string(uint_ty)),
49+
ty::TyFloat(float_ty) => output.push_str(ast_util::float_ty_to_string(float_ty)),
5950
ty::TyStruct(def, substs) |
6051
ty::TyEnum(def, substs) => {
6152
push_item_name(cx, def.did, qualified, output);

src/libsyntax/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ impl fmt::Debug for IntTy {
12581258

12591259
impl fmt::Display for IntTy {
12601260
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1261-
write!(f, "{}", ast_util::int_ty_to_string(*self, None))
1261+
write!(f, "{}", ast_util::int_ty_to_string(*self))
12621262
}
12631263
}
12641264

@@ -1303,7 +1303,7 @@ impl fmt::Debug for UintTy {
13031303

13041304
impl fmt::Display for UintTy {
13051305
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1306-
write!(f, "{}", ast_util::uint_ty_to_string(*self, None))
1306+
write!(f, "{}", ast_util::uint_ty_to_string(*self))
13071307
}
13081308
}
13091309

src/libsyntax/ast_util.rs

+18-24
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,23 @@ pub fn is_path(e: P<Expr>) -> bool {
111111
match e.node { ExprPath(..) => true, _ => false }
112112
}
113113

114-
/// Get a string representation of a signed int type, with its value.
115-
/// We want to avoid "45int" and "-3int" in favor of "45" and "-3"
116-
pub fn int_ty_to_string(t: IntTy, val: Option<i64>) -> String {
117-
let s = match t {
114+
pub fn int_ty_to_string(t: IntTy) -> &'static str {
115+
match t {
118116
TyIs => "isize",
119117
TyI8 => "i8",
120118
TyI16 => "i16",
121119
TyI32 => "i32",
122120
TyI64 => "i64"
123-
};
124-
125-
match val {
126-
// cast to a u64 so we can correctly print INT64_MIN. All integral types
127-
// are parsed as u64, so we wouldn't want to print an extra negative
128-
// sign.
129-
Some(n) => format!("{}{}", n as u64, s),
130-
None => s.to_string()
131121
}
132122
}
133123

124+
pub fn int_val_to_string(t: IntTy, val: i64) -> String {
125+
// cast to a u64 so we can correctly print INT64_MIN. All integral types
126+
// are parsed as u64, so we wouldn't want to print an extra negative
127+
// sign.
128+
format!("{}{}", val as u64, int_ty_to_string(t))
129+
}
130+
134131
pub fn int_ty_max(t: IntTy) -> u64 {
135132
match t {
136133
TyI8 => 0x80,
@@ -140,23 +137,20 @@ pub fn int_ty_max(t: IntTy) -> u64 {
140137
}
141138
}
142139

143-
/// Get a string representation of an unsigned int type, with its value.
144-
/// We want to avoid "42u" in favor of "42us". "42uint" is right out.
145-
pub fn uint_ty_to_string(t: UintTy, val: Option<u64>) -> String {
146-
let s = match t {
140+
pub fn uint_ty_to_string(t: UintTy) -> &'static str {
141+
match t {
147142
TyUs => "usize",
148143
TyU8 => "u8",
149144
TyU16 => "u16",
150145
TyU32 => "u32",
151146
TyU64 => "u64"
152-
};
153-
154-
match val {
155-
Some(n) => format!("{}{}", n, s),
156-
None => s.to_string()
157147
}
158148
}
159149

150+
pub fn uint_val_to_string(t: UintTy, val: u64) -> String {
151+
format!("{}{}", val, uint_ty_to_string(t))
152+
}
153+
160154
pub fn uint_ty_max(t: UintTy) -> u64 {
161155
match t {
162156
TyU8 => 0xff,
@@ -166,10 +160,10 @@ pub fn uint_ty_max(t: UintTy) -> u64 {
166160
}
167161
}
168162

169-
pub fn float_ty_to_string(t: FloatTy) -> String {
163+
pub fn float_ty_to_string(t: FloatTy) -> &'static str {
170164
match t {
171-
TyF32 => "f32".to_string(),
172-
TyF64 => "f64".to_string(),
165+
TyF32 => "f32",
166+
TyF64 => "f64",
173167
}
174168
}
175169

src/libsyntax/print/pprust.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -651,15 +651,15 @@ pub trait PrintState<'a> {
651651
match t {
652652
ast::SignedIntLit(st, ast::Plus) => {
653653
word(self.writer(),
654-
&ast_util::int_ty_to_string(st, Some(i as i64)))
654+
&ast_util::int_val_to_string(st, i as i64))
655655
}
656656
ast::SignedIntLit(st, ast::Minus) => {
657-
let istr = ast_util::int_ty_to_string(st, Some(-(i as i64)));
657+
let istr = ast_util::int_val_to_string(st, -(i as i64));
658658
word(self.writer(),
659659
&format!("-{}", istr))
660660
}
661661
ast::UnsignedIntLit(ut) => {
662-
word(self.writer(), &ast_util::uint_ty_to_string(ut, Some(i)))
662+
word(self.writer(), &ast_util::uint_val_to_string(ut, i))
663663
}
664664
ast::UnsuffixedIntLit(ast::Plus) => {
665665
word(self.writer(), &format!("{}", i))

0 commit comments

Comments
 (0)