Add sanity check to BinOp::ty()
This commit is contained in:
parent
77d7e44573
commit
4616b9fd1d
@ -277,7 +277,9 @@ pub enum BinOp {
|
|||||||
impl BinOp {
|
impl BinOp {
|
||||||
/// Return the type of this operation for the given input Ty.
|
/// Return the type of this operation for the given input Ty.
|
||||||
/// This function does not perform type checking, and it currently doesn't handle SIMD.
|
/// This function does not perform type checking, and it currently doesn't handle SIMD.
|
||||||
pub fn ty(&self, lhs_ty: Ty, _rhs_ty: Ty) -> Ty {
|
pub fn ty(&self, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
|
||||||
|
assert!(lhs_ty.kind().is_primitive());
|
||||||
|
assert!(rhs_ty.kind().is_primitive());
|
||||||
match self {
|
match self {
|
||||||
BinOp::Add
|
BinOp::Add
|
||||||
| BinOp::AddUnchecked
|
| BinOp::AddUnchecked
|
||||||
@ -289,13 +291,17 @@ impl BinOp {
|
|||||||
| BinOp::Rem
|
| BinOp::Rem
|
||||||
| BinOp::BitXor
|
| BinOp::BitXor
|
||||||
| BinOp::BitAnd
|
| BinOp::BitAnd
|
||||||
| BinOp::BitOr
|
| BinOp::BitOr => {
|
||||||
| BinOp::Shl
|
assert_eq!(lhs_ty, rhs_ty);
|
||||||
| BinOp::ShlUnchecked
|
lhs_ty
|
||||||
| BinOp::Shr
|
}
|
||||||
| BinOp::ShrUnchecked
|
BinOp::Shl | BinOp::ShlUnchecked | BinOp::Shr | BinOp::ShrUnchecked | BinOp::Offset => {
|
||||||
| BinOp::Offset => lhs_ty,
|
lhs_ty
|
||||||
BinOp::Eq | BinOp::Lt | BinOp::Le | BinOp::Ne | BinOp::Ge | BinOp::Gt => Ty::bool_ty(),
|
}
|
||||||
|
BinOp::Eq | BinOp::Lt | BinOp::Le | BinOp::Ne | BinOp::Ge | BinOp::Gt => {
|
||||||
|
assert_eq!(lhs_ty, rhs_ty);
|
||||||
|
Ty::bool_ty()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -244,6 +244,19 @@ impl TyKind {
|
|||||||
matches!(self, TyKind::RigidTy(RigidTy::FnPtr(..)))
|
matches!(self, TyKind::RigidTy(RigidTy::FnPtr(..)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_primitive(&self) -> bool {
|
||||||
|
matches!(
|
||||||
|
self,
|
||||||
|
TyKind::RigidTy(
|
||||||
|
RigidTy::Bool
|
||||||
|
| RigidTy::Char
|
||||||
|
| RigidTy::Int(_)
|
||||||
|
| RigidTy::Uint(_)
|
||||||
|
| RigidTy::Float(_)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn trait_principal(&self) -> Option<Binder<ExistentialTraitRef>> {
|
pub fn trait_principal(&self) -> Option<Binder<ExistentialTraitRef>> {
|
||||||
if let TyKind::RigidTy(RigidTy::Dynamic(predicates, _, _)) = self {
|
if let TyKind::RigidTy(RigidTy::Dynamic(predicates, _, _)) = self {
|
||||||
if let Some(Binder { value: ExistentialPredicate::Trait(trait_ref), bound_vars }) =
|
if let Some(Binder { value: ExistentialPredicate::Trait(trait_ref), bound_vars }) =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user