Skip to content

Commit 4f2b108

Browse files
committed
Prefer a two value enum over bool
1 parent 30f168e commit 4f2b108

File tree

9 files changed

+19
-23
lines changed

9 files changed

+19
-23
lines changed

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,7 @@ pub struct PatField<'hir> {
16001600
pub span: Span,
16011601
}
16021602

1603-
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
1603+
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic, Hash, Eq, Encodable, Decodable)]
16041604
pub enum RangeEnd {
16051605
Included,
16061606
Excluded,

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -2708,11 +2708,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27082708
let start = start.map(|expr| self.lower_const_arg(expr, FeedConstTy::No));
27092709
let end = end.map(|expr| self.lower_const_arg(expr, FeedConstTy::No));
27102710

2711-
let include_end = match include_end {
2712-
hir::RangeEnd::Included => true,
2713-
hir::RangeEnd::Excluded => false,
2714-
};
2715-
27162711
let pat = tcx.mk_pat(ty::PatternKind::Range { start, end, include_end });
27172712
Ty::new_pat(tcx, ty, pat)
27182713
}

compiler/rustc_lint/src/types.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_abi::{BackendRepr, TagEncoding, VariantIdx, Variants, WrappingRange};
55
use rustc_data_structures::fx::FxHashSet;
66
use rustc_errors::DiagMessage;
77
use rustc_hir::intravisit::VisitorExt;
8-
use rustc_hir::{AmbigArg, Expr, ExprKind, HirId, LangItem};
8+
use rustc_hir::{AmbigArg, Expr, ExprKind, HirId, LangItem, RangeEnd};
99
use rustc_middle::bug;
1010
use rustc_middle::ty::layout::{LayoutOf, SizeSkeleton};
1111
use rustc_middle::ty::{
@@ -893,12 +893,9 @@ fn ty_is_known_nonnull<'tcx>(
893893
let end =
894894
end.try_to_value()?.try_to_bits(tcx, typing_env)?;
895895

896-
if include_end {
897-
// This also works for negative numbers, as we just need
898-
// to ensure we aren't wrapping over zero.
899-
start > 0 && end >= start
900-
} else {
901-
start > 0 && end > start
896+
match include_end {
897+
RangeEnd::Included => start > 0 && end >= start,
898+
RangeEnd::Excluded => start > 0 && end > start,
902899
}
903900
}
904901
_ => false,

compiler/rustc_middle/src/ty/pattern.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt;
22

33
use rustc_data_structures::intern::Interned;
4+
use rustc_hir::RangeEnd;
45
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
56

67
use crate::ty;
@@ -30,10 +31,7 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
3031
if let Some(start) = start {
3132
write!(f, "{start}")?;
3233
}
33-
write!(f, "..")?;
34-
if include_end {
35-
write!(f, "=")?;
36-
}
34+
write!(f, "{include_end}")?;
3735
if let Some(end) = end {
3836
write!(f, "{end}")?;
3937
}
@@ -46,5 +44,5 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
4644
#[derive(Clone, PartialEq, Eq, Hash)]
4745
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
4846
pub enum PatternKind<'tcx> {
49-
Range { start: Option<ty::Const<'tcx>>, end: Option<ty::Const<'tcx>>, include_end: bool },
47+
Range { start: Option<ty::Const<'tcx>>, end: Option<ty::Const<'tcx>>, include_end: RangeEnd },
5048
}

compiler/rustc_middle/src/ty/structural_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ TrivialTypeTraversalImpls! {
284284
rustc_hir::def_id::LocalDefId,
285285
rustc_hir::HirId,
286286
rustc_hir::MatchSource,
287+
rustc_hir::RangeEnd,
287288
rustc_span::Ident,
288289
rustc_span::Span,
289290
rustc_span::Symbol,

compiler/rustc_smir/src/rustc_internal/internal.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
// Prefer importing stable_mir over internal rustc constructs to make this file more readable.
77

8+
use rustc_hir::RangeEnd;
89
use rustc_middle::ty::{self as rustc_ty, Const as InternalConst, Ty as InternalTy, TyCtxt};
910
use rustc_span::Symbol;
1011
use stable_mir::abi::Layout;
@@ -91,7 +92,7 @@ impl RustcInternal for Pattern {
9192
Pattern::Range { start, end, include_end } => rustc_ty::PatternKind::Range {
9293
start: start.as_ref().map(|c| c.internal(tables, tcx)),
9394
end: end.as_ref().map(|c| c.internal(tables, tcx)),
94-
include_end: *include_end,
95+
include_end: if *include_end { RangeEnd::Included } else { RangeEnd::Excluded },
9596
},
9697
})
9798
}

compiler/rustc_smir/src/rustc_smir/convert/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
408408
ty::PatternKind::Range { start, end, include_end } => stable_mir::ty::Pattern::Range {
409409
start: start.stable(tables),
410410
end: end.stable(tables),
411-
include_end,
411+
include_end: matches!(include_end, rustc_hir::RangeEnd::Included),
412412
},
413413
}
414414
}

compiler/rustc_symbol_mangling/src/v0.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,10 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
417417
let consts = [
418418
start.unwrap_or(self.tcx.consts.unit),
419419
end.unwrap_or(self.tcx.consts.unit),
420-
ty::Const::from_bool(self.tcx, include_end),
420+
ty::Const::from_bool(
421+
self.tcx,
422+
matches!(include_end, rustc_hir::RangeEnd::Included),
423+
),
421424
];
422425
// HACK: Represent as tuple until we have something better.
423426
// HACK: constants are used in arrays, even if the types don't match.

compiler/rustc_ty_utils/src/layout.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ fn layout_of_uncached<'tcx>(
218218
let mut end = extract_const_value(cx, ty, end)?
219219
.try_to_bits(tcx, cx.typing_env)
220220
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
221-
if !include_end {
222-
end = end.wrapping_sub(1);
221+
match include_end {
222+
rustc_hir::RangeEnd::Included => {}
223+
rustc_hir::RangeEnd::Excluded => end = end.wrapping_sub(1),
223224
}
224225
scalar.valid_range_mut().end = end;
225226
}

0 commit comments

Comments
 (0)