Skip to content

Respect endianness correctly in CheckEnums test suite #143339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/tools/compiletest/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/compiletest/src/directive-list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/tools/compiletest/src/directives/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
16 changes: 16 additions & 0 deletions src/tools/compiletest/src/directives/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/mir/enum/convert_non_enum_break_big_endian.rs
Original file line number Diff line number Diff line change
@@ -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 }) };
}
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of splitting this test, consider using revisions and keeping the old name?

Copy link
Contributor

@lcnr lcnr Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

search for existing //@ revisions to see how it works

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or better, use cfg to make the code work under little-endian and big-endian.


#[allow(dead_code)]
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/mir/enum/convert_non_enum_ok_big_endian.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//@ run-fail
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test called ok that is "run-fail" doesn't make a lot of sense, does it?

//@ 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 }) };
}
Copy link
Member

@RalfJung RalfJung Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this test use a struct with two u16 fields to begin with? if you transmuted a u32 directly to Foo, things would be endian-independent. (same for convert_non_enum_break)

Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same in this test


#[allow(dead_code)]
#[repr(u32)]
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/mir/enum/niche_option_tuple_break_big_endian.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should this pass on big-endian? That makes no sense, it's still an invalid enum!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I think I see what happens... Foo becomes a 1-byte enum and 3 in big-endian leaves that byte 0, and only changes the padding.

The test should be made endian-independent by adding repr(u32) to Foo and by having Bar with two u32 fields:

#[allow(dead_code)]
#[repr(u32)]
enum Foo {
    A,
    B,
}

#[allow(dead_code)]
struct Bar {
    a: u32,
    b: u32,
}

fn main() {
    let _val: Option<(u32, Foo)> =
        unsafe { std::mem::transmute::<_, Option<(u32, Foo)>>(Bar { a: 3, b: 3 }) };
}
Original file line number Diff line number Diff line change
@@ -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 }) };
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is endian-dependent about this test?

Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this one


#[allow(dead_code)]
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/mir/enum/with_niche_int_break_big_endian.rs
Original file line number Diff line number Diff line change
@@ -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::<u32, Nested>(4) };
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace 4 by u32::MAX here, then the test fails both under little-endian and big-endian.

Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
Loading