Skip to content

Commit 4616b9f

Browse files
committed
Add sanity check to BinOp::ty()
1 parent 77d7e44 commit 4616b9f

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

compiler/stable_mir/src/mir/body.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ pub enum BinOp {
277277
impl BinOp {
278278
/// Return the type of this operation for the given input Ty.
279279
/// This function does not perform type checking, and it currently doesn't handle SIMD.
280-
pub fn ty(&self, lhs_ty: Ty, _rhs_ty: Ty) -> Ty {
280+
pub fn ty(&self, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
281+
assert!(lhs_ty.kind().is_primitive());
282+
assert!(rhs_ty.kind().is_primitive());
281283
match self {
282284
BinOp::Add
283285
| BinOp::AddUnchecked
@@ -289,13 +291,17 @@ impl BinOp {
289291
| BinOp::Rem
290292
| BinOp::BitXor
291293
| BinOp::BitAnd
292-
| BinOp::BitOr
293-
| BinOp::Shl
294-
| BinOp::ShlUnchecked
295-
| BinOp::Shr
296-
| BinOp::ShrUnchecked
297-
| BinOp::Offset => lhs_ty,
298-
BinOp::Eq | BinOp::Lt | BinOp::Le | BinOp::Ne | BinOp::Ge | BinOp::Gt => Ty::bool_ty(),
294+
| BinOp::BitOr => {
295+
assert_eq!(lhs_ty, rhs_ty);
296+
lhs_ty
297+
}
298+
BinOp::Shl | BinOp::ShlUnchecked | BinOp::Shr | BinOp::ShrUnchecked | BinOp::Offset => {
299+
lhs_ty
300+
}
301+
BinOp::Eq | BinOp::Lt | BinOp::Le | BinOp::Ne | BinOp::Ge | BinOp::Gt => {
302+
assert_eq!(lhs_ty, rhs_ty);
303+
Ty::bool_ty()
304+
}
299305
}
300306
}
301307
}

compiler/stable_mir/src/ty.rs

+13
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,19 @@ impl TyKind {
244244
matches!(self, TyKind::RigidTy(RigidTy::FnPtr(..)))
245245
}
246246

247+
pub fn is_primitive(&self) -> bool {
248+
matches!(
249+
self,
250+
TyKind::RigidTy(
251+
RigidTy::Bool
252+
| RigidTy::Char
253+
| RigidTy::Int(_)
254+
| RigidTy::Uint(_)
255+
| RigidTy::Float(_)
256+
)
257+
)
258+
}
259+
247260
pub fn trait_principal(&self) -> Option<Binder<ExistentialTraitRef>> {
248261
if let TyKind::RigidTy(RigidTy::Dynamic(predicates, _, _)) = self {
249262
if let Some(Binder { value: ExistentialPredicate::Trait(trait_ref), bound_vars }) =

0 commit comments

Comments
 (0)