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