diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/discrim | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/discrim')
-rw-r--r-- | src/test/ui/discrim/discrim-ill-typed.rs | 114 | ||||
-rw-r--r-- | src/test/ui/discrim/discrim-ill-typed.stderr | 91 | ||||
-rw-r--r-- | src/test/ui/discrim/discrim-overflow-2.rs | 80 | ||||
-rw-r--r-- | src/test/ui/discrim/discrim-overflow-2.stderr | 67 | ||||
-rw-r--r-- | src/test/ui/discrim/discrim-overflow.rs | 99 | ||||
-rw-r--r-- | src/test/ui/discrim/discrim-overflow.stderr | 67 |
6 files changed, 518 insertions, 0 deletions
diff --git a/src/test/ui/discrim/discrim-ill-typed.rs b/src/test/ui/discrim/discrim-ill-typed.rs new file mode 100644 index 000000000..3cf0ea0e6 --- /dev/null +++ b/src/test/ui/discrim/discrim-ill-typed.rs @@ -0,0 +1,114 @@ +// When explicit discriminant value has +// a type that does not match the representation +// type, rustc should fail gracefully. + +// See also run-pass/discrim-explicit-23030.rs where the input types +// are correct. + +#![allow(dead_code, unused_variables, unused_imports)] + +fn f_i8() { + #[repr(i8)] + enum A { + Ok = i8::MAX - 1, + Ok2, + OhNo = 0_u8, + //~^ ERROR mismatched types + //~| expected `i8`, found `u8` + } + + let x = A::Ok; +} + +fn f_u8() { + #[repr(u8)] + enum A { + Ok = u8::MAX - 1, + Ok2, + OhNo = 0_i8, + //~^ ERROR mismatched types + //~| expected `u8`, found `i8` + } + + let x = A::Ok; +} + +fn f_i16() { + #[repr(i16)] + enum A { + Ok = i16::MAX - 1, + Ok2, + OhNo = 0_u16, + //~^ ERROR mismatched types + //~| expected `i16`, found `u16` + } + + let x = A::Ok; +} + +fn f_u16() { + #[repr(u16)] + enum A { + Ok = u16::MAX - 1, + Ok2, + OhNo = 0_i16, + //~^ ERROR mismatched types + //~| expected `u16`, found `i16` + } + + let x = A::Ok; +} + +fn f_i32() { + #[repr(i32)] + enum A { + Ok = i32::MAX - 1, + Ok2, + OhNo = 0_u32, + //~^ ERROR mismatched types + //~| expected `i32`, found `u32` + } + + let x = A::Ok; +} + +fn f_u32() { + #[repr(u32)] + enum A { + Ok = u32::MAX - 1, + Ok2, + OhNo = 0_i32, + //~^ ERROR mismatched types + //~| expected `u32`, found `i32` + } + + let x = A::Ok; +} + +fn f_i64() { + #[repr(i64)] + enum A { + Ok = i64::MAX - 1, + Ok2, + OhNo = 0_u64, + //~^ ERROR mismatched types + //~| expected `i64`, found `u64` + } + + let x = A::Ok; +} + +fn f_u64() { + #[repr(u64)] + enum A { + Ok = u64::MAX - 1, + Ok2, + OhNo = 0_i64, + //~^ ERROR mismatched types + //~| expected `u64`, found `i64` + } + + let x = A::Ok; +} + +fn main() { } diff --git a/src/test/ui/discrim/discrim-ill-typed.stderr b/src/test/ui/discrim/discrim-ill-typed.stderr new file mode 100644 index 000000000..27f516341 --- /dev/null +++ b/src/test/ui/discrim/discrim-ill-typed.stderr @@ -0,0 +1,91 @@ +error[E0308]: mismatched types + --> $DIR/discrim-ill-typed.rs:15:16 + | +LL | OhNo = 0_u8, + | ^^^^ expected `i8`, found `u8` + | +help: change the type of the numeric literal from `u8` to `i8` + | +LL | OhNo = 0_i8, + | ~~ + +error[E0308]: mismatched types + --> $DIR/discrim-ill-typed.rs:28:16 + | +LL | OhNo = 0_i8, + | ^^^^ expected `u8`, found `i8` + | +help: change the type of the numeric literal from `i8` to `u8` + | +LL | OhNo = 0_u8, + | ~~ + +error[E0308]: mismatched types + --> $DIR/discrim-ill-typed.rs:41:16 + | +LL | OhNo = 0_u16, + | ^^^^^ expected `i16`, found `u16` + | +help: change the type of the numeric literal from `u16` to `i16` + | +LL | OhNo = 0_i16, + | ~~~ + +error[E0308]: mismatched types + --> $DIR/discrim-ill-typed.rs:54:16 + | +LL | OhNo = 0_i16, + | ^^^^^ expected `u16`, found `i16` + | +help: change the type of the numeric literal from `i16` to `u16` + | +LL | OhNo = 0_u16, + | ~~~ + +error[E0308]: mismatched types + --> $DIR/discrim-ill-typed.rs:67:16 + | +LL | OhNo = 0_u32, + | ^^^^^ expected `i32`, found `u32` + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | OhNo = 0_i32, + | ~~~ + +error[E0308]: mismatched types + --> $DIR/discrim-ill-typed.rs:80:16 + | +LL | OhNo = 0_i32, + | ^^^^^ expected `u32`, found `i32` + | +help: change the type of the numeric literal from `i32` to `u32` + | +LL | OhNo = 0_u32, + | ~~~ + +error[E0308]: mismatched types + --> $DIR/discrim-ill-typed.rs:93:16 + | +LL | OhNo = 0_u64, + | ^^^^^ expected `i64`, found `u64` + | +help: change the type of the numeric literal from `u64` to `i64` + | +LL | OhNo = 0_i64, + | ~~~ + +error[E0308]: mismatched types + --> $DIR/discrim-ill-typed.rs:106:16 + | +LL | OhNo = 0_i64, + | ^^^^^ expected `u64`, found `i64` + | +help: change the type of the numeric literal from `i64` to `u64` + | +LL | OhNo = 0_u64, + | ~~~ + +error: aborting due to 8 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/discrim/discrim-overflow-2.rs b/src/test/ui/discrim/discrim-overflow-2.rs new file mode 100644 index 000000000..ca24317c5 --- /dev/null +++ b/src/test/ui/discrim/discrim-overflow-2.rs @@ -0,0 +1,80 @@ +// Issue 23030: Detect overflowing discriminant +// +// Check that we detect the overflow even if enum is not used. + +// See also run-pass/discrim-explicit-23030.rs where the suggested +// workaround is tested. + +fn f_i8() { + #[repr(i8)] + enum A { + Ok = i8::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } +} + +fn f_u8() { + #[repr(u8)] + enum A { + Ok = u8::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } +} + +fn f_i16() { + #[repr(i16)] + enum A { + Ok = i16::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } +} + +fn f_u16() { + #[repr(u16)] + enum A { + Ok = u16::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } +} + +fn f_i32() { + #[repr(i32)] + enum A { + Ok = i32::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } +} + +fn f_u32() { + #[repr(u32)] + enum A { + Ok = u32::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } +} + +fn f_i64() { + #[repr(i64)] + enum A { + Ok = i64::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } +} + +fn f_u64() { + #[repr(u64)] + enum A { + Ok = u64::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } +} + +fn main() { } diff --git a/src/test/ui/discrim/discrim-overflow-2.stderr b/src/test/ui/discrim/discrim-overflow-2.stderr new file mode 100644 index 000000000..3ca84c667 --- /dev/null +++ b/src/test/ui/discrim/discrim-overflow-2.stderr @@ -0,0 +1,67 @@ +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow-2.rs:13:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 127 + | + = note: explicitly set `OhNo = -128` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow-2.rs:22:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 255 + | + = note: explicitly set `OhNo = 0` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow-2.rs:31:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 32767 + | + = note: explicitly set `OhNo = -32768` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow-2.rs:40:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 65535 + | + = note: explicitly set `OhNo = 0` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow-2.rs:49:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 2147483647 + | + = note: explicitly set `OhNo = -2147483648` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow-2.rs:58:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 4294967295 + | + = note: explicitly set `OhNo = 0` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow-2.rs:67:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 9223372036854775807 + | + = note: explicitly set `OhNo = -9223372036854775808` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow-2.rs:76:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 18446744073709551615 + | + = note: explicitly set `OhNo = 0` if that is desired outcome + +error: aborting due to 8 previous errors + +For more information about this error, try `rustc --explain E0370`. diff --git a/src/test/ui/discrim/discrim-overflow.rs b/src/test/ui/discrim/discrim-overflow.rs new file mode 100644 index 000000000..774ced93c --- /dev/null +++ b/src/test/ui/discrim/discrim-overflow.rs @@ -0,0 +1,99 @@ +// Issue 23030: Detect overflowing discriminant + +// See also run-pass/discrim-explicit-23030.rs where the suggested +// workaround is tested. + +fn f_i8() { + #[repr(i8)] + enum A { + Ok = i8::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } + + let x = A::Ok; +} + +fn f_u8() { + #[repr(u8)] + enum A { + Ok = u8::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } + + let x = A::Ok; +} + +fn f_i16() { + #[repr(i16)] + enum A { + Ok = i16::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + } + + let x = A::Ok; +} + +fn f_u16() { + #[repr(u16)] + enum A { + Ok = u16::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| overflowed on value after 65535 + } + + let x = A::Ok; +} + +fn f_i32() { + #[repr(i32)] + enum A { + Ok = i32::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| overflowed on value after 2147483647 + } + + let x = A::Ok; +} + +fn f_u32() { + #[repr(u32)] + enum A { + Ok = u32::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| overflowed on value after 4294967295 + } + + let x = A::Ok; +} + +fn f_i64() { + #[repr(i64)] + enum A { + Ok = i64::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| overflowed on value after 9223372036854775807 + } + + let x = A::Ok; +} + +fn f_u64() { + #[repr(u64)] + enum A { + Ok = u64::MAX - 1, + Ok2, + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| overflowed on value after 18446744073709551615 + } + + let x = A::Ok; +} + +fn main() { } diff --git a/src/test/ui/discrim/discrim-overflow.stderr b/src/test/ui/discrim/discrim-overflow.stderr new file mode 100644 index 000000000..1b331bb1b --- /dev/null +++ b/src/test/ui/discrim/discrim-overflow.stderr @@ -0,0 +1,67 @@ +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow.rs:11:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 127 + | + = note: explicitly set `OhNo = -128` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow.rs:22:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 255 + | + = note: explicitly set `OhNo = 0` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow.rs:33:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 32767 + | + = note: explicitly set `OhNo = -32768` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow.rs:44:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 65535 + | + = note: explicitly set `OhNo = 0` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow.rs:56:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 2147483647 + | + = note: explicitly set `OhNo = -2147483648` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow.rs:68:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 4294967295 + | + = note: explicitly set `OhNo = 0` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow.rs:80:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 9223372036854775807 + | + = note: explicitly set `OhNo = -9223372036854775808` if that is desired outcome + +error[E0370]: enum discriminant overflowed + --> $DIR/discrim-overflow.rs:92:9 + | +LL | OhNo, + | ^^^^ overflowed on value after 18446744073709551615 + | + = note: explicitly set `OhNo = 0` if that is desired outcome + +error: aborting due to 8 previous errors + +For more information about this error, try `rustc --explain E0370`. |