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 5 times, most recently from fe2558c to 795d6e9 Compare July 9, 2026 22:46
@lucasly-ba

Copy link
Copy Markdown
Contributor Author

@P-E-P

Comment thread gcc/rust/typecheck/rust-hir-type-check-item.cc Outdated
return;

const int64_t c_int_min = -2147483648LL;
const int64_t c_uint_max = 4294967295LL;

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.

Likewise

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

should be good @P-E-P

@lucasly-ba
lucasly-ba force-pushed the repr-c-enums-larger-than-int branch 2 times, most recently from ef91dd8 to b891dae Compare July 21, 2026 00:17
#include "options.h"
#include "rust-compile-base.h"
#include "rust-compile-context.h"
#include <cstdint>

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.

You cannot include a system header like this, you need rust-system.h

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

should be good

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 b891dae to 0cb6ec5 Compare July 23, 2026 14:59
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.

3 participants