diff options
| author | Oli Scherer <github333195615777966@oli-obk.de> | 2025-02-12 10:37:49 +0000 |
|---|---|---|
| committer | Oli Scherer <github333195615777966@oli-obk.de> | 2025-03-06 10:03:11 +0000 |
| commit | 4f2b108816e782f68d5964bec74448c04bd36ac5 (patch) | |
| tree | eb13cb11bda8ada297e58273595157818b57dddf | |
| parent | 30f168ef811aec63124eac677e14699baa9395bd (diff) | |
| download | rust-4f2b108816e782f68d5964bec74448c04bd36ac5.tar.gz rust-4f2b108816e782f68d5964bec74448c04bd36ac5.zip | |
Prefer a two value enum over bool
| -rw-r--r-- | compiler/rustc_hir/src/hir.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_lint/src/types.rs | 11 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/pattern.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/structural_impls.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_smir/src/rustc_internal/internal.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_smir/src/rustc_smir/convert/ty.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_symbol_mangling/src/v0.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_ty_utils/src/layout.rs | 5 |
9 files changed, 19 insertions, 23 deletions
diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 399f1f4b237..3b506039025 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -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, diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index d044688246f..fe446492aca 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -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) } diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index d2b08eab479..8304d533c26 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -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, diff --git a/compiler/rustc_middle/src/ty/pattern.rs b/compiler/rustc_middle/src/ty/pattern.rs index e604aedd05e..48ae5b1e657 100644 --- a/compiler/rustc_middle/src/ty/pattern.rs +++ b/compiler/rustc_middle/src/ty/pattern.rs @@ -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 }, } diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index db9e9fbc643..6c62c04f42e 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -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, diff --git a/compiler/rustc_smir/src/rustc_internal/internal.rs b/compiler/rustc_smir/src/rustc_internal/internal.rs index 50cf605ba2a..093ce5f5648 100644 --- a/compiler/rustc_smir/src/rustc_internal/internal.rs +++ b/compiler/rustc_smir/src/rustc_internal/internal.rs @@ -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 }, }, }) } diff --git a/compiler/rustc_smir/src/rustc_smir/convert/ty.rs b/compiler/rustc_smir/src/rustc_smir/convert/ty.rs index a0faf20c79a..9c06d08d61b 100644 --- a/compiler/rustc_smir/src/rustc_smir/convert/ty.rs +++ b/compiler/rustc_smir/src/rustc_smir/convert/ty.rs @@ -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), }, } } diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index a11f6f0df1c..79ab51ec9b3 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -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. diff --git a/compiler/rustc_ty_utils/src/layout.rs b/compiler/rustc_ty_utils/src/layout.rs index 852d3fc58d8..2ba3fb22e6c 100644 --- a/compiler/rustc_ty_utils/src/layout.rs +++ b/compiler/rustc_ty_utils/src/layout.rs @@ -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; } |
