Skip to content

Commit 5133e15

Browse files
committed
Auto merge of #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

Lines changed: 8 additions & 10 deletions
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

Lines changed: 3 additions & 25 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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
}
Lines changed: 15 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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
}
Lines changed: 15 additions & 0 deletions
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+
Lines changed: 23 additions & 0 deletions
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+

0 commit comments

Comments
 (0)