Prefer a two value enum over bool
This commit is contained in:
parent
30f168ef81
commit
4f2b108816
9 changed files with 19 additions and 23 deletions
|
@ -1600,7 +1600,7 @@ pub struct PatField<'hir> {
|
|||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
|
||||
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic, Hash, Eq, Encodable, Decodable)]
|
||||
pub enum RangeEnd {
|
||||
Included,
|
||||
Excluded,
|
||||
|
|
|
@ -2708,11 +2708,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
let start = start.map(|expr| self.lower_const_arg(expr, FeedConstTy::No));
|
||||
let end = end.map(|expr| self.lower_const_arg(expr, FeedConstTy::No));
|
||||
|
||||
let include_end = match include_end {
|
||||
hir::RangeEnd::Included => true,
|
||||
hir::RangeEnd::Excluded => false,
|
||||
};
|
||||
|
||||
let pat = tcx.mk_pat(ty::PatternKind::Range { start, end, include_end });
|
||||
Ty::new_pat(tcx, ty, pat)
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ use rustc_abi::{BackendRepr, TagEncoding, VariantIdx, Variants, WrappingRange};
|
|||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_errors::DiagMessage;
|
||||
use rustc_hir::intravisit::VisitorExt;
|
||||
use rustc_hir::{AmbigArg, Expr, ExprKind, HirId, LangItem};
|
||||
use rustc_hir::{AmbigArg, Expr, ExprKind, HirId, LangItem, RangeEnd};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::ty::layout::{LayoutOf, SizeSkeleton};
|
||||
use rustc_middle::ty::{
|
||||
|
@ -893,12 +893,9 @@ fn ty_is_known_nonnull<'tcx>(
|
|||
let end =
|
||||
end.try_to_value()?.try_to_bits(tcx, typing_env)?;
|
||||
|
||||
if include_end {
|
||||
// This also works for negative numbers, as we just need
|
||||
// to ensure we aren't wrapping over zero.
|
||||
start > 0 && end >= start
|
||||
} else {
|
||||
start > 0 && end > start
|
||||
match include_end {
|
||||
RangeEnd::Included => start > 0 && end >= start,
|
||||
RangeEnd::Excluded => start > 0 && end > start,
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::fmt;
|
||||
|
||||
use rustc_data_structures::intern::Interned;
|
||||
use rustc_hir::RangeEnd;
|
||||
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
|
||||
|
||||
use crate::ty;
|
||||
|
@ -30,10 +31,7 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
|
|||
if let Some(start) = start {
|
||||
write!(f, "{start}")?;
|
||||
}
|
||||
write!(f, "..")?;
|
||||
if include_end {
|
||||
write!(f, "=")?;
|
||||
}
|
||||
write!(f, "{include_end}")?;
|
||||
if let Some(end) = end {
|
||||
write!(f, "{end}")?;
|
||||
}
|
||||
|
@ -46,5 +44,5 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
|
|||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
|
||||
pub enum PatternKind<'tcx> {
|
||||
Range { start: Option<ty::Const<'tcx>>, end: Option<ty::Const<'tcx>>, include_end: bool },
|
||||
Range { start: Option<ty::Const<'tcx>>, end: Option<ty::Const<'tcx>>, include_end: RangeEnd },
|
||||
}
|
||||
|
|
|
@ -284,6 +284,7 @@ TrivialTypeTraversalImpls! {
|
|||
rustc_hir::def_id::LocalDefId,
|
||||
rustc_hir::HirId,
|
||||
rustc_hir::MatchSource,
|
||||
rustc_hir::RangeEnd,
|
||||
rustc_span::Ident,
|
||||
rustc_span::Span,
|
||||
rustc_span::Symbol,
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
// Prefer importing stable_mir over internal rustc constructs to make this file more readable.
|
||||
|
||||
use rustc_hir::RangeEnd;
|
||||
use rustc_middle::ty::{self as rustc_ty, Const as InternalConst, Ty as InternalTy, TyCtxt};
|
||||
use rustc_span::Symbol;
|
||||
use stable_mir::abi::Layout;
|
||||
|
@ -91,7 +92,7 @@ impl RustcInternal for Pattern {
|
|||
Pattern::Range { start, end, include_end } => rustc_ty::PatternKind::Range {
|
||||
start: start.as_ref().map(|c| c.internal(tables, tcx)),
|
||||
end: end.as_ref().map(|c| c.internal(tables, tcx)),
|
||||
include_end: *include_end,
|
||||
include_end: if *include_end { RangeEnd::Included } else { RangeEnd::Excluded },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
@ -408,7 +408,7 @@ impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
|
|||
ty::PatternKind::Range { start, end, include_end } => stable_mir::ty::Pattern::Range {
|
||||
start: start.stable(tables),
|
||||
end: end.stable(tables),
|
||||
include_end,
|
||||
include_end: matches!(include_end, rustc_hir::RangeEnd::Included),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -417,7 +417,10 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
|
|||
let consts = [
|
||||
start.unwrap_or(self.tcx.consts.unit),
|
||||
end.unwrap_or(self.tcx.consts.unit),
|
||||
ty::Const::from_bool(self.tcx, include_end),
|
||||
ty::Const::from_bool(
|
||||
self.tcx,
|
||||
matches!(include_end, rustc_hir::RangeEnd::Included),
|
||||
),
|
||||
];
|
||||
// HACK: Represent as tuple until we have something better.
|
||||
// HACK: constants are used in arrays, even if the types don't match.
|
||||
|
|
|
@ -218,8 +218,9 @@ fn layout_of_uncached<'tcx>(
|
|||
let mut end = extract_const_value(cx, ty, end)?
|
||||
.try_to_bits(tcx, cx.typing_env)
|
||||
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
|
||||
if !include_end {
|
||||
end = end.wrapping_sub(1);
|
||||
match include_end {
|
||||
rustc_hir::RangeEnd::Included => {}
|
||||
rustc_hir::RangeEnd::Excluded => end = end.wrapping_sub(1),
|
||||
}
|
||||
scalar.valid_range_mut().end = end;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue