about summary refs log tree commit diff
path: root/src/test/ui/consts
diff options
context:
space:
mode:
authorWesley Wiser <wwiser@gmail.com>2019-12-12 22:03:32 -0500
committerWesley Wiser <wwiser@gmail.com>2019-12-30 20:06:31 -0500
commite083273ec77b96a17f317d83fa3fed8ab205964a (patch)
tree609a360b3a82cdd0fdb2b93aa2a9154b3cd82d8a /src/test/ui/consts
parenta9dd56ff9a08d74c53d5cc22d18f126a12749608 (diff)
downloadrust-e083273ec77b96a17f317d83fa3fed8ab205964a.tar.gz
rust-e083273ec77b96a17f317d83fa3fed8ab205964a.zip
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.
Diffstat (limited to 'src/test/ui/consts')
-rw-r--r--src/test/ui/consts/const-prop-overflowing-casts.rs9
-rw-r--r--src/test/ui/consts/const-prop-overflowing-casts.stderr22
2 files changed, 31 insertions, 0 deletions
diff --git a/src/test/ui/consts/const-prop-overflowing-casts.rs b/src/test/ui/consts/const-prop-overflowing-casts.rs
new file mode 100644
index 00000000000..11a04611487
--- /dev/null
+++ b/src/test/ui/consts/const-prop-overflowing-casts.rs
@@ -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
+}
diff --git a/src/test/ui/consts/const-prop-overflowing-casts.stderr b/src/test/ui/consts/const-prop-overflowing-casts.stderr
new file mode 100644
index 00000000000..af4e2c7005a
--- /dev/null
+++ b/src/test/ui/consts/const-prop-overflowing-casts.stderr
@@ -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
+