forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#67676 - wesleywiser:lint_overflowing_int_ca…
…sts, r=oli-obk 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. r? @oli-obk
- Loading branch information
Showing
5 changed files
with
216 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
fn main() { | ||
let x = 42u8 as u32; | ||
|
||
let y = 42u32 as u8; | ||
} | ||
|
||
// END RUST SOURCE | ||
// START rustc.main.ConstProp.before.mir | ||
// let mut _0: (); | ||
// let _1: u32; | ||
// scope 1 { | ||
// debug x => _1; | ||
// let _2: u8; | ||
// scope 2 { | ||
// debug y => _2; | ||
// } | ||
// } | ||
// bb0: { | ||
// StorageLive(_1); | ||
// _1 = const 42u8 as u32 (Misc); | ||
// StorageLive(_2); | ||
// _2 = const 42u32 as u8 (Misc); | ||
// _0 = (); | ||
// StorageDead(_2); | ||
// StorageDead(_1); | ||
// return; | ||
// } | ||
// END rustc.main.ConstProp.before.mir | ||
// START rustc.main.ConstProp.after.mir | ||
// let mut _0: (); | ||
// let _1: u32; | ||
// scope 1 { | ||
// debug x => _1; | ||
// let _2: u8; | ||
// scope 2 { | ||
// debug y => _2; | ||
// } | ||
// } | ||
// bb0: { | ||
// StorageLive(_1); | ||
// _1 = const 42u32; | ||
// StorageLive(_2); | ||
// _2 = const 42u8; | ||
// _0 = (); | ||
// StorageDead(_2); | ||
// StorageDead(_1); | ||
// return; | ||
// } | ||
// END rustc.main.ConstProp.after.mir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// build-fail | ||
// ignore-tidy-linelength | ||
|
||
fn main() { | ||
let _ = 0u8 as u32; | ||
let _ = (1u32 << 31) as u16; //~ ERROR truncating cast: the value 2147483648 requires 32 bits but the target type is only 16 bits | ||
let _ = (1u16 << 15) as u8; //~ ERROR truncating cast: the value 32768 requires 16 bits but the target type is only 8 bits | ||
let _ = (!0u16) as u8; //~ ERROR truncating cast: the value 65535 requires 16 bits but the target type is only 8 bits | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: truncating cast: the value 2147483648 requires 32 bits but the target type is only 16 bits | ||
--> $DIR/const-prop-overflowing-casts.rs:6:13 | ||
| | ||
LL | let _ = (1u32 << 31) as u16; | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
|
||
error: truncating cast: the value 32768 requires 16 bits but the target type is only 8 bits | ||
--> $DIR/const-prop-overflowing-casts.rs:7:13 | ||
| | ||
LL | let _ = (1u16 << 15) as u8; | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
||
error: truncating cast: the value 65535 requires 16 bits but the target type is only 8 bits | ||
--> $DIR/const-prop-overflowing-casts.rs:8:13 | ||
| | ||
LL | let _ = (!0u16) as u8; | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters