Skip to content

Commit 48230d3

Browse files
committed
Prefer a two value enum over bool
1 parent eeb9035 commit 48230d3

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
@@ -1516,7 +1516,7 @@ pub struct PatField<'hir> {
15161516
pub span: Span,
15171517
}
15181518

1519-
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
1519+
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic, Hash, Eq, Encodable, Decodable)]
15201520
pub enum RangeEnd {
15211521
Included,
15221522
Excluded,

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

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

2473-
let include_end = match include_end {
2474-
hir::RangeEnd::Included => true,
2475-
hir::RangeEnd::Excluded => false,
2476-
};
2477-
24782473
let pat = tcx.mk_pat(ty::PatternKind::Range { start, end, include_end });
24792474
Ty::new_pat(tcx, ty, pat)
24802475
}

compiler/rustc_lint/src/types.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_abi::{BackendRepr, ExternAbi, TagEncoding, VariantIdx, Variants, Wrapp
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
@@ -207,8 +207,9 @@ fn layout_of_uncached<'tcx>(
207207
let mut end = extract_const_value(cx, ty, end)?
208208
.try_to_bits(tcx, cx.typing_env)
209209
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
210-
if !include_end {
211-
end = end.wrapping_sub(1);
210+
match include_end {
211+
rustc_hir::RangeEnd::Included => {}
212+
rustc_hir::RangeEnd::Excluded => end = end.wrapping_sub(1),
212213
}
213214
scalar.valid_range_mut().end = end;
214215
}

0 commit comments

Comments
 (0)