-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MIR] SwitchInt Everywhere #39456
[MIR] SwitchInt Everywhere #39456
Changes from all commits
779c6b6
98d1db7
24c93ef
5d70a7f
aac82d9
a00a0ad
92c56f6
64182a5
c993986
4be1848
8e00d28
76d9a4e
362eb7e
a8b7b62
eb727a8
6c19104
1949bdf
49ccc10
7d1f36a
f3bd723
b663d9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ use ty::subst::{Subst, Substs}; | |
use ty::{self, AdtDef, Ty, TyCtxt}; | ||
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; | ||
use hir; | ||
use ty::util::IntTypeExt; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub enum LvalueTy<'tcx> { | ||
|
@@ -135,15 +136,15 @@ impl<'tcx> Lvalue<'tcx> { | |
impl<'tcx> Rvalue<'tcx> { | ||
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Ty<'tcx>> | ||
{ | ||
match self { | ||
&Rvalue::Use(ref operand) => Some(operand.ty(mir, tcx)), | ||
&Rvalue::Repeat(ref operand, ref count) => { | ||
match *self { | ||
Rvalue::Use(ref operand) => Some(operand.ty(mir, tcx)), | ||
Rvalue::Repeat(ref operand, ref count) => { | ||
let op_ty = operand.ty(mir, tcx); | ||
let count = count.value.as_u64(tcx.sess.target.uint_type); | ||
assert_eq!(count as usize as u64, count); | ||
Some(tcx.mk_array(op_ty, count as usize)) | ||
} | ||
&Rvalue::Ref(reg, bk, ref lv) => { | ||
Rvalue::Ref(reg, bk, ref lv) => { | ||
let lv_ty = lv.ty(mir, tcx).to_ty(tcx); | ||
Some(tcx.mk_ref(reg, | ||
ty::TypeAndMut { | ||
|
@@ -152,27 +153,37 @@ impl<'tcx> Rvalue<'tcx> { | |
} | ||
)) | ||
} | ||
&Rvalue::Len(..) => Some(tcx.types.usize), | ||
&Rvalue::Cast(.., ty) => Some(ty), | ||
&Rvalue::BinaryOp(op, ref lhs, ref rhs) => { | ||
Rvalue::Len(..) => Some(tcx.types.usize), | ||
Rvalue::Cast(.., ty) => Some(ty), | ||
Rvalue::BinaryOp(op, ref lhs, ref rhs) => { | ||
let lhs_ty = lhs.ty(mir, tcx); | ||
let rhs_ty = rhs.ty(mir, tcx); | ||
Some(op.ty(tcx, lhs_ty, rhs_ty)) | ||
} | ||
&Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => { | ||
Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => { | ||
let lhs_ty = lhs.ty(mir, tcx); | ||
let rhs_ty = rhs.ty(mir, tcx); | ||
let ty = op.ty(tcx, lhs_ty, rhs_ty); | ||
let ty = tcx.intern_tup(&[ty, tcx.types.bool], false); | ||
Some(ty) | ||
} | ||
&Rvalue::UnaryOp(_, ref operand) => { | ||
Rvalue::UnaryOp(_, ref operand) => { | ||
Some(operand.ty(mir, tcx)) | ||
} | ||
&Rvalue::Box(t) => { | ||
Rvalue::Discriminant(ref lval) => { | ||
let ty = lval.ty(mir, tcx).to_ty(tcx); | ||
if let ty::TyAdt(adt_def, _) = ty.sty { | ||
Some(adt_def.discr_ty.to_ty(tcx)) | ||
} else { | ||
// Undefined behaviour, bug for now; may want to return something for | ||
// the `discriminant` intrinsic later. | ||
bug!("Rvalue::Discriminant on Lvalue of type {:?}", ty); | ||
} | ||
} | ||
Rvalue::Box(t) => { | ||
Some(tcx.mk_box(t)) | ||
} | ||
&Rvalue::Aggregate(ref ak, ref ops) => { | ||
Rvalue::Aggregate(ref ak, ref ops) => { | ||
match *ak { | ||
AggregateKind::Array => { | ||
if let Some(operand) = ops.get(0) { | ||
|
@@ -196,7 +207,7 @@ impl<'tcx> Rvalue<'tcx> { | |
} | ||
} | ||
} | ||
&Rvalue::InlineAsm { .. } => None | ||
Rvalue::InlineAsm { .. } => None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, this is pre-existing and not a problem with this PR per se, but I think that every rvalue should have a type. The cases where we see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
IIRC it has been this way all the way back to when first MIR patches landed. I have no idea why is it this way at the moment and I do not see any reason why it should stay an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, seems fine (but independent from this PR) |
||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the reason that I didn't do it this way is so that one could easily get a
[BasicBlock]
of places you might branch to. We could also just have the type-checker or other sanity checkers assert this property dynamically, which would help.