1 // Helpers for handling cast expressions, used in both
4 use crate::ty::{self, Ty};
8 /// Types that are represented as ints.
9 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
18 // Valid types for the result of a non-coercion cast
19 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
20 pub enum CastTy<'tcx> {
21 /// Various types that are represented as ints and handled mostly
22 /// in the same way, merged for easier matching.
24 /// Floating-Point types
29 Ptr(ty::TypeAndMut<'tcx>),
31 RPtr(ty::TypeAndMut<'tcx>),
34 /// Cast Kind. See RFC 401 (or librustc_typeck/check/cast.rs)
35 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
50 impl<'tcx> CastTy<'tcx> {
51 /// Returns `Some` for integral/pointer casts.
52 /// casts like unsizing casts will return `None`
53 pub fn from_ty(t: Ty<'tcx>) -> Option<CastTy<'tcx>> {
55 ty::Bool => Some(CastTy::Int(IntTy::Bool)),
56 ty::Char => Some(CastTy::Int(IntTy::Char)),
57 ty::Int(_) => Some(CastTy::Int(IntTy::I)),
58 ty::Infer(ty::InferTy::IntVar(_)) => Some(CastTy::Int(IntTy::I)),
59 ty::Infer(ty::InferTy::FloatVar(_)) => Some(CastTy::Float),
60 ty::Uint(u) => Some(CastTy::Int(IntTy::U(u))),
61 ty::Float(_) => Some(CastTy::Float),
62 ty::Adt(d,_) if d.is_enum() && d.is_payloadfree() =>
63 Some(CastTy::Int(IntTy::CEnum)),
64 ty::RawPtr(mt) => Some(CastTy::Ptr(mt)),
65 ty::Ref(_, ty, mutbl) => Some(CastTy::RPtr(ty::TypeAndMut { ty, mutbl })),
66 ty::FnPtr(..) => Some(CastTy::FnPtr),