Skip to content

Commit a867059

Browse files
committed
Auto merge of rust-lang#99735 - JohnTitor:rollup-d93jyr2, r=JohnTitor
Rollup of 9 pull requests Successful merges: - rust-lang#92390 (Constify a few `(Partial)Ord` impls) - rust-lang#97077 (Simplify some code that depend on Deref) - rust-lang#98710 (correct the output of a `capacity` method example) - rust-lang#99084 (clarify how write_bytes can lead to UB due to invalid values) - rust-lang#99178 (Lighten up const_prop_lint, reusing const_prop) - rust-lang#99673 (don't ICE on invalid dyn calls) - rust-lang#99703 (Expose size_hint() for TokenStream's iterator) - rust-lang#99709 (`Inherited` always has `TypeckResults` available) - rust-lang#99713 (Fix sidebar background) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 6dbae3a + e58bfac commit a867059

File tree

15 files changed

+157
-471
lines changed

15 files changed

+157
-471
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
435435
LocalRef::Place(place) => place,
436436
LocalRef::UnsizedPlace(place) => bx.load_operand(place).deref(cx),
437437
LocalRef::Operand(..) => {
438-
if let Some(elem) = place_ref
439-
.projection
440-
.iter()
441-
.enumerate()
442-
.find(|elem| matches!(elem.1, mir::ProjectionElem::Deref))
443-
{
444-
base = elem.0 + 1;
438+
if place_ref.has_deref() {
439+
base = 1;
445440
let cg_base = self.codegen_consume(
446441
bx,
447-
mir::PlaceRef { projection: &place_ref.projection[..elem.0], ..place_ref },
442+
mir::PlaceRef { projection: &place_ref.projection[..0], ..place_ref },
448443
);
449-
450444
cg_base.deref(bx.cx())
451445
} else {
452446
bug!("using operand local {:?} as place", place_ref);

compiler/rustc_const_eval/src/interpret/terminator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
571571

572572
// Now determine the actual method to call. We can do that in two different ways and
573573
// compare them to ensure everything fits.
574-
let ty::VtblEntry::Method(fn_inst) = self.get_vtable_entries(vptr)?[idx] else {
575-
span_bug!(self.cur_span(), "dyn call index points at something that is not a method")
574+
let Some(ty::VtblEntry::Method(fn_inst)) = self.get_vtable_entries(vptr)?.get(idx).copied() else {
575+
throw_ub_format!("`dyn` call trying to call something that is not a method")
576576
};
577577
if cfg!(debug_assertions) {
578578
let tcx = *self.tcx;

compiler/rustc_middle/src/mir/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,14 @@ impl<'tcx> Place<'tcx> {
14611461
self.projection.iter().any(|elem| elem.is_indirect())
14621462
}
14631463

1464+
/// If MirPhase >= Derefered and if projection contains Deref,
1465+
/// It's guaranteed to be in the first place
1466+
pub fn has_deref(&self) -> bool {
1467+
// To make sure this is not accidently used in wrong mir phase
1468+
debug_assert!(!self.projection[1..].contains(&PlaceElem::Deref));
1469+
self.projection.first() == Some(&PlaceElem::Deref)
1470+
}
1471+
14641472
/// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
14651473
/// a single deref of a local.
14661474
#[inline(always)]
@@ -1533,6 +1541,12 @@ impl<'tcx> PlaceRef<'tcx> {
15331541
}
15341542
}
15351543

1544+
/// If MirPhase >= Derefered and if projection contains Deref,
1545+
/// It's guaranteed to be in the first place
1546+
pub fn has_deref(&self) -> bool {
1547+
self.projection.first() == Some(&PlaceElem::Deref)
1548+
}
1549+
15361550
/// If this place represents a local variable like `_X` with no
15371551
/// projections, return `Some(_X)`.
15381552
#[inline]

compiler/rustc_mir_transform/src/add_retag.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,9 @@ pub struct AddRetag;
1515
/// (Concurrent accesses by other threads are no problem as these are anyway non-atomic
1616
/// copies. Data races are UB.)
1717
fn is_stable(place: PlaceRef<'_>) -> bool {
18-
place.projection.iter().all(|elem| {
19-
match elem {
20-
// Which place this evaluates to can change with any memory write,
21-
// so cannot assume this to be stable.
22-
ProjectionElem::Deref => false,
23-
// Array indices are interesting, but MIR building generates a *fresh*
24-
// temporary for every array access, so the index cannot be changed as
25-
// a side-effect.
26-
ProjectionElem::Index { .. } |
27-
// The rest is completely boring, they just offset by a constant.
28-
ProjectionElem::Field { .. } |
29-
ProjectionElem::ConstantIndex { .. } |
30-
ProjectionElem::Subslice { .. } |
31-
ProjectionElem::Downcast { .. } => true,
32-
}
33-
})
18+
// Which place this evaluates to can change with any memory write,
19+
// so cannot assume deref to be stable.
20+
!place.has_deref()
3421
}
3522

3623
/// Determine whether this type may contain a reference (or box), and thus needs retagging.
@@ -91,11 +78,8 @@ impl<'tcx> MirPass<'tcx> for AddRetag {
9178
};
9279
let place_base_raw = |place: &Place<'tcx>| {
9380
// If this is a `Deref`, get the type of what we are deref'ing.
94-
let deref_base =
95-
place.projection.iter().rposition(|p| matches!(p, ProjectionElem::Deref));
96-
if let Some(deref_base) = deref_base {
97-
let base_proj = &place.projection[..deref_base];
98-
let ty = Place::ty_from(place.local, base_proj, &*local_decls, tcx).ty;
81+
if place.has_deref() {
82+
let ty = &local_decls[place.local].ty;
9983
ty.is_unsafe_ptr()
10084
} else {
10185
// Not a deref, and thus not raw.

compiler/rustc_mir_transform/src/const_prop.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,18 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
155155
}
156156
}
157157

158-
struct ConstPropMachine<'mir, 'tcx> {
158+
pub struct ConstPropMachine<'mir, 'tcx> {
159159
/// The virtual call stack.
160160
stack: Vec<Frame<'mir, 'tcx>>,
161161
/// `OnlyInsideOwnBlock` locals that were written in the current block get erased at the end.
162-
written_only_inside_own_block_locals: FxHashSet<Local>,
162+
pub written_only_inside_own_block_locals: FxHashSet<Local>,
163163
/// Locals that need to be cleared after every block terminates.
164-
only_propagate_inside_block_locals: BitSet<Local>,
165-
can_const_prop: IndexVec<Local, ConstPropMode>,
164+
pub only_propagate_inside_block_locals: BitSet<Local>,
165+
pub can_const_prop: IndexVec<Local, ConstPropMode>,
166166
}
167167

168168
impl ConstPropMachine<'_, '_> {
169-
fn new(
169+
pub fn new(
170170
only_propagate_inside_block_locals: BitSet<Local>,
171171
can_const_prop: IndexVec<Local, ConstPropMode>,
172172
) -> Self {
@@ -816,7 +816,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
816816

817817
/// The mode that `ConstProp` is allowed to run in for a given `Local`.
818818
#[derive(Clone, Copy, Debug, PartialEq)]
819-
enum ConstPropMode {
819+
pub enum ConstPropMode {
820820
/// The `Local` can be propagated into and reads of this `Local` can also be propagated.
821821
FullConstProp,
822822
/// The `Local` can only be propagated into and from its own block.
@@ -828,7 +828,7 @@ enum ConstPropMode {
828828
NoPropagation,
829829
}
830830

831-
struct CanConstProp {
831+
pub struct CanConstProp {
832832
can_const_prop: IndexVec<Local, ConstPropMode>,
833833
// False at the beginning. Once set, no more assignments are allowed to that local.
834834
found_assignment: BitSet<Local>,
@@ -838,7 +838,7 @@ struct CanConstProp {
838838

839839
impl CanConstProp {
840840
/// Returns true if `local` can be propagated
841-
fn check<'tcx>(
841+
pub fn check<'tcx>(
842842
tcx: TyCtxt<'tcx>,
843843
param_env: ParamEnv<'tcx>,
844844
body: &Body<'tcx>,

0 commit comments

Comments
 (0)