Skip to content

Commit 71a8e82

Browse files
committed
rustc: middle: simplify primitive type declaration in ty.
1 parent 0f8f259 commit 71a8e82

File tree

1 file changed

+39
-82
lines changed

1 file changed

+39
-82
lines changed

src/librustc/middle/ty.rs

Lines changed: 39 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -939,40 +939,51 @@ pub enum BoundRegion {
939939
BrEnv
940940
}
941941

942-
mod primitives {
943-
use super::TyS;
944-
945-
use syntax::ast;
942+
#[inline]
943+
pub fn mk_prim_t<'tcx>(primitive: &'tcx TyS<'static>) -> Ty<'tcx> {
944+
// FIXME(#17596) Ty<'tcx> is incorrectly invariant w.r.t 'tcx.
945+
unsafe { &*(primitive as *const _ as *const TyS<'tcx>) }
946+
}
946947

947-
macro_rules! def_prim_ty(
948-
($name:ident, $sty:expr) => (
949-
pub static $name: TyS<'static> = TyS {
948+
// Do not change these from static to const, interning types requires
949+
// the primitives to have a significant address.
950+
macro_rules! def_prim_tys(
951+
($($name:ident -> $sty:expr;)*) => (
952+
$(#[inline] pub fn $name<'tcx>() -> Ty<'tcx> {
953+
static PRIM_TY: TyS<'static> = TyS {
950954
sty: $sty,
951-
flags: super::NO_TYPE_FLAGS,
955+
flags: NO_TYPE_FLAGS,
952956
};
953-
)
957+
mk_prim_t(&PRIM_TY)
958+
})*
954959
)
960+
)
961+
962+
def_prim_tys!{
963+
mk_nil -> ty_nil;
964+
mk_bool -> ty_bool;
965+
mk_char -> ty_char;
966+
mk_int -> ty_int(ast::TyI);
967+
mk_i8 -> ty_int(ast::TyI8);
968+
mk_i16 -> ty_int(ast::TyI16);
969+
mk_i32 -> ty_int(ast::TyI32);
970+
mk_i64 -> ty_int(ast::TyI64);
971+
mk_uint -> ty_uint(ast::TyU);
972+
mk_u8 -> ty_uint(ast::TyU8);
973+
mk_u16 -> ty_uint(ast::TyU16);
974+
mk_u32 -> ty_uint(ast::TyU32);
975+
mk_u64 -> ty_uint(ast::TyU64);
976+
mk_f32 -> ty_float(ast::TyF32);
977+
mk_f64 -> ty_float(ast::TyF64);
978+
}
955979

956-
def_prim_ty!(TY_NIL, super::ty_nil)
957-
def_prim_ty!(TY_BOOL, super::ty_bool)
958-
def_prim_ty!(TY_CHAR, super::ty_char)
959-
def_prim_ty!(TY_INT, super::ty_int(ast::TyI))
960-
def_prim_ty!(TY_I8, super::ty_int(ast::TyI8))
961-
def_prim_ty!(TY_I16, super::ty_int(ast::TyI16))
962-
def_prim_ty!(TY_I32, super::ty_int(ast::TyI32))
963-
def_prim_ty!(TY_I64, super::ty_int(ast::TyI64))
964-
def_prim_ty!(TY_UINT, super::ty_uint(ast::TyU))
965-
def_prim_ty!(TY_U8, super::ty_uint(ast::TyU8))
966-
def_prim_ty!(TY_U16, super::ty_uint(ast::TyU16))
967-
def_prim_ty!(TY_U32, super::ty_uint(ast::TyU32))
968-
def_prim_ty!(TY_U64, super::ty_uint(ast::TyU64))
969-
def_prim_ty!(TY_F32, super::ty_float(ast::TyF32))
970-
def_prim_ty!(TY_F64, super::ty_float(ast::TyF64))
971-
972-
pub static TY_ERR: TyS<'static> = TyS {
973-
sty: super::ty_err,
974-
flags: super::HAS_TY_ERR,
980+
#[inline]
981+
pub fn mk_err<'tcx>() -> Ty<'tcx> {
982+
static TY_ERR: TyS<'static> = TyS {
983+
sty: ty_err,
984+
flags: HAS_TY_ERR,
975985
};
986+
mk_prim_t(&TY_ERR)
976987
}
977988

978989
// NB: If you change this, you'll probably want to change the corresponding
@@ -1760,57 +1771,6 @@ pub fn mk_t<'tcx>(cx: &ctxt<'tcx>, st: sty<'tcx>) -> Ty<'tcx> {
17601771
ty
17611772
}
17621773

1763-
#[inline]
1764-
pub fn mk_prim_t<'tcx>(primitive: &'tcx TyS<'static>) -> Ty<'tcx> {
1765-
// FIXME(#17596) Ty<'tcx> is incorrectly invariant w.r.t 'tcx.
1766-
unsafe { &*(primitive as *const _ as *const TyS<'tcx>) }
1767-
}
1768-
1769-
#[inline]
1770-
pub fn mk_nil<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_NIL) }
1771-
1772-
#[inline]
1773-
pub fn mk_err<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_ERR) }
1774-
1775-
#[inline]
1776-
pub fn mk_bool<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_BOOL) }
1777-
1778-
#[inline]
1779-
pub fn mk_int<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_INT) }
1780-
1781-
#[inline]
1782-
pub fn mk_i8<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_I8) }
1783-
1784-
#[inline]
1785-
pub fn mk_i16<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_I16) }
1786-
1787-
#[inline]
1788-
pub fn mk_i32<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_I32) }
1789-
1790-
#[inline]
1791-
pub fn mk_i64<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_I64) }
1792-
1793-
#[inline]
1794-
pub fn mk_f32<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_F32) }
1795-
1796-
#[inline]
1797-
pub fn mk_f64<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_F64) }
1798-
1799-
#[inline]
1800-
pub fn mk_uint<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_UINT) }
1801-
1802-
#[inline]
1803-
pub fn mk_u8<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_U8) }
1804-
1805-
#[inline]
1806-
pub fn mk_u16<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_U16) }
1807-
1808-
#[inline]
1809-
pub fn mk_u32<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_U32) }
1810-
1811-
#[inline]
1812-
pub fn mk_u64<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_U64) }
1813-
18141774
pub fn mk_mach_int<'tcx>(tm: ast::IntTy) -> Ty<'tcx> {
18151775
match tm {
18161776
ast::TyI => mk_int(),
@@ -1838,9 +1798,6 @@ pub fn mk_mach_float<'tcx>(tm: ast::FloatTy) -> Ty<'tcx> {
18381798
}
18391799
}
18401800

1841-
#[inline]
1842-
pub fn mk_char<'tcx>() -> Ty<'tcx> { mk_prim_t(&primitives::TY_CHAR) }
1843-
18441801
pub fn mk_str<'tcx>(cx: &ctxt<'tcx>) -> Ty<'tcx> {
18451802
mk_t(cx, ty_str)
18461803
}

0 commit comments

Comments
 (0)