1
Fork 0

Rollup merge of #123655 - celinval:smir-fix-binop-ty, r=compiler-errors

Remove unimplemented!() from BinOp::ty() function

To reduce redundancy, we now internalize the BinOp instead of duplicating the `ty()` function body.
This commit is contained in:
Matthias Krüger 2024-04-09 06:02:24 +02:00 committed by GitHub
commit 9ea1063a12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 196 additions and 39 deletions

View file

@ -8,7 +8,7 @@ use std::cell::Cell;
use crate::abi::{FnAbi, Layout, LayoutShape};
use crate::mir::alloc::{AllocId, GlobalAlloc};
use crate::mir::mono::{Instance, InstanceDef, StaticDef};
use crate::mir::{Body, Place};
use crate::mir::{BinOp, Body, Place};
use crate::target::MachineInfo;
use crate::ty::{
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, ForeignDef,
@ -211,6 +211,9 @@ pub trait Context {
/// Get a debug string representation of a place.
fn place_pretty(&self, place: &Place) -> String;
/// Get the resulting type of binary operation.
fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty;
}
// A thread local variable that stores a pointer to the tables mapping between TyCtxt

View file

@ -1,3 +1,4 @@
use crate::compiler_interface::with;
use crate::mir::pretty::function_body;
use crate::ty::{
AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, RigidTy, Ty, TyKind,
@ -337,42 +338,7 @@ impl BinOp {
/// 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.
pub fn ty(&self, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
match self {
BinOp::Add
| BinOp::AddUnchecked
| BinOp::Sub
| BinOp::SubUnchecked
| BinOp::Mul
| BinOp::MulUnchecked
| BinOp::Div
| BinOp::Rem
| BinOp::BitXor
| BinOp::BitAnd
| BinOp::BitOr => {
assert_eq!(lhs_ty, rhs_ty);
assert!(lhs_ty.kind().is_primitive());
lhs_ty
}
BinOp::Shl | BinOp::ShlUnchecked | BinOp::Shr | BinOp::ShrUnchecked => {
assert!(lhs_ty.kind().is_primitive());
assert!(rhs_ty.kind().is_primitive());
lhs_ty
}
BinOp::Offset => {
assert!(lhs_ty.kind().is_raw_ptr());
assert!(rhs_ty.kind().is_integral());
lhs_ty
}
BinOp::Eq | BinOp::Lt | BinOp::Le | BinOp::Ne | BinOp::Ge | BinOp::Gt => {
assert_eq!(lhs_ty, rhs_ty);
let lhs_kind = lhs_ty.kind();
assert!(lhs_kind.is_primitive() || lhs_kind.is_raw_ptr() || lhs_kind.is_fn_ptr());
Ty::bool_ty()
}
BinOp::Cmp => {
unimplemented!("Should cmp::Ordering be a RigidTy?");
}
}
with(|ctx| ctx.binop_ty(*self, lhs_ty, rhs_ty))
}
}