gccrs: add repr_c_enums_larger_than_int lint#4651
Open
lucasly-ba wants to merge 1 commit into
Open
Conversation
e144c27 to
d6cf7fd
Compare
philberty
requested changes
Jun 29, 2026
philberty
left a comment
Member
There was a problem hiding this comment.
Yeah i think we need to do const eval during type resolution of the enum disriminant variant
d646490 to
fe2558c
Compare
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>
fe2558c to
795d6e9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Warn on a
#[repr(C)]enum whose discriminant fits into neither a Cintnor a Cunsigned 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 interpretsrepr(C)discriminants asisize, so the size may not match the C one.