Skip to content

fix: Allow sized C-like enums in Rust - #905

Open
NEOatNHNG wants to merge 1 commit into
mainfrom
NEOatNHNG/rust-allow-sized-enums
Open

fix: Allow sized C-like enums in Rust#905
NEOatNHNG wants to merge 1 commit into
mainfrom
NEOatNHNG/rust-allow-sized-enums

Conversation

@NEOatNHNG

Copy link
Copy Markdown
Contributor

The equivalent of enum class: size_type on C++

The equivalent of enum class on C++
Comment thread score/mw/com/rust/score_com_macros/lib.rs

/// Check for #[repr(C)] existence
/// Check whether the type is representable as C struct or enum.
fn has_repr_c(attrs: &[syn::Attribute]) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn has_repr_c(attrs: &[syn::Attribute]) -> bool {
fn has_repr_c_and_primitive(attrs: &[syn::Attribute]) -> bool {

Comment on lines +331 to +336
if meta.input.peek(syn::token::Paren) {
let content;
syn::parenthesized!(content in meta.input);
let _ = content;
}
Ok(())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think padding should be handled by ABI, possibly through some kind of code generation tool. If we allow padding to be managed on the Rust side, but other applications or components do not apply the exact same rules, it could lead to compatibility issues.

I would recommend making #[repr(packed)] and #[repr(packed(N))] a compilation error when user put this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have same opinion about align as well

@NEOatNHNG NEOatNHNG Aug 12, 2026

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.

OK, I can also only allow repr(C) and repr(inttype) and forbid everything else if that is what we want. That effectively blocks a user from using align and packed on the data structure if using Rust while still being allowed on the C++ side. And this did work in the code before this PR so it would remove a "feature".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My concern is that alignment and padding should be managed from a single source of truth for both C++ and Rust. If you look at the Rust examples, we write the corresponding C++ structure as well for FFI layer translation. If a user applies packing or custom alignment only on the Rust side and forgets to apply the same settings in C++, it can easily result in layout mismatches and misalignment issues.

So, if we want to support this feature, we need to ensure that both the Rust and C++ definitions are generated or configured with identical alignment requirements.

I'm not against adding the feature, but we need a mechanism to guarantee consistency across both sides. A Rust macro can validate attributes present on the Rust type, but currently we do not have a way to verify that the equivalent C++ structure has been defined with the same alignment characteristics. Without such validation, we risk introducing subtle interoperability bugs.

bharatGoswami8 pushed a commit to bharatGoswami8/communication that referenced this pull request Aug 12, 2026
Partial fix for CodeQL/MISRA rule cpp/misra/signed-integer-overflow
(RULE-4-1-3), finding at score/message_passing/client_connection.cpp:383
(alert eclipse-score#905, 'Operation / of type long may overflow or underflow').

Signed integer division only invokes undefined behavior when the divisor
is -1 and the dividend equals the type's minimum value. kConnectRetryT is
a namespace-scope constexpr positive constant (3), so it can never be 0
or -1 and this division can never overflow. Add a static_assert
establishing that invariant at the point of use so the guarantee is
visible and enforced at compile time.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

2 participants