Skip to content

Generate an empty enum that compiles in C and Rust [patch] - #195

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/190-empty-enum
Oct 9, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/190-empty-enum

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #190

What changed

  • C: CGenerator.GenerateEnumDeclaration always wrote typedef enum Name { … } Name;. ISO C does not allow an empty enumerator list, so gcc rejected the output for an enum with no members (empty enum is invalid). An enum with no members now becomes a typedef of its underlying type, or of int if none is set. A note explains that C has no empty enum, using the same WriteInexpressible style as the generator's other notes.
  • Rust: rustc rejects #[repr] on an enum with no variants (E0084). RustGenerator.GenerateEnumDeclaration now omits it when Members is empty.

The other five targets already handled an empty enum, so they are unchanged.

Tests

  • CGeneratorTests.Enum_WithNoMembers_IsATypedefOfItsType: Empty becomes typedef int Empty;, and Flags : long becomes typedef long long Flags;.
  • CGeneratedSourceCompilesTests: the exemplar header now includes both empty enums and is compiled with -std=c11.
  • RustGeneratorTests.Enum_WithNoVariants_HasNoRepr: checks that #[repr] is gone from an empty enum.
  • RustGeneratedSourceCompilesTests: the exemplar now includes an empty enum with UnderlyingType = int and is compiled with rustc.

With the generator changes reverted, all four tests fail. gcc reports empty enum is invalid and rustc reports E0084. With the changes, the full suite passes (1102/1102).

This is separate from #181, the empty-struct case in C. That case goes through a different code path and is not touched here.

🤖 Generated with Claude Code

https://claude.ai/code/session_01X8BPy6trRZiUxiwJcbUGzR


Generated by Claude Code

ISO C has no empty enumerator list, so typedef enum Empty { } Empty;
failed with "empty enum is invalid". An enum with no members is now a
typedef of its underlying type, or of int, with a note saying why.

rustc rejects #[repr] on a zero-variant enum (E0084), so the Rust
generator leaves it off when there are no members.

Fixes #190

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X8BPy6trRZiUxiwJcbUGzR
@sonarqubecloud

sonarqubecloud Bot commented Oct 9, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit afed877 into main Oct 9, 2026
14 checks passed
@matt-edmondson
matt-edmondson deleted the fix/190-empty-enum branch October 9, 2026 08:16
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.

An enum with no members generates C that fails to compile ("empty enum is invalid"), and Rust that fails with E0084 when it has an UnderlyingType

2 participants