Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Prefer a two value enum over bool
  • Loading branch information
oli-obk committed Mar 6, 2025
commit 4f2b108816e782f68d5964bec74448c04bd36ac5
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 4 additions & 7 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_middle/src/ty/pattern.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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}")?;
}
Expand All @@ -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 },
}
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_smir/src/rustc_internal/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 },
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_smir/convert/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_ty_utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down