Skip to content

Commit dc676b9

Browse files
authored
Rollup merge of rust-lang#74221 - oli-obk:const_prop_ice, r=wesleywiser
Don't panic if the lhs of a div by zero is not statically known Fixes rust-lang#73993 for real this time r? @wesleywiser
2 parents 1355232 + d512a6c commit dc676b9

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

src/librustc_mir/transform/const_prop.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
484484
lint: &'static lint::Lint,
485485
source_info: SourceInfo,
486486
message: &'static str,
487-
panic: AssertKind<ConstInt>,
487+
panic: AssertKind<impl std::fmt::Debug>,
488488
) -> Option<()> {
489489
let lint_root = self.lint_root(source_info)?;
490490
self.tcx.struct_span_lint_hir(lint, lint_root, source_info.span, |lint| {
@@ -1004,11 +1004,27 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
10041004
let expected = ScalarMaybeUninit::from(Scalar::from_bool(*expected));
10051005
let value_const = self.ecx.read_scalar(value).unwrap();
10061006
if expected != value_const {
1007+
enum DbgVal<T> {
1008+
Val(T),
1009+
Underscore,
1010+
}
1011+
impl<T: std::fmt::Debug> std::fmt::Debug for DbgVal<T> {
1012+
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1013+
match self {
1014+
Self::Val(val) => val.fmt(fmt),
1015+
Self::Underscore => fmt.write_str("_"),
1016+
}
1017+
}
1018+
}
10071019
let mut eval_to_int = |op| {
1008-
let op = self
1009-
.eval_operand(op, source_info)
1010-
.expect("if we got here, it must be const");
1011-
self.ecx.read_immediate(op).unwrap().to_const_int()
1020+
// This can be `None` if the lhs wasn't const propagated and we just
1021+
// triggered the assert on the value of the rhs.
1022+
match self.eval_operand(op, source_info) {
1023+
Some(op) => {
1024+
DbgVal::Val(self.ecx.read_immediate(op).unwrap().to_const_int())
1025+
}
1026+
None => DbgVal::Underscore,
1027+
}
10121028
};
10131029
let msg = match msg {
10141030
AssertKind::DivisionByZero(op) => {

src/test/ui/const_prop/ice-assert-fail-div-by-zero.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
// check-pass
1+
// build-pass
2+
3+
// compile-flags: --crate-type lib
4+
5+
#![warn(unconditional_panic)]
26

37
pub struct Fixed64(i64);
48

59
pub fn div(f: Fixed64) {
6-
f.0 / 0;
10+
f.0 / 0; //~ WARN will panic at runtime
711
}
8-
9-
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: this operation will panic at runtime
2+
--> $DIR/ice-assert-fail-div-by-zero.rs:10:5
3+
|
4+
LL | f.0 / 0;
5+
| ^^^^^^^ attempt to divide _ by zero
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/ice-assert-fail-div-by-zero.rs:5:9
9+
|
10+
LL | #![warn(unconditional_panic)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
warning: 1 warning emitted
14+

0 commit comments

Comments
 (0)