Skip to content

Commit e083273

Browse files
committed
Lint overflowing integer casts in const prop
This extends the invalid cases we catch in const prop to include overflowing integer casts using the same machinery as the overflowing binary and unary operation logic.
1 parent a9dd56f commit e083273

File tree

5 files changed

+127
-7
lines changed

5 files changed

+127
-7
lines changed

src/librustc_mir/transform/const_prop.rs

+46-7
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use rustc::mir::visit::{
1212
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
1313
};
1414
use rustc::mir::{
15-
read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, ClearCrossCrate, Constant,
16-
Local, LocalDecl, LocalKind, Location, Operand, Place, PlaceBase, ReadOnlyBodyAndCache, Rvalue,
17-
SourceInfo, SourceScope, SourceScopeData, Statement, StatementKind, Terminator, TerminatorKind,
18-
UnOp, RETURN_PLACE,
15+
read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, CastKind, ClearCrossCrate,
16+
Constant, Local, LocalDecl, LocalKind, Location, Operand, Place, PlaceBase,
17+
ReadOnlyBodyAndCache, Rvalue, SourceInfo, SourceScope, SourceScopeData, Statement,
18+
StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
1919
};
2020
use rustc::ty::layout::{
2121
HasDataLayout, HasTyCtxt, LayoutError, LayoutOf, Size, TargetDataLayout, TyLayout,
@@ -29,9 +29,9 @@ use syntax_pos::{Span, DUMMY_SP};
2929

3030
use crate::const_eval::error_to_const_error;
3131
use crate::interpret::{
32-
self, intern_const_alloc_recursive, AllocId, Allocation, Frame, ImmTy, Immediate, InterpCx,
33-
LocalState, LocalValue, Memory, MemoryKind, OpTy, Operand as InterpOperand, PlaceTy, Pointer,
34-
ScalarMaybeUndef, StackPopCleanup,
32+
self, intern_const_alloc_recursive, truncate, AllocId, Allocation, Frame, ImmTy, Immediate,
33+
InterpCx, LocalState, LocalValue, Memory, MemoryKind, OpTy, Operand as InterpOperand, PlaceTy,
34+
Pointer, ScalarMaybeUndef, StackPopCleanup,
3535
};
3636
use crate::rustc::ty::subst::Subst;
3737
use crate::transform::{MirPass, MirSource};
@@ -584,6 +584,45 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
584584
}
585585
}
586586

587+
Rvalue::Cast(CastKind::Misc, op, ty) => {
588+
trace!("checking Cast(Misc, {:?}, {:?})", op, ty);
589+
590+
if ty.is_integral() && op.ty(&self.local_decls, self.tcx).is_integral() {
591+
let value = self.use_ecx(source_info, |this| {
592+
this.ecx.read_immediate(this.ecx.eval_operand(op, None)?)
593+
})?;
594+
595+
// Do not try to read bits for ZSTs
596+
if !value.layout.is_zst() {
597+
let value_size = value.layout.size;
598+
let value_bits = value.to_scalar().and_then(|r| r.to_bits(value_size));
599+
if let Ok(value_bits) = value_bits {
600+
let truncated = truncate(value_bits, place_layout.size);
601+
if truncated != value_bits {
602+
let scope = source_info.scope;
603+
let lint_root = match &self.source_scopes[scope].local_data {
604+
ClearCrossCrate::Set(data) => data.lint_root,
605+
ClearCrossCrate::Clear => return None,
606+
};
607+
self.tcx.lint_hir(
608+
::rustc::lint::builtin::CONST_ERR,
609+
lint_root,
610+
span,
611+
&format!(
612+
"truncating cast: the value {} requires {} bits but \
613+
the target type is only {} bits",
614+
value_bits,
615+
value_size.bits(),
616+
place_layout.size.bits()
617+
),
618+
);
619+
return None;
620+
}
621+
}
622+
}
623+
}
624+
}
625+
587626
_ => {}
588627
}
589628

src/test/mir-opt/const_prop/cast.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
fn main() {
2+
let x = 42u8 as u32;
3+
4+
let y = 42u32 as u8;
5+
}
6+
7+
// END RUST SOURCE
8+
// START rustc.main.ConstProp.before.mir
9+
// let mut _0: ();
10+
// let _1: u32;
11+
// scope 1 {
12+
// debug x => _1;
13+
// let _2: u8;
14+
// scope 2 {
15+
// debug y => _2;
16+
// }
17+
// }
18+
// bb0: {
19+
// StorageLive(_1);
20+
// _1 = const 42u8 as u32 (Misc);
21+
// StorageLive(_2);
22+
// _2 = const 42u32 as u8 (Misc);
23+
// _0 = ();
24+
// StorageDead(_2);
25+
// StorageDead(_1);
26+
// return;
27+
// }
28+
// END rustc.main.ConstProp.before.mir
29+
// START rustc.main.ConstProp.after.mir
30+
// let mut _0: ();
31+
// let _1: u32;
32+
// scope 1 {
33+
// debug x => _1;
34+
// let _2: u8;
35+
// scope 2 {
36+
// debug y => _2;
37+
// }
38+
// }
39+
// bb0: {
40+
// StorageLive(_1);
41+
// _1 = const 42u32;
42+
// StorageLive(_2);
43+
// _2 = const 42u8;
44+
// _0 = ();
45+
// StorageDead(_2);
46+
// StorageDead(_1);
47+
// return;
48+
// }
49+
// END rustc.main.ConstProp.after.mir
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// build-fail
2+
// ignore-tidy-linelength
3+
4+
fn main() {
5+
let _ = 0u8 as u32;
6+
let _ = (1u32 << 31) as u16; //~ ERROR truncating cast: the value 2147483648 requires 32 bits but the target type is only 16 bits
7+
let _ = (1u16 << 15) as u8; //~ ERROR truncating cast: the value 32768 requires 16 bits but the target type is only 8 bits
8+
let _ = (!0u16) as u8; //~ ERROR truncating cast: the value 65535 requires 16 bits but the target type is only 8 bits
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: truncating cast: the value 2147483648 requires 32 bits but the target type is only 16 bits
2+
--> $DIR/const-prop-overflowing-casts.rs:6:13
3+
|
4+
LL | let _ = (1u32 << 31) as u16;
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[deny(const_err)]` on by default
8+
9+
error: truncating cast: the value 32768 requires 16 bits but the target type is only 8 bits
10+
--> $DIR/const-prop-overflowing-casts.rs:7:13
11+
|
12+
LL | let _ = (1u16 << 15) as u8;
13+
| ^^^^^^^^^^^^^^^^^^
14+
15+
error: truncating cast: the value 65535 requires 16 bits but the target type is only 8 bits
16+
--> $DIR/const-prop-overflowing-casts.rs:8:13
17+
|
18+
LL | let _ = (!0u16) as u8;
19+
| ^^^^^^^^^^^^^
20+
21+
error: aborting due to 3 previous errors
22+

src/test/ui/simd/simd-intrinsic-generic-cast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#![feature(repr_simd, platform_intrinsics, concat_idents, test)]
66
#![allow(non_camel_case_types)]
7+
#![allow(const_err)] // the test macro casts i32s to i8 and u8 which causes lots of warnings
78

89
extern crate test;
910

0 commit comments

Comments
 (0)