Skip to content

gccrs: add repr_c_enums_larger_than_int lint#4651

Open
lucasly-ba wants to merge 1 commit into
Rust-GCC:masterfrom
lucasly-ba:repr-c-enums-larger-than-int
Open

gccrs: add repr_c_enums_larger_than_int lint#4651
lucasly-ba wants to merge 1 commit into
Rust-GCC:masterfrom
lucasly-ba:repr-c-enums-larger-than-int

Conversation

@lucasly-ba

@lucasly-ba lucasly-ba commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Warn on a #[repr(C)] enum whose discriminant fits into neither a C int nor a C unsigned int. Such an enum is non-portable: C only permits enums whose discriminants do not all fit into one of those two types since C23, and Rust interprets repr(C) discriminants as isize, so the size may not match the C one.

@lucasly-ba lucasly-ba force-pushed the repr-c-enums-larger-than-int branch 2 times, most recently from e144c27 to d6cf7fd Compare June 28, 2026 21:10

@philberty philberty left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah i think we need to do const eval during type resolution of the enum disriminant variant

Comment thread gcc/rust/typecheck/rust-hir-type-check-item.cc Outdated
@lucasly-ba lucasly-ba force-pushed the repr-c-enums-larger-than-int branch 4 times, most recently from d646490 to fe2558c Compare July 9, 2026 13:45
Warn on a `#[repr(C)]` enum whose discriminant fits into neither a C
`int` nor a C `unsigned int`. Such an enum is non-portable: C only
permits enums whose discriminants do not all fit into one of those two
types since C23, and Rust interprets `repr(C)` discriminants as `isize`,
so its size may not match the C one.

The discriminant is const evaluated with query_compile_const_expr during
type resolution, so any constant discriminant expression is handled.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-item.cc
	(check_repr_c_enum_discriminants): New.
	(TypeCheckItem::visit): Check repr(C) enum discriminants.

gcc/testsuite/ChangeLog:

	* rust/compile/repr-c-enums-larger-than-int_0.rs: New test.

Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
@lucasly-ba lucasly-ba force-pushed the repr-c-enums-larger-than-int branch from fe2558c to 795d6e9 Compare July 9, 2026 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants