From 9a7585782e45e661a474f95becc64453c8070dac Mon Sep 17 00:00:00 2001 From: Makai Date: Thu, 13 Aug 2026 16:15:34 +0800 Subject: [PATCH] Change SwitchTargets to use ScalarInt --- compiler/rustc_codegen_ssa/src/mir/block.rs | 7 +++---- compiler/rustc_middle/src/mir/mod.rs | 4 ++-- compiler/rustc_middle/src/mir/syntax.rs | 7 +++---- compiler/rustc_middle/src/mir/terminator.rs | 18 +++++++++--------- .../src/impls/initialized.rs | 2 +- .../rustc_mir_transform/src/ssa_range_prop.rs | 2 +- 6 files changed, 19 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index 7f907bc630b2f..8b1fbe574fdef 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -7,7 +7,6 @@ use rustc_abi::{ }; use rustc_ast as ast; use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; -use rustc_data_structures::packed::Pu128; use rustc_hir::attrs::AttributeKind; use rustc_hir::attrs::lang_items::LangItem; use rustc_lint_defs::builtin::TAIL_CALL_TRACK_CALLER; @@ -15,7 +14,7 @@ use rustc_middle::mir::interpret::{CTFE_ALLOC_SALT, Scalar}; use rustc_middle::mir::{self, AssertKind, InlineAsmMacro, SwitchTargets, UnwindTerminateReason}; use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout, ValidityRequirement}; use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths}; -use rustc_middle::ty::{self, Instance, Ty, TypeVisitableExt}; +use rustc_middle::ty::{self, Instance, ScalarInt, Ty, TypeVisitableExt}; use rustc_middle::{bug, span_bug}; use rustc_session::config::OptLevel; use rustc_span::{Span, Spanned}; @@ -456,8 +455,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } } else if target_iter.len() == 2 && self.mir[targets.otherwise()].is_empty_unreachable() - && targets.all_values().contains(&Pu128(0)) - && targets.all_values().contains(&Pu128(1)) + && targets.all_values().contains(&ScalarInt::from(0_u128)) + && targets.all_values().contains(&ScalarInt::from(1_u128)) { // This is the really common case for `bool`, `Option`, etc. // By using `trunc nuw` we communicate that other values are diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 9fa577ebdcc9b..be170e509e3ad 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -1732,11 +1732,11 @@ mod size_asserts { use super::*; // tidy-alphabetical-start - static_assert_size!(BasicBlockData<'_>, 144); + static_assert_size!(BasicBlockData<'_>, 152); static_assert_size!(LocalDecl<'_>, 40); static_assert_size!(SourceScopeData<'_>, 64); static_assert_size!(Statement<'_>, 40); - static_assert_size!(Terminator<'_>, 104); + static_assert_size!(Terminator<'_>, 112); static_assert_size!(VarDebugInfo<'_>, 88); // tidy-alphabetical-end } diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 4e2d16625266c..c3765f6c210be 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -5,7 +5,6 @@ use rustc_abi::{FieldIdx, VariantIdx}; use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece, Mutability}; -use rustc_data_structures::packed::Pu128; use rustc_hir::CoroutineKind; use rustc_hir::def_id::DefId; use rustc_index::IndexVec; @@ -18,7 +17,7 @@ use smallvec::SmallVec; use super::{BasicBlock, Const, Local, UserTypeProjection}; use crate::mir::coverage::CoverageKind; use crate::ty::adjustment::PointerCoercion; -use crate::ty::{self, GenericArgsRef, List, Region, Ty, UserTypeAnnotationIndex}; +use crate::ty::{self, GenericArgsRef, List, Region, ScalarInt, Ty, UserTypeAnnotationIndex}; /// Represents the "flavors" of MIR. /// @@ -977,7 +976,7 @@ pub enum BackwardIncompatibleDropReason { pub struct SwitchTargets { /// Possible values. For each value, the location to branch to is found in /// the corresponding element in the `targets` vector. - pub(super) values: SmallVec<[Pu128; 1]>, + pub(super) values: SmallVec<[ScalarInt; 1]>, /// Possible branch targets. The last element of this vector is used for /// the "otherwise" branch, so `targets.len() == values.len() + 1` always @@ -1746,6 +1745,6 @@ mod size_asserts { static_assert_size!(PlaceElem<'_>, 24); static_assert_size!(Rvalue<'_>, 40); static_assert_size!(StatementKind<'_>, 16); - static_assert_size!(TerminatorKind<'_>, 80); + static_assert_size!(TerminatorKind<'_>, 88); // tidy-alphabetical-end } diff --git a/compiler/rustc_middle/src/mir/terminator.rs b/compiler/rustc_middle/src/mir/terminator.rs index 2f800c38cd4ac..176e930431bbb 100644 --- a/compiler/rustc_middle/src/mir/terminator.rs +++ b/compiler/rustc_middle/src/mir/terminator.rs @@ -3,7 +3,6 @@ use std::slice; use rustc_ast::InlineAsmOptions; -use rustc_data_structures::packed::Pu128; use rustc_hir::attrs::AttributeKind; use rustc_hir::attrs::lang_items::LangItem; use rustc_macros::{StableHash, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable}; @@ -11,6 +10,7 @@ use smallvec::{SmallVec, smallvec}; use thin_vec::ThinVec; use super::*; +use crate::ty::ScalarInt; impl SwitchTargets { /// Creates switch targets from an iterator of values and target blocks. @@ -19,7 +19,7 @@ impl SwitchTargets { /// `goto otherwise;`. pub fn new(targets: impl Iterator, otherwise: BasicBlock) -> Self { let (values, mut targets): (SmallVec<_>, SmallVec<_>) = - targets.map(|(v, t)| (Pu128(v), t)).unzip(); + targets.map(|(v, t)| (ScalarInt::from(v), t)).unzip(); targets.push(otherwise); Self { values, targets } } @@ -27,7 +27,7 @@ impl SwitchTargets { /// Builds a switch targets definition that jumps to `then` if the tested value equals `value`, /// and to `else_` if not. pub fn static_if(value: u128, then: BasicBlock, else_: BasicBlock) -> Self { - Self { values: smallvec![Pu128(value)], targets: smallvec![then, else_] } + Self { values: smallvec![ScalarInt::from(value)], targets: smallvec![then, else_] } } /// Inverse of `SwitchTargets::static_if`. @@ -36,7 +36,7 @@ impl SwitchTargets { if let &[value] = &self.values[..] && let &[then, else_] = &self.targets[..] { - Some((value.get(), then, else_)) + Some((value.to_u128(), then, else_)) } else { None } @@ -72,12 +72,12 @@ impl SwitchTargets { /// Returns a slice with all considered values (not including the fallback). #[inline] - pub fn all_values(&self) -> &[Pu128] { + pub fn all_values(&self) -> &[ScalarInt] { &self.values } #[inline] - pub fn all_values_mut(&mut self) -> &mut [Pu128] { + pub fn all_values_mut(&mut self) -> &mut [ScalarInt] { &mut self.values } @@ -92,7 +92,7 @@ impl SwitchTargets { /// Adds a new target to the switch. Panics if you add an already present value. #[inline] pub fn add_target(&mut self, value: u128, bb: BasicBlock) { - let value = Pu128(value); + let value = ScalarInt::from(value); if self.values.contains(&value) { bug!("target value {:?} already present", value); } @@ -108,7 +108,7 @@ impl SwitchTargets { } pub struct SwitchTargetsIter<'a> { - inner: iter::Zip, slice::Iter<'a, BasicBlock>>, + inner: iter::Zip, slice::Iter<'a, BasicBlock>>, } impl<'a> Iterator for SwitchTargetsIter<'a> { @@ -116,7 +116,7 @@ impl<'a> Iterator for SwitchTargetsIter<'a> { #[inline] fn next(&mut self) -> Option { - self.inner.next().map(|(val, bb)| (val.get(), *bb)) + self.inner.next().map(|(val, bb)| (val.to_u128(), *bb)) } #[inline] diff --git a/compiler/rustc_mir_dataflow/src/impls/initialized.rs b/compiler/rustc_mir_dataflow/src/impls/initialized.rs index 1b2c58c7e514c..6a4e0c9d623b7 100644 --- a/compiler/rustc_mir_dataflow/src/impls/initialized.rs +++ b/compiler/rustc_mir_dataflow/src/impls/initialized.rs @@ -75,7 +75,7 @@ impl<'tcx> MaybePlacesSwitchIntData<'tcx> { // On each call to this closure `find` only consumes part of // the `discriminants` iterator. discriminants - .find(|(_, discr)| discr.val == value.get()) + .find(|(_, discr)| discr.val == value.to_u128()) .expect("SwitchInt vals should match a variant") .0 }) diff --git a/compiler/rustc_mir_transform/src/ssa_range_prop.rs b/compiler/rustc_mir_transform/src/ssa_range_prop.rs index 0492398fd7bcb..dce23e9617ca7 100644 --- a/compiler/rustc_mir_transform/src/ssa_range_prop.rs +++ b/compiler/rustc_mir_transform/src/ssa_range_prop.rs @@ -198,7 +198,7 @@ impl<'tcx> MutVisitor<'tcx> for RangeSet<'tcx, '_, '_> { && self.unique_predecessors.contains(otherwise.block) { assert_ne!(location.block, otherwise.block); - let range = if val.get() == 0 { + let range = if val.to_u128() == 0 { WrappingRange { start: 1, end: 1 } } else { WrappingRange { start: 0, end: 0 }