Use the correct type when comparing nested constants.

This commit is contained in:
Jason Newcomb 2022-10-10 15:33:49 -04:00
parent cf72565a12
commit 0cc749296f
2 changed files with 42 additions and 6 deletions

View File

@ -136,17 +136,49 @@ impl Constant {
(&Self::F64(l), &Self::F64(r)) => l.partial_cmp(&r),
(&Self::F32(l), &Self::F32(r)) => l.partial_cmp(&r),
(&Self::Bool(ref l), &Self::Bool(ref r)) => Some(l.cmp(r)),
(&Self::Tuple(ref l), &Self::Tuple(ref r)) | (&Self::Vec(ref l), &Self::Vec(ref r)) => iter::zip(l, r)
.map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
.unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
(&Self::Tuple(ref l), &Self::Tuple(ref r)) if l.len() == r.len() => match *cmp_type.kind() {
ty::Tuple(tys) if tys.len() == l.len() => l
.iter()
.zip(r)
.zip(tys)
.map(|((li, ri), cmp_type)| Self::partial_cmp(tcx, cmp_type, li, ri))
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
.unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
_ => None,
},
(&Self::Vec(ref l), &Self::Vec(ref r)) => {
let cmp_type = match *cmp_type.kind() {
ty::Array(ty, _) | ty::Slice(ty) => ty,
_ => return None,
};
iter::zip(l, r)
.map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
.unwrap_or_else(|| Some(l.len().cmp(&r.len())))
},
(&Self::Repeat(ref lv, ref ls), &Self::Repeat(ref rv, ref rs)) => {
match Self::partial_cmp(tcx, cmp_type, lv, rv) {
match Self::partial_cmp(
tcx,
match *cmp_type.kind() {
ty::Array(ty, _) => ty,
_ => return None,
},
lv,
rv,
) {
Some(Equal) => Some(ls.cmp(rs)),
x => x,
}
},
(&Self::Ref(ref lb), &Self::Ref(ref rb)) => Self::partial_cmp(tcx, cmp_type, lb, rb),
(&Self::Ref(ref lb), &Self::Ref(ref rb)) => Self::partial_cmp(
tcx,
match *cmp_type.kind() {
ty::Ref(_, ty, _) => ty,
_ => return None,
},
lb,
rb,
),
// TODO: are there any useful inter-type orderings?
_ => None,
}

View File

@ -0,0 +1,4 @@
fn main() {
let x = &1;
let _ = &1 < x && x < &10;
}