Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConstProp: Correctly remove const if unknown value assigned to it. #118426

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion compiler/rustc_mir_transform/src/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {

// FIXME we need to revisit this for #67176
if rvalue.has_param() {
trace!("skipping, has param");
return None;
}
if !rvalue
Expand Down Expand Up @@ -707,7 +708,11 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
self.super_assign(place, rvalue, location);

let Some(()) = self.check_rvalue(rvalue) else { return };
let Some(()) = self.check_rvalue(rvalue) else {
trace!("rvalue check failed, removing const");
Self::remove_const(&mut self.ecx, place.local);
return;
};

match self.ecx.machine.can_const_prop[place.local] {
// Do nothing if the place is indirect.
Expand Down
26 changes: 26 additions & 0 deletions tests/mir-opt/const_prop/overwrite_with_const_with_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// unit-test: ConstProp
// compile-flags: -O

// Regression test for https://github.com/rust-lang/rust/issues/118328

#![allow(unused_assignments)]

struct SizeOfConst<T>(std::marker::PhantomData<T>);
impl<T> SizeOfConst<T> {
const SIZE: usize = std::mem::size_of::<T>();
}

// EMIT_MIR overwrite_with_const_with_params.size_of.ConstProp.diff
fn size_of<T>() -> usize {
// CHECK-LABEL: fn size_of(
// CHECK: _1 = const 0_usize;
// CHECK-NEXT: _1 = const _;
// CHECK-NEXT: _0 = _1;
let mut a = 0;
a = SizeOfConst::<T>::SIZE;
a
}

fn main() {
assert_eq!(size_of::<u32>(), std::mem::size_of::<u32>());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- // MIR for `size_of` before ConstProp
+ // MIR for `size_of` after ConstProp

fn size_of() -> usize {
let mut _0: usize;
let mut _1: usize;
scope 1 {
debug a => _1;
}

bb0: {
StorageLive(_1);
_1 = const 0_usize;
_1 = const _;
_0 = _1;
StorageDead(_1);
return;
}
}

21 changes: 21 additions & 0 deletions tests/ui/const_prop/overwrite_with_const_with_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// compile-flags: -O
// run-pass

// Regression test for https://github.com/rust-lang/rust/issues/118328

#![allow(unused_assignments)]

struct SizeOfConst<T>(std::marker::PhantomData<T>);
impl<T> SizeOfConst<T> {
const SIZE: usize = std::mem::size_of::<T>();
}

fn size_of<T>() -> usize {
let mut a = 0;
a = SizeOfConst::<T>::SIZE;
a
}

fn main() {
assert_eq!(size_of::<u32>(), std::mem::size_of::<u32>());
}
Loading