Skip to content

Commit 5133e15

Browse files
committed
Auto merge of rust-lang#109521 - tmiasko:const-prop-validation, r=wesleywiser
Don't validate constants in const propagation Validation is neither necessary nor desirable. The constant validation is already omitted at mir-opt-level >= 3, so there there are not changes in MIR test output (the propagation of invalid constants is covered by an existing test in tests/mir-opt/const_prop/invalid_constant.rs).
2 parents 1cb6357 + d1bd1be commit 5133e15

23 files changed

+247
-40
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ macro_rules! throw_validation_failure {
3838
msg.push_str(", but expected ");
3939
write!(&mut msg, $($expected_fmt)*).unwrap();
4040
)?
41-
let path = rustc_middle::ty::print::with_no_trimmed_paths!({
42-
let where_ = &$where;
43-
if !where_.is_empty() {
44-
let mut path = String::new();
45-
write_path(&mut path, where_);
46-
Some(path)
47-
} else {
48-
None
49-
}
50-
});
41+
let where_ = &$where;
42+
let path = if !where_.is_empty() {
43+
let mut path = String::new();
44+
write_path(&mut path, where_);
45+
Some(path)
46+
} else {
47+
None
48+
};
5149
throw_ub!(ValidationFailure { path, msg })
5250
}};
5351
}

compiler/rustc_mir_transform/src/const_prop.rs

+3-25
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use rustc_target::spec::abi::Abi as CallAbi;
2121

2222
use crate::MirPass;
2323
use rustc_const_eval::interpret::{
24-
self, compile_time_machine, AllocId, ConstAllocation, ConstValue, CtfeValidationMode, Frame,
25-
ImmTy, Immediate, InterpCx, InterpResult, LocalValue, MemoryKind, OpTy, PlaceTy, Pointer,
26-
Scalar, StackPopCleanup,
24+
self, compile_time_machine, AllocId, ConstAllocation, ConstValue, Frame, ImmTy, Immediate,
25+
InterpCx, InterpResult, LocalValue, MemoryKind, OpTy, PlaceTy, Pointer, Scalar,
26+
StackPopCleanup,
2727
};
2828

2929
/// The maximum number of bytes that we'll allocate space for a local or the return value.
@@ -464,16 +464,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
464464

465465
return None;
466466
}
467-
// Do not try creating references, nor any types with potentially-complex
468-
// invariants. This avoids an issue where checking validity would do a
469-
// bunch of work generating a nice message about the invariant violation,
470-
// only to not show it to anyone (since this isn't the lint).
471-
Rvalue::Cast(CastKind::Transmute, op, dst_ty) if !dst_ty.is_primitive() => {
472-
trace!("skipping Transmute of {:?} to {:?}", op, dst_ty);
473-
474-
return None;
475-
}
476-
477467
// There's no other checking to do at this time.
478468
Rvalue::Aggregate(..)
479469
| Rvalue::Use(..)
@@ -591,18 +581,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
591581
}
592582

593583
trace!("attempting to replace {:?} with {:?}", rval, value);
594-
if let Err(e) = self.ecx.const_validate_operand(
595-
value,
596-
vec![],
597-
// FIXME: is ref tracking too expensive?
598-
// FIXME: what is the point of ref tracking if we do not even check the tracked refs?
599-
&mut interpret::RefTracking::empty(),
600-
CtfeValidationMode::Regular,
601-
) {
602-
trace!("validation error, attempt failed: {:?}", e);
603-
return;
604-
}
605-
606584
// FIXME> figure out what to do when read_immediate_raw fails
607585
let imm = self.ecx.read_immediate_raw(value).ok();
608586

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- // MIR for `from_char` before ConstProp
2+
+ // MIR for `from_char` after ConstProp
3+
4+
fn from_char() -> i32 {
5+
let mut _0: i32; // return place in scope 0 at $DIR/transmute.rs:+0:23: +0:26
6+
scope 1 {
7+
}
8+
9+
bb0: {
10+
- _0 = const 'R' as i32 (Transmute); // scope 1 at $DIR/transmute.rs:+1:14: +1:28
11+
+ _0 = const 82_i32; // scope 1 at $DIR/transmute.rs:+1:14: +1:28
12+
return; // scope 0 at $DIR/transmute.rs:+2:2: +2:2
13+
}
14+
}
15+

tests/mir-opt/const_prop/transmute.invalid_bool.ConstProp.diff renamed to tests/mir-opt/const_prop/transmute.invalid_bool.ConstProp.32bit.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
}
88

99
bb0: {
10-
_0 = const -1_i8 as bool (Transmute); // scope 1 at $DIR/transmute.rs:+1:14: +1:30
10+
- _0 = const -1_i8 as bool (Transmute); // scope 1 at $DIR/transmute.rs:+1:14: +1:30
11+
+ _0 = const {transmute(0xff): bool}; // scope 1 at $DIR/transmute.rs:+1:14: +1:30
1112
return; // scope 0 at $DIR/transmute.rs:+2:2: +2:2
1213
}
1314
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- // MIR for `invalid_bool` before ConstProp
2+
+ // MIR for `invalid_bool` after ConstProp
3+
4+
fn invalid_bool() -> bool {
5+
let mut _0: bool; // return place in scope 0 at $DIR/transmute.rs:+0:33: +0:37
6+
scope 1 {
7+
}
8+
9+
bb0: {
10+
- _0 = const -1_i8 as bool (Transmute); // scope 1 at $DIR/transmute.rs:+1:14: +1:30
11+
+ _0 = const {transmute(0xff): bool}; // scope 1 at $DIR/transmute.rs:+1:14: +1:30
12+
return; // scope 0 at $DIR/transmute.rs:+2:2: +2:2
13+
}
14+
}
15+

tests/mir-opt/const_prop/transmute.invalid_char.ConstProp.diff renamed to tests/mir-opt/const_prop/transmute.invalid_char.ConstProp.32bit.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
}
88

99
bb0: {
10-
_0 = const _ as char (Transmute); // scope 1 at $DIR/transmute.rs:+1:14: +1:33
10+
- _0 = const _ as char (Transmute); // scope 1 at $DIR/transmute.rs:+1:14: +1:33
11+
+ _0 = const {transmute(0x7fffffff): char}; // scope 1 at $DIR/transmute.rs:+1:14: +1:33
1112
return; // scope 0 at $DIR/transmute.rs:+2:2: +2:2
1213
}
1314
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- // MIR for `invalid_char` before ConstProp
2+
+ // MIR for `invalid_char` after ConstProp
3+
4+
fn invalid_char() -> char {
5+
let mut _0: char; // return place in scope 0 at $DIR/transmute.rs:+0:33: +0:37
6+
scope 1 {
7+
}
8+
9+
bb0: {
10+
- _0 = const _ as char (Transmute); // scope 1 at $DIR/transmute.rs:+1:14: +1:33
11+
+ _0 = const {transmute(0x7fffffff): char}; // scope 1 at $DIR/transmute.rs:+1:14: +1:33
12+
return; // scope 0 at $DIR/transmute.rs:+2:2: +2:2
13+
}
14+
}
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
- // MIR for `less_as_i8` before ConstProp
2+
+ // MIR for `less_as_i8` after ConstProp
3+
4+
fn less_as_i8() -> i8 {
5+
let mut _0: i8; // return place in scope 0 at $DIR/transmute.rs:+0:24: +0:26
6+
let mut _1: std::cmp::Ordering; // in scope 0 at $DIR/transmute.rs:+1:24: +1:48
7+
scope 1 {
8+
}
9+
10+
bb0: {
11+
StorageLive(_1); // scope 1 at $DIR/transmute.rs:+1:24: +1:48
12+
- _1 = Less; // scope 1 at $DIR/transmute.rs:+1:24: +1:48
13+
- _0 = move _1 as i8 (Transmute); // scope 1 at $DIR/transmute.rs:+1:14: +1:49
14+
+ _1 = const Less; // scope 1 at $DIR/transmute.rs:+1:24: +1:48
15+
+ // mir::Constant
16+
+ // + span: no-location
17+
+ // + literal: Const { ty: std::cmp::Ordering, val: Value(Scalar(0xff)) }
18+
+ _0 = const -1_i8; // scope 1 at $DIR/transmute.rs:+1:14: +1:49
19+
StorageDead(_1); // scope 1 at $DIR/transmute.rs:+1:48: +1:49
20+
return; // scope 0 at $DIR/transmute.rs:+2:2: +2:2
21+
}
22+
}
23+

tests/mir-opt/const_prop/transmute.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// unit-test: ConstProp
22
// compile-flags: -O --crate-type=lib
3+
// ignore-endian-big
4+
// EMIT_MIR_FOR_EACH_BIT_WIDTH
35

46
use std::mem::transmute;
57

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
- // MIR for `undef_union_as_integer` before ConstProp
2+
+ // MIR for `undef_union_as_integer` after ConstProp
3+
4+
fn undef_union_as_integer() -> u32 {
5+
let mut _0: u32; // return place in scope 0 at $DIR/transmute.rs:+0:43: +0:46
6+
let mut _1: undef_union_as_integer::Union32; // in scope 0 at $DIR/transmute.rs:+2:24: +2:44
7+
let mut _2: (); // in scope 0 at $DIR/transmute.rs:+2:40: +2:42
8+
scope 1 {
9+
}
10+
11+
bb0: {
12+
StorageLive(_1); // scope 1 at $DIR/transmute.rs:+2:24: +2:44
13+
StorageLive(_2); // scope 1 at $DIR/transmute.rs:+2:40: +2:42
14+
_2 = (); // scope 1 at $DIR/transmute.rs:+2:40: +2:42
15+
_1 = Union32 { value: move _2 }; // scope 1 at $DIR/transmute.rs:+2:24: +2:44
16+
StorageDead(_2); // scope 1 at $DIR/transmute.rs:+2:43: +2:44
17+
_0 = move _1 as u32 (Transmute); // scope 1 at $DIR/transmute.rs:+2:14: +2:45
18+
StorageDead(_1); // scope 1 at $DIR/transmute.rs:+2:44: +2:45
19+
return; // scope 0 at $DIR/transmute.rs:+3:2: +3:2
20+
}
21+
}
22+

tests/mir-opt/const_prop/transmute.unreachable_box.ConstProp.diff renamed to tests/mir-opt/const_prop/transmute.unreachable_box.ConstProp.32bit.diff

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
bb0: {
1616
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
1717
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
18-
_2 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
18+
- _2 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
19+
+ _2 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
20+
+ // mir::Constant
21+
+ // + span: no-location
22+
+ // + literal: Const { ty: Box<Never>, val: Value(Scalar(0x00000001)) }
1923
StorageLive(_3); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
2024
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
2125
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
- // MIR for `unreachable_box` before ConstProp
2+
+ // MIR for `unreachable_box` after ConstProp
3+
4+
fn unreachable_box() -> ! {
5+
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:36: +0:37
6+
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:38: +3:2
7+
let _2: std::boxed::Box<Never>; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
8+
let mut _3: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:16
9+
scope 1 {
10+
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
11+
}
12+
scope 2 {
13+
}
14+
15+
bb0: {
16+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
17+
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
18+
- _2 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
19+
+ _2 = const Box::<Never>(Unique::<Never> {{ pointer: NonNull::<Never> {{ pointer: {0x1 as *const Never} }}, _marker: PhantomData::<Never> }}, std::alloc::Global); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
20+
+ // mir::Constant
21+
+ // + span: no-location
22+
+ // + literal: Const { ty: Box<Never>, val: Value(Scalar(0x0000000000000001)) }
23+
StorageLive(_3); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
24+
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
25+
}
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
- // MIR for `unreachable_direct` before ConstProp
2+
+ // MIR for `unreachable_direct` after ConstProp
3+
4+
fn unreachable_direct() -> ! {
5+
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:39: +0:40
6+
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:41: +3:2
7+
let _2: Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
8+
let mut _3: (); // in scope 0 at $DIR/transmute.rs:+1:39: +1:41
9+
let mut _4: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:15
10+
scope 1 {
11+
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
12+
}
13+
scope 2 {
14+
}
15+
16+
bb0: {
17+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:41: +3:2
18+
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
19+
StorageLive(_3); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
20+
_3 = (); // scope 2 at $DIR/transmute.rs:+1:39: +1:41
21+
_2 = move _3 as Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:29: +1:42
22+
unreachable; // scope 2 at $DIR/transmute.rs:+1:29: +1:42
23+
}
24+
}
25+

tests/mir-opt/const_prop/transmute.unreachable_mut.ConstProp.diff renamed to tests/mir-opt/const_prop/transmute.unreachable_mut.ConstProp.32bit.diff

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
1818
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
1919
StorageLive(_3); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
20-
_3 = const 1_usize as &mut Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
20+
- _3 = const 1_usize as &mut Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
21+
+ _3 = const {0x1 as &mut Never}; // scope 2 at $DIR/transmute.rs:+1:34: +1:52
22+
+ // mir::Constant
23+
+ // + span: no-location
24+
+ // + literal: Const { ty: &mut Never, val: Value(Scalar(0x00000001)) }
2125
_2 = &mut (*_3); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
2226
StorageDead(_3); // scope 0 at $DIR/transmute.rs:+1:54: +1:55
2327
StorageLive(_4); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
- // MIR for `unreachable_mut` before ConstProp
2+
+ // MIR for `unreachable_mut` after ConstProp
3+
4+
fn unreachable_mut() -> ! {
5+
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:36: +0:37
6+
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:38: +3:2
7+
let _2: &mut Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
8+
let mut _3: &mut Never; // in scope 0 at $DIR/transmute.rs:+1:34: +1:52
9+
let mut _4: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:16
10+
scope 1 {
11+
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
12+
}
13+
scope 2 {
14+
}
15+
16+
bb0: {
17+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
18+
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
19+
StorageLive(_3); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
20+
- _3 = const 1_usize as &mut Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:34: +1:52
21+
+ _3 = const {0x1 as &mut Never}; // scope 2 at $DIR/transmute.rs:+1:34: +1:52
22+
+ // mir::Constant
23+
+ // + span: no-location
24+
+ // + literal: Const { ty: &mut Never, val: Value(Scalar(0x0000000000000001)) }
25+
_2 = &mut (*_3); // scope 0 at $DIR/transmute.rs:+1:34: +1:52
26+
StorageDead(_3); // scope 0 at $DIR/transmute.rs:+1:54: +1:55
27+
StorageLive(_4); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
28+
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
29+
}
30+
}
31+

tests/mir-opt/const_prop/transmute.unreachable_ref.ConstProp.diff renamed to tests/mir-opt/const_prop/transmute.unreachable_ref.ConstProp.32bit.diff

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
bb0: {
1616
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
1717
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
18-
_2 = const 1_usize as &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
18+
- _2 = const 1_usize as &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
19+
+ _2 = const {0x1 as &Never}; // scope 2 at $DIR/transmute.rs:+1:30: +1:48
20+
+ // mir::Constant
21+
+ // + span: no-location
22+
+ // + literal: Const { ty: &Never, val: Value(Scalar(0x00000001)) }
1923
StorageLive(_3); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
2024
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
2125
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
- // MIR for `unreachable_ref` before ConstProp
2+
+ // MIR for `unreachable_ref` after ConstProp
3+
4+
fn unreachable_ref() -> ! {
5+
let mut _0: !; // return place in scope 0 at $DIR/transmute.rs:+0:36: +0:37
6+
let mut _1: !; // in scope 0 at $DIR/transmute.rs:+0:38: +3:2
7+
let _2: &Never; // in scope 0 at $DIR/transmute.rs:+1:9: +1:10
8+
let mut _3: !; // in scope 0 at $DIR/transmute.rs:+2:5: +2:16
9+
scope 1 {
10+
debug x => _2; // in scope 1 at $DIR/transmute.rs:+1:9: +1:10
11+
}
12+
scope 2 {
13+
}
14+
15+
bb0: {
16+
StorageLive(_1); // scope 0 at $DIR/transmute.rs:+0:38: +3:2
17+
StorageLive(_2); // scope 0 at $DIR/transmute.rs:+1:9: +1:10
18+
- _2 = const 1_usize as &Never (Transmute); // scope 2 at $DIR/transmute.rs:+1:30: +1:48
19+
+ _2 = const {0x1 as &Never}; // scope 2 at $DIR/transmute.rs:+1:30: +1:48
20+
+ // mir::Constant
21+
+ // + span: no-location
22+
+ // + literal: Const { ty: &Never, val: Value(Scalar(0x0000000000000001)) }
23+
StorageLive(_3); // scope 1 at $DIR/transmute.rs:+2:5: +2:16
24+
unreachable; // scope 1 at $DIR/transmute.rs:+2:11: +2:13
25+
}
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- // MIR for `valid_char` before ConstProp
2+
+ // MIR for `valid_char` after ConstProp
3+
4+
fn valid_char() -> char {
5+
let mut _0: char; // return place in scope 0 at $DIR/transmute.rs:+0:24: +0:28
6+
scope 1 {
7+
}
8+
9+
bb0: {
10+
- _0 = const 82_u32 as char (Transmute); // scope 1 at $DIR/transmute.rs:+1:14: +1:33
11+
+ _0 = const 'R'; // scope 1 at $DIR/transmute.rs:+1:14: +1:33
12+
return; // scope 0 at $DIR/transmute.rs:+2:2: +2:2
13+
}
14+
}
15+

0 commit comments

Comments
 (0)