Evaluate float consts eagerly
This commit is contained in:
parent
56ada88e7e
commit
fac50e8fb3
3 changed files with 136 additions and 54 deletions
|
@ -50,6 +50,7 @@ use std::ops::RangeInclusive;
|
||||||
|
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
|
|
||||||
|
use rustc_apfloat::ieee::{DoubleS, IeeeFloat, SingleS};
|
||||||
use rustc_data_structures::captures::Captures;
|
use rustc_data_structures::captures::Captures;
|
||||||
use rustc_hir::{HirId, RangeEnd};
|
use rustc_hir::{HirId, RangeEnd};
|
||||||
use rustc_index::Idx;
|
use rustc_index::Idx;
|
||||||
|
@ -65,7 +66,6 @@ use rustc_target::abi::{FieldIdx, Integer, Size, VariantIdx, FIRST_VARIANT};
|
||||||
use self::Constructor::*;
|
use self::Constructor::*;
|
||||||
use self::SliceKind::*;
|
use self::SliceKind::*;
|
||||||
|
|
||||||
use super::compare_const_vals;
|
|
||||||
use super::usefulness::{MatchCheckCtxt, PatCtxt};
|
use super::usefulness::{MatchCheckCtxt, PatCtxt};
|
||||||
use crate::errors::{Overlap, OverlappingRangeEndpoints};
|
use crate::errors::{Overlap, OverlappingRangeEndpoints};
|
||||||
|
|
||||||
|
@ -619,7 +619,8 @@ pub(super) enum Constructor<'tcx> {
|
||||||
/// Ranges of integer literal values (`2`, `2..=5` or `2..5`).
|
/// Ranges of integer literal values (`2`, `2..=5` or `2..5`).
|
||||||
IntRange(IntRange),
|
IntRange(IntRange),
|
||||||
/// Ranges of floating-point literal values (`2.0..=5.2`).
|
/// Ranges of floating-point literal values (`2.0..=5.2`).
|
||||||
FloatRange(mir::Const<'tcx>, mir::Const<'tcx>, RangeEnd),
|
F32Range(IeeeFloat<SingleS>, IeeeFloat<SingleS>, RangeEnd),
|
||||||
|
F64Range(IeeeFloat<DoubleS>, IeeeFloat<DoubleS>, RangeEnd),
|
||||||
/// String literals. Strings are not quite the same as `&[u8]` so we treat them separately.
|
/// String literals. Strings are not quite the same as `&[u8]` so we treat them separately.
|
||||||
Str(mir::Const<'tcx>),
|
Str(mir::Const<'tcx>),
|
||||||
/// Array and slice patterns.
|
/// Array and slice patterns.
|
||||||
|
@ -634,7 +635,9 @@ pub(super) enum Constructor<'tcx> {
|
||||||
/// Stands for constructors that are not seen in the matrix, as explained in the documentation
|
/// Stands for constructors that are not seen in the matrix, as explained in the documentation
|
||||||
/// for [`SplitWildcard`]. The carried `bool` is used for the `non_exhaustive_omitted_patterns`
|
/// for [`SplitWildcard`]. The carried `bool` is used for the `non_exhaustive_omitted_patterns`
|
||||||
/// lint.
|
/// lint.
|
||||||
Missing { nonexhaustive_enum_missing_real_variants: bool },
|
Missing {
|
||||||
|
nonexhaustive_enum_missing_real_variants: bool,
|
||||||
|
},
|
||||||
/// Wildcard pattern.
|
/// Wildcard pattern.
|
||||||
Wildcard,
|
Wildcard,
|
||||||
/// Or-pattern.
|
/// Or-pattern.
|
||||||
|
@ -722,7 +725,8 @@ impl<'tcx> Constructor<'tcx> {
|
||||||
},
|
},
|
||||||
Slice(slice) => slice.arity(),
|
Slice(slice) => slice.arity(),
|
||||||
Str(..)
|
Str(..)
|
||||||
| FloatRange(..)
|
| F32Range(..)
|
||||||
|
| F64Range(..)
|
||||||
| IntRange(..)
|
| IntRange(..)
|
||||||
| NonExhaustive
|
| NonExhaustive
|
||||||
| Opaque
|
| Opaque
|
||||||
|
@ -795,21 +799,21 @@ impl<'tcx> Constructor<'tcx> {
|
||||||
(Variant(self_id), Variant(other_id)) => self_id == other_id,
|
(Variant(self_id), Variant(other_id)) => self_id == other_id,
|
||||||
|
|
||||||
(IntRange(self_range), IntRange(other_range)) => self_range.is_covered_by(other_range),
|
(IntRange(self_range), IntRange(other_range)) => self_range.is_covered_by(other_range),
|
||||||
(
|
(F32Range(self_from, self_to, self_end), F32Range(other_from, other_to, other_end)) => {
|
||||||
FloatRange(self_from, self_to, self_end),
|
self_from.ge(other_from)
|
||||||
FloatRange(other_from, other_to, other_end),
|
&& match self_to.partial_cmp(other_to) {
|
||||||
) => {
|
Some(Ordering::Less) => true,
|
||||||
match (
|
Some(Ordering::Equal) => other_end == self_end,
|
||||||
compare_const_vals(pcx.cx.tcx, *self_to, *other_to, pcx.cx.param_env),
|
_ => false,
|
||||||
compare_const_vals(pcx.cx.tcx, *self_from, *other_from, pcx.cx.param_env),
|
}
|
||||||
) {
|
}
|
||||||
(Some(to), Some(from)) => {
|
(F64Range(self_from, self_to, self_end), F64Range(other_from, other_to, other_end)) => {
|
||||||
(from == Ordering::Greater || from == Ordering::Equal)
|
self_from.ge(other_from)
|
||||||
&& (to == Ordering::Less
|
&& match self_to.partial_cmp(other_to) {
|
||||||
|| (other_end == self_end && to == Ordering::Equal))
|
Some(Ordering::Less) => true,
|
||||||
|
Some(Ordering::Equal) => other_end == self_end,
|
||||||
|
_ => false,
|
||||||
}
|
}
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
(Str(self_val), Str(other_val)) => {
|
(Str(self_val), Str(other_val)) => {
|
||||||
// FIXME Once valtrees are available we can directly use the bytes
|
// FIXME Once valtrees are available we can directly use the bytes
|
||||||
|
@ -859,7 +863,7 @@ impl<'tcx> Constructor<'tcx> {
|
||||||
.any(|other| slice.is_covered_by(other)),
|
.any(|other| slice.is_covered_by(other)),
|
||||||
// This constructor is never covered by anything else
|
// This constructor is never covered by anything else
|
||||||
NonExhaustive => false,
|
NonExhaustive => false,
|
||||||
Str(..) | FloatRange(..) | Opaque | Missing { .. } | Wildcard | Or => {
|
Str(..) | F32Range(..) | F64Range(..) | Opaque | Missing { .. } | Wildcard | Or => {
|
||||||
span_bug!(pcx.span, "found unexpected ctor in all_ctors: {:?}", self)
|
span_bug!(pcx.span, "found unexpected ctor in all_ctors: {:?}", self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1203,7 +1207,8 @@ impl<'p, 'tcx> Fields<'p, 'tcx> {
|
||||||
_ => bug!("bad slice pattern {:?} {:?}", constructor, pcx),
|
_ => bug!("bad slice pattern {:?} {:?}", constructor, pcx),
|
||||||
},
|
},
|
||||||
Str(..)
|
Str(..)
|
||||||
| FloatRange(..)
|
| F32Range(..)
|
||||||
|
| F64Range(..)
|
||||||
| IntRange(..)
|
| IntRange(..)
|
||||||
| NonExhaustive
|
| NonExhaustive
|
||||||
| Opaque
|
| Opaque
|
||||||
|
@ -1348,8 +1353,19 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
|
||||||
fields = Fields::empty();
|
fields = Fields::empty();
|
||||||
} else {
|
} else {
|
||||||
match pat.ty.kind() {
|
match pat.ty.kind() {
|
||||||
ty::Float(_) => {
|
ty::Float(float_ty) => {
|
||||||
ctor = FloatRange(*value, *value, RangeEnd::Included);
|
let bits = value.eval_bits(cx.tcx, cx.param_env);
|
||||||
|
use rustc_apfloat::Float;
|
||||||
|
ctor = match float_ty {
|
||||||
|
ty::FloatTy::F32 => {
|
||||||
|
let value = rustc_apfloat::ieee::Single::from_bits(bits);
|
||||||
|
F32Range(value, value, RangeEnd::Included)
|
||||||
|
}
|
||||||
|
ty::FloatTy::F64 => {
|
||||||
|
let value = rustc_apfloat::ieee::Double::from_bits(bits);
|
||||||
|
F64Range(value, value, RangeEnd::Included)
|
||||||
|
}
|
||||||
|
};
|
||||||
fields = Fields::empty();
|
fields = Fields::empty();
|
||||||
}
|
}
|
||||||
ty::Ref(_, t, _) if t.is_str() => {
|
ty::Ref(_, t, _) if t.is_str() => {
|
||||||
|
@ -1376,17 +1392,25 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
&PatKind::Range(box PatRange { lo, hi, end }) => {
|
&PatKind::Range(box PatRange { lo, hi, end }) => {
|
||||||
|
use rustc_apfloat::Float;
|
||||||
let ty = lo.ty();
|
let ty = lo.ty();
|
||||||
ctor = if let Some(int_range) = IntRange::from_range(
|
let lo = lo.eval_bits(cx.tcx, cx.param_env);
|
||||||
cx.tcx,
|
let hi = hi.eval_bits(cx.tcx, cx.param_env);
|
||||||
lo.eval_bits(cx.tcx, cx.param_env),
|
ctor = match ty.kind() {
|
||||||
hi.eval_bits(cx.tcx, cx.param_env),
|
ty::Char | ty::Int(_) | ty::Uint(_) => {
|
||||||
ty,
|
IntRange(IntRange::from_range(cx.tcx, lo, hi, ty, &end).unwrap())
|
||||||
&end,
|
}
|
||||||
) {
|
ty::Float(ty::FloatTy::F32) => {
|
||||||
IntRange(int_range)
|
let lo = rustc_apfloat::ieee::Single::from_bits(lo);
|
||||||
} else {
|
let hi = rustc_apfloat::ieee::Single::from_bits(hi);
|
||||||
FloatRange(lo, hi, end)
|
F32Range(lo, hi, *end)
|
||||||
|
}
|
||||||
|
ty::Float(ty::FloatTy::F64) => {
|
||||||
|
let lo = rustc_apfloat::ieee::Double::from_bits(lo);
|
||||||
|
let hi = rustc_apfloat::ieee::Double::from_bits(hi);
|
||||||
|
F64Range(lo, hi, *end)
|
||||||
|
}
|
||||||
|
_ => bug!("invalid type for range pattern: {}", ty),
|
||||||
};
|
};
|
||||||
fields = Fields::empty();
|
fields = Fields::empty();
|
||||||
}
|
}
|
||||||
|
@ -1491,14 +1515,13 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
&Str(value) => PatKind::Constant { value },
|
&Str(value) => PatKind::Constant { value },
|
||||||
&FloatRange(lo, hi, end) => PatKind::Range(Box::new(PatRange { lo, hi, end })),
|
|
||||||
IntRange(range) => return range.to_pat(cx.tcx, self.ty),
|
IntRange(range) => return range.to_pat(cx.tcx, self.ty),
|
||||||
Wildcard | NonExhaustive => PatKind::Wild,
|
Wildcard | NonExhaustive => PatKind::Wild,
|
||||||
Missing { .. } => bug!(
|
Missing { .. } => bug!(
|
||||||
"trying to convert a `Missing` constructor into a `Pat`; this is probably a bug,
|
"trying to convert a `Missing` constructor into a `Pat`; this is probably a bug,
|
||||||
`Missing` should have been processed in `apply_constructors`"
|
`Missing` should have been processed in `apply_constructors`"
|
||||||
),
|
),
|
||||||
Opaque | Or => {
|
F32Range(..) | F64Range(..) | Opaque | Or => {
|
||||||
bug!("can't convert to pattern: {:?}", self)
|
bug!("can't convert to pattern: {:?}", self)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1673,11 +1696,8 @@ impl<'p, 'tcx> fmt::Debug for DeconstructedPat<'p, 'tcx> {
|
||||||
}
|
}
|
||||||
write!(f, "]")
|
write!(f, "]")
|
||||||
}
|
}
|
||||||
&FloatRange(lo, hi, end) => {
|
F32Range(lo, hi, end) => write!(f, "{lo}{end}{hi}"),
|
||||||
write!(f, "{lo}")?;
|
F64Range(lo, hi, end) => write!(f, "{lo}{end}{hi}"),
|
||||||
write!(f, "{end}")?;
|
|
||||||
write!(f, "{hi}")
|
|
||||||
}
|
|
||||||
IntRange(range) => write!(f, "{range:?}"), // Best-effort, will render e.g. `false` as `0..=0`
|
IntRange(range) => write!(f, "{range:?}"), // Best-effort, will render e.g. `false` as `0..=0`
|
||||||
Wildcard | Missing { .. } | NonExhaustive => write!(f, "_ : {:?}", self.ty),
|
Wildcard | Missing { .. } | NonExhaustive => write!(f, "_ : {:?}", self.ty),
|
||||||
Or => {
|
Or => {
|
||||||
|
|
|
@ -1,19 +1,45 @@
|
||||||
|
#![feature(exclusive_range_pattern)]
|
||||||
#![allow(illegal_floating_point_literal_pattern)]
|
#![allow(illegal_floating_point_literal_pattern)]
|
||||||
#![deny(unreachable_patterns)]
|
#![deny(unreachable_patterns)]
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
match 0.0 {
|
match 0.0 {
|
||||||
0.0..=1.0 => {}
|
0.0..=1.0 => {}
|
||||||
_ => {} // ok
|
_ => {} // ok
|
||||||
}
|
}
|
||||||
|
|
||||||
match 0.0 { //~ ERROR non-exhaustive patterns
|
match 0.0 {
|
||||||
0.0..=1.0 => {}
|
//~^ ERROR non-exhaustive patterns
|
||||||
|
0.0..=1.0 => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
match 1.0f64 {
|
match 1.0f64 {
|
||||||
0.01f64 ..= 6.5f64 => {}
|
0.01f64..=6.5f64 => {}
|
||||||
0.02f64 => {} //~ ERROR unreachable pattern
|
0.005f64 => {}
|
||||||
_ => {}
|
0.01f64 => {} //~ ERROR unreachable pattern
|
||||||
|
0.02f64 => {} //~ ERROR unreachable pattern
|
||||||
|
6.5f64 => {} //~ ERROR unreachable pattern
|
||||||
|
6.6f64 => {}
|
||||||
|
1.0f64..=4.0f64 => {} //~ ERROR unreachable pattern
|
||||||
|
5.0f64..=7.0f64 => {}
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
match 1.0f64 {
|
||||||
|
0.01f64..6.5f64 => {}
|
||||||
|
6.5f64 => {} // this is reachable
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
match 1.0f32 {
|
||||||
|
0.01f32..=6.5f32 => {}
|
||||||
|
0.01f32 => {} //~ ERROR unreachable pattern
|
||||||
|
0.02f32 => {} //~ ERROR unreachable pattern
|
||||||
|
6.5f32 => {} //~ ERROR unreachable pattern
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
match 1.0f32 {
|
||||||
|
0.01f32..6.5f32 => {}
|
||||||
|
6.5f32 => {} // this is reachable
|
||||||
|
_ => {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error[E0004]: non-exhaustive patterns: `_` not covered
|
error[E0004]: non-exhaustive patterns: `_` not covered
|
||||||
--> $DIR/floats.rs:10:11
|
--> $DIR/floats.rs:11:11
|
||||||
|
|
|
|
||||||
LL | match 0.0 {
|
LL | match 0.0 {
|
||||||
| ^^^ pattern `_` not covered
|
| ^^^ pattern `_` not covered
|
||||||
|
@ -7,22 +7,58 @@ LL | match 0.0 {
|
||||||
= note: the matched value is of type `f64`
|
= note: the matched value is of type `f64`
|
||||||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
||||||
|
|
|
|
||||||
LL ~ 0.0..=1.0 => {},
|
LL ~ 0.0..=1.0 => {},
|
||||||
LL + _ => todo!()
|
LL + _ => todo!()
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unreachable pattern
|
error: unreachable pattern
|
||||||
--> $DIR/floats.rs:16:7
|
--> $DIR/floats.rs:19:9
|
||||||
|
|
|
|
||||||
LL | 0.02f64 => {}
|
LL | 0.01f64 => {}
|
||||||
| ^^^^^^^
|
| ^^^^^^^
|
||||||
|
|
|
|
||||||
note: the lint level is defined here
|
note: the lint level is defined here
|
||||||
--> $DIR/floats.rs:2:9
|
--> $DIR/floats.rs:3:9
|
||||||
|
|
|
|
||||||
LL | #![deny(unreachable_patterns)]
|
LL | #![deny(unreachable_patterns)]
|
||||||
| ^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: unreachable pattern
|
||||||
|
--> $DIR/floats.rs:20:9
|
||||||
|
|
|
||||||
|
LL | 0.02f64 => {}
|
||||||
|
| ^^^^^^^
|
||||||
|
|
||||||
|
error: unreachable pattern
|
||||||
|
--> $DIR/floats.rs:21:9
|
||||||
|
|
|
||||||
|
LL | 6.5f64 => {}
|
||||||
|
| ^^^^^^
|
||||||
|
|
||||||
|
error: unreachable pattern
|
||||||
|
--> $DIR/floats.rs:23:9
|
||||||
|
|
|
||||||
|
LL | 1.0f64..=4.0f64 => {}
|
||||||
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: unreachable pattern
|
||||||
|
--> $DIR/floats.rs:35:9
|
||||||
|
|
|
||||||
|
LL | 0.01f32 => {}
|
||||||
|
| ^^^^^^^
|
||||||
|
|
||||||
|
error: unreachable pattern
|
||||||
|
--> $DIR/floats.rs:36:9
|
||||||
|
|
|
||||||
|
LL | 0.02f32 => {}
|
||||||
|
| ^^^^^^^
|
||||||
|
|
||||||
|
error: unreachable pattern
|
||||||
|
--> $DIR/floats.rs:37:9
|
||||||
|
|
|
||||||
|
LL | 6.5f32 => {}
|
||||||
|
| ^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to 8 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0004`.
|
For more information about this error, try `rustc --explain E0004`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue