Add SwitchTargetValue.

This is much clearer than `Option<u128>`.
This commit is contained in:
Nicholas Nethercote 2025-02-14 09:54:01 +11:00
parent 23dbff88f6
commit 8403d39dce
5 changed files with 33 additions and 12 deletions

View file

@ -27,7 +27,15 @@ type Predecessors = IndexVec<BasicBlock, SmallVec<[BasicBlock; 4]>>;
/// `BasicBlocks::switch_sources`, which is only called by backwards analyses
/// that do `SwitchInt` handling, and we don't have any of those, not even in
/// tests. See #95120 and #94576.
type SwitchSources = FxHashMap<(BasicBlock, BasicBlock), SmallVec<[Option<u128>; 1]>>;
type SwitchSources = FxHashMap<(BasicBlock, BasicBlock), SmallVec<[SwitchTargetValue; 1]>>;
#[derive(Debug, Clone, Copy)]
pub enum SwitchTargetValue {
// A normal switch value.
Normal(u128),
// The final "otherwise" fallback value.
Otherwise,
}
#[derive(Clone, Default, Debug)]
struct Cache {
@ -89,9 +97,15 @@ impl<'tcx> BasicBlocks<'tcx> {
}) = &data.terminator
{
for (value, target) in targets.iter() {
switch_sources.entry((target, bb)).or_default().push(Some(value));
switch_sources
.entry((target, bb))
.or_default()
.push(SwitchTargetValue::Normal(value));
}
switch_sources.entry((targets.otherwise(), bb)).or_default().push(None);
switch_sources
.entry((targets.otherwise(), bb))
.or_default()
.push(SwitchTargetValue::Otherwise);
}
}
switch_sources

View file

@ -7,7 +7,7 @@ use std::fmt::{self, Debug, Formatter};
use std::ops::{Index, IndexMut};
use std::{iter, mem};
pub use basic_blocks::BasicBlocks;
pub use basic_blocks::{BasicBlocks, SwitchTargetValue};
use either::Either;
use polonius_engine::Atom;
use rustc_abi::{FieldIdx, VariantIdx};

View file

@ -1,6 +1,8 @@
use std::ops::RangeInclusive;
use rustc_middle::mir::{self, BasicBlock, CallReturnPlaces, Location, TerminatorEdges};
use rustc_middle::mir::{
self, BasicBlock, CallReturnPlaces, Location, SwitchTargetValue, TerminatorEdges,
};
use super::visitor::ResultsVisitor;
use super::{Analysis, Effect, EffectIndex, Results, SwitchIntTarget};
@ -290,7 +292,8 @@ impl Direction for Forward {
let mut tmp = analysis.bottom_value(body);
for (value, target) in targets.iter() {
tmp.clone_from(exit_state);
let si_target = SwitchIntTarget { value: Some(value), target };
let si_target =
SwitchIntTarget { value: SwitchTargetValue::Normal(value), target };
analysis.apply_switch_int_edge_effect(&mut data, &mut tmp, si_target);
propagate(target, &tmp);
}
@ -302,7 +305,7 @@ impl Direction for Forward {
analysis.apply_switch_int_edge_effect(
&mut data,
exit_state,
SwitchIntTarget { value: None, target: otherwise },
SwitchIntTarget { value: SwitchTargetValue::Otherwise, target: otherwise },
);
propagate(otherwise, exit_state);
} else {

View file

@ -38,7 +38,9 @@ use rustc_data_structures::work_queue::WorkQueue;
use rustc_index::bit_set::{DenseBitSet, MixedBitSet};
use rustc_index::{Idx, IndexVec};
use rustc_middle::bug;
use rustc_middle::mir::{self, BasicBlock, CallReturnPlaces, Location, TerminatorEdges, traversal};
use rustc_middle::mir::{
self, BasicBlock, CallReturnPlaces, Location, SwitchTargetValue, TerminatorEdges, traversal,
};
use rustc_middle::ty::TyCtxt;
use tracing::error;
@ -431,7 +433,7 @@ impl EffectIndex {
}
pub struct SwitchIntTarget {
pub value: Option<u128>,
pub value: SwitchTargetValue,
pub target: BasicBlock,
}

View file

@ -4,7 +4,9 @@ use rustc_abi::VariantIdx;
use rustc_index::Idx;
use rustc_index::bit_set::{DenseBitSet, MixedBitSet};
use rustc_middle::bug;
use rustc_middle::mir::{self, Body, CallReturnPlaces, Location, TerminatorEdges};
use rustc_middle::mir::{
self, Body, CallReturnPlaces, Location, SwitchTargetValue, TerminatorEdges,
};
use rustc_middle::ty::util::Discr;
use rustc_middle::ty::{self, TyCtxt};
use tracing::{debug, instrument};
@ -424,7 +426,7 @@ impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
state: &mut Self::Domain,
edge: SwitchIntTarget,
) {
if let Some(value) = edge.value {
if let SwitchTargetValue::Normal(value) = edge.value {
// Kill all move paths that correspond to variants we know to be inactive along this
// particular outgoing edge of a `SwitchInt`.
drop_flag_effects::on_all_inactive_variants(
@ -537,7 +539,7 @@ impl<'tcx> Analysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
state: &mut Self::Domain,
edge: SwitchIntTarget,
) {
if let Some(value) = edge.value {
if let SwitchTargetValue::Normal(value) = edge.value {
// Mark all move paths that correspond to variants other than this one as maybe
// uninitialized (in reality, they are *definitely* uninitialized).
drop_flag_effects::on_all_inactive_variants(