From c9fb6dcc0386183463471827a92277882c5e6352 Mon Sep 17 00:00:00 2001 From: Bastian Kersting Date: Wed, 2 Jul 2025 20:29:22 +0300 Subject: [PATCH 1/2] Add test directive //@ ignore-endian-little We already have //@ ignore-endian-big, this does the same for little endian. --- src/tools/compiletest/src/common.rs | 4 ++++ src/tools/compiletest/src/directive-list.rs | 1 + src/tools/compiletest/src/directives/cfg.rs | 5 +++++ src/tools/compiletest/src/directives/tests.rs | 16 ++++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs index cdce5d941d015..5758e739fe3b7 100644 --- a/src/tools/compiletest/src/common.rs +++ b/src/tools/compiletest/src/common.rs @@ -459,6 +459,10 @@ impl Config { self.target_cfg().endian == Endian::Big } + pub fn is_little_endian(&self) -> bool { + self.target_cfg().endian == Endian::Little + } + pub fn get_pointer_width(&self) -> u32 { *&self.target_cfg().pointer_width } diff --git a/src/tools/compiletest/src/directive-list.rs b/src/tools/compiletest/src/directive-list.rs index adf2a7bffeff9..86aef5fa297f7 100644 --- a/src/tools/compiletest/src/directive-list.rs +++ b/src/tools/compiletest/src/directive-list.rs @@ -58,6 +58,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[ "ignore-elf", "ignore-emscripten", "ignore-endian-big", + "ignore-endian-little", "ignore-enzyme", "ignore-freebsd", "ignore-fuchsia", diff --git a/src/tools/compiletest/src/directives/cfg.rs b/src/tools/compiletest/src/directives/cfg.rs index 35f6a9e164486..c35964d888401 100644 --- a/src/tools/compiletest/src/directives/cfg.rs +++ b/src/tools/compiletest/src/directives/cfg.rs @@ -205,6 +205,11 @@ fn parse_cfg_name_directive<'a>( condition: config.is_big_endian(), message: "on big-endian targets", } + condition! { + name: "endian-little", + condition: config.is_little_endian(), + message: "on little-endian targets", + } condition! { name: format!("stage{}", config.stage).as_str(), allowed_names: &["stage0", "stage1", "stage2"], diff --git a/src/tools/compiletest/src/directives/tests.rs b/src/tools/compiletest/src/directives/tests.rs index d4570f8267781..f1faef412f6f5 100644 --- a/src/tools/compiletest/src/directives/tests.rs +++ b/src/tools/compiletest/src/directives/tests.rs @@ -689,6 +689,22 @@ fn is_big_endian() { } } +#[test] +fn is_little_endian() { + let endians = [ + ("x86_64-unknown-linux-gnu", true), + ("bpfeb-unknown-none", false), + ("m68k-unknown-linux-gnu", false), + ("aarch64_be-unknown-linux-gnu", false), + ("powerpc64-unknown-linux-gnu", false), + ]; + for (target, is_little) in endians { + let config = cfg().target(target).build(); + assert_eq!(config.is_little_endian(), is_little, "{target} {is_little}"); + assert_eq!(check_ignore(&config, "//@ ignore-endian-little"), is_little); + } +} + #[test] fn pointer_width() { let widths = [ From 4b95376295152049e4dadf2c15c50007fb35b1b4 Mon Sep 17 00:00:00 2001 From: Bastian Kersting Date: Wed, 2 Jul 2025 20:33:28 +0300 Subject: [PATCH 2/2] Respect endianness correctly in CheckEnums test suite The endianness can change the test expectation for the enum check. This change is correcting these expectations and adds separate tests for big endian targets with the correct expectations. --- .../enum/convert_non_enum_break_big_endian.rs | 22 ++++++++++++++++++ ...> convert_non_enum_break_little_endian.rs} | 3 +++ .../enum/convert_non_enum_ok_big_endian.rs | 23 +++++++++++++++++++ ...s => convert_non_enum_ok_little_endian.rs} | 3 +++ .../niche_option_tuple_break_big_endian.rs | 22 ++++++++++++++++++ ...niche_option_tuple_break_little_endian.rs} | 3 +++ .../enum/with_niche_int_break_big_endian.rs | 23 +++++++++++++++++++ ... => with_niche_int_break_little_endian.rs} | 3 +++ 8 files changed, 102 insertions(+) create mode 100644 tests/ui/mir/enum/convert_non_enum_break_big_endian.rs rename tests/ui/mir/enum/{convert_non_enum_break.rs => convert_non_enum_break_little_endian.rs} (76%) create mode 100644 tests/ui/mir/enum/convert_non_enum_ok_big_endian.rs rename tests/ui/mir/enum/{convert_non_enum_ok.rs => convert_non_enum_ok_little_endian.rs} (76%) create mode 100644 tests/ui/mir/enum/niche_option_tuple_break_big_endian.rs rename tests/ui/mir/enum/{niche_option_tuple_break.rs => niche_option_tuple_break_little_endian.rs} (77%) create mode 100644 tests/ui/mir/enum/with_niche_int_break_big_endian.rs rename tests/ui/mir/enum/{with_niche_int_break.rs => with_niche_int_break_little_endian.rs} (76%) diff --git a/tests/ui/mir/enum/convert_non_enum_break_big_endian.rs b/tests/ui/mir/enum/convert_non_enum_break_big_endian.rs new file mode 100644 index 0000000000000..8a92e0387a7e8 --- /dev/null +++ b/tests/ui/mir/enum/convert_non_enum_break_big_endian.rs @@ -0,0 +1,22 @@ +//@ run-pass +//@ compile-flags: -C debug-assertions +// This test depends on the endianess and has a different behavior on +// little endian. +//@ ignore-endian-little + +#[allow(dead_code)] +#[repr(u32)] +enum Foo { + A, + B, +} + +#[allow(dead_code)] +struct Bar { + a: u16, + b: u16, +} + +fn main() { + let _val: Foo = unsafe { std::mem::transmute::<_, Foo>(Bar { a: 0, b: 1 }) }; +} diff --git a/tests/ui/mir/enum/convert_non_enum_break.rs b/tests/ui/mir/enum/convert_non_enum_break_little_endian.rs similarity index 76% rename from tests/ui/mir/enum/convert_non_enum_break.rs rename to tests/ui/mir/enum/convert_non_enum_break_little_endian.rs index de062c39907a7..94bee80d7bca5 100644 --- a/tests/ui/mir/enum/convert_non_enum_break.rs +++ b/tests/ui/mir/enum/convert_non_enum_break_little_endian.rs @@ -1,5 +1,8 @@ //@ run-fail //@ compile-flags: -C debug-assertions +// This test depends on the endianess and has a different behavior on +// big endian. +//@ ignore-endian-big //@ error-pattern: trying to construct an enum from an invalid value 0x10000 #[allow(dead_code)] diff --git a/tests/ui/mir/enum/convert_non_enum_ok_big_endian.rs b/tests/ui/mir/enum/convert_non_enum_ok_big_endian.rs new file mode 100644 index 0000000000000..389d6f0a0604a --- /dev/null +++ b/tests/ui/mir/enum/convert_non_enum_ok_big_endian.rs @@ -0,0 +1,23 @@ +//@ run-fail +//@ compile-flags: -C debug-assertions +// This test depends on the endianess and has a different behavior on +// little endian. +//@ ignore-endian-little +//@ error-pattern: trying to construct an enum from an invalid value + +#[allow(dead_code)] +#[repr(u32)] +enum Foo { + A, + B, +} + +#[allow(dead_code)] +struct Bar { + a: u16, + b: u16, +} + +fn main() { + let _val: Foo = unsafe { std::mem::transmute::<_, Foo>(Bar { a: 1, b: 0 }) }; +} diff --git a/tests/ui/mir/enum/convert_non_enum_ok.rs b/tests/ui/mir/enum/convert_non_enum_ok_little_endian.rs similarity index 76% rename from tests/ui/mir/enum/convert_non_enum_ok.rs rename to tests/ui/mir/enum/convert_non_enum_ok_little_endian.rs index 37fc64342ca93..df5e4c3780e2b 100644 --- a/tests/ui/mir/enum/convert_non_enum_ok.rs +++ b/tests/ui/mir/enum/convert_non_enum_ok_little_endian.rs @@ -1,5 +1,8 @@ //@ run-pass //@ compile-flags: -C debug-assertions +// This test depends on the endianess and has a different behavior on +// big endian. +//@ ignore-endian-big #[allow(dead_code)] #[repr(u32)] diff --git a/tests/ui/mir/enum/niche_option_tuple_break_big_endian.rs b/tests/ui/mir/enum/niche_option_tuple_break_big_endian.rs new file mode 100644 index 0000000000000..1544084e36687 --- /dev/null +++ b/tests/ui/mir/enum/niche_option_tuple_break_big_endian.rs @@ -0,0 +1,22 @@ +//@ run-pass +//@ compile-flags: -C debug-assertions +// This test depends on the endianess and has a different behavior on +// big endian. +//@ ignore-endian-little + +#[allow(dead_code)] +enum Foo { + A, + B, +} + +#[allow(dead_code)] +struct Bar { + a: usize, + b: usize, +} + +fn main() { + let _val: Option<(usize, Foo)> = + unsafe { std::mem::transmute::<_, Option<(usize, Foo)>>(Bar { a: 3, b: 3 }) }; +} diff --git a/tests/ui/mir/enum/niche_option_tuple_break.rs b/tests/ui/mir/enum/niche_option_tuple_break_little_endian.rs similarity index 77% rename from tests/ui/mir/enum/niche_option_tuple_break.rs rename to tests/ui/mir/enum/niche_option_tuple_break_little_endian.rs index 43eef3a4cc5f0..125a25fc1bc16 100644 --- a/tests/ui/mir/enum/niche_option_tuple_break.rs +++ b/tests/ui/mir/enum/niche_option_tuple_break_little_endian.rs @@ -1,5 +1,8 @@ //@ run-fail //@ compile-flags: -C debug-assertions +// This test depends on the endianess and has a different behavior on +// big endian. +//@ ignore-endian-big //@ error-pattern: trying to construct an enum from an invalid value 0x3 #[allow(dead_code)] diff --git a/tests/ui/mir/enum/with_niche_int_break_big_endian.rs b/tests/ui/mir/enum/with_niche_int_break_big_endian.rs new file mode 100644 index 0000000000000..9deb118880a78 --- /dev/null +++ b/tests/ui/mir/enum/with_niche_int_break_big_endian.rs @@ -0,0 +1,23 @@ +//@ run-pass +//@ compile-flags: -C debug-assertions +// This test depends on the endianess and has a different behavior on +// little endian. +//@ ignore-endian-little + +#[allow(dead_code)] +#[repr(u16)] +enum Mix { + A, + B(u16), +} + +#[allow(dead_code)] +enum Nested { + C(Mix), + D, + E, +} + +fn main() { + let _val: Nested = unsafe { std::mem::transmute::(4) }; +} diff --git a/tests/ui/mir/enum/with_niche_int_break.rs b/tests/ui/mir/enum/with_niche_int_break_little_endian.rs similarity index 76% rename from tests/ui/mir/enum/with_niche_int_break.rs rename to tests/ui/mir/enum/with_niche_int_break_little_endian.rs index 0ec60a33564af..a466964757709 100644 --- a/tests/ui/mir/enum/with_niche_int_break.rs +++ b/tests/ui/mir/enum/with_niche_int_break_little_endian.rs @@ -1,6 +1,9 @@ //@ run-fail //@ compile-flags: -C debug-assertions //@ error-pattern: trying to construct an enum from an invalid value 0x4 +// This test depends on the endianess and has a different behavior on +// big endian. +//@ ignore-endian-big #[allow(dead_code)] #[repr(u16)]