Skip to content

Clarify in breaking-change rules and R2R that converting a struct to an enum is a breaking change #137055

Closed

Description

@davidwrighton

In docs/coding-guidelines/breaking-change-rules.md, converting an enum to a struct is implicitly covered by:

✗ Disallowed

  • Removing one or more base classes for a type, including changing struct to class and vice versa

(since an enum derives from System.Enum : System.ValueType). However, the reverse conversion—changing a struct into an enum—is not listed under Disallowed, and on paper appears to fall under:

✓ Allowed

  • Introducing a new base class
    So long as it does not introduce any new abstract members or change the semantics or behavior of existing members, a type can be introduced into a hierarchy between two existing types.

Because of that ambiguity, IsNegativeCastResultVersionResilient in CorInfoImpl.ReadyToRun.cs (added in #135899) explicitly tried to accommodate a struct becoming an enum across version bubbles:

// A value type can gain interfaces (or become an enum), but its other cast targets are fixed.
if (fromType.IsValueType && !toType.IsInterface && !toType.IsWellKnownType(WellKnownType.Enum))
    return true;

The existing R2R compiler does not make struct $\rightarrow$ enum changes safe

In reality, the runtime, JIT, and ReadyToRun (R2R) compiler fundamentally assume throughout the codebase that a struct cannot become an enum (or vice versa) across a servicing / version-bubble boundary:

  1. asCorInfoType and ABI / calling conventions (CorInfoImpl.cs:1030-1071):
    • In asCorInfoType, an enum is unwrapped to its primitive underlying CorInfoType (CORINFO_TYPE_INT, CORINFO_TYPE_BYTE, etc.), whereas a struct is CORINFO_TYPE_VALUECLASS.
    • Method signatures (Get_CORINFO_SIG_INFO), argument types (getArgType), and field types (getFieldType) directly bake this distinction into R2R codegen and calling conventions without emitting a CheckTypeLayout fixup to guard against a struct becoming an enum.
  2. ICorJitInfo::isEnum (CorInfoImpl.cs:3313):
    • CorInfoImpl.isEnum unconditionally returns CorInfoIsEnumResult.Must or CorInfoIsEnumResult.MustNot from type.IsEnum without checking VersionsWithType(type).
    • Consequently, typeof(ExternalStruct).IsEnum (NI_System_Type_get_IsEnum) and typeof(ExternalStruct).IsPrimitive (NI_System_Type_get_IsPrimitive) fold MustNot directly into R2R code today.
  3. Comparer and generic sharing behavior (ComparerIntrinsics.cs):
    • EqualityComparer<T> and Comparer<T> select EnumEqualityComparer<T> / EnumComparer<T> vs struct comparers based on IsEnum, and generic canonicalization / array covariance rules treat enums and structs differently.

Proposal

Rather than trying to make the R2R compiler treat struct $\rightarrow$ enum as a versionable change:

  1. Update docs/coding-guidelines/breaking-change-rules.md (and any API compatibility tooling rules, if applicable) to explicitly state that changing a struct to an enum or vice versa is a disallowed breaking change.
  2. Simplify IsNegativeCastResultVersionResilient in CorInfoImpl.ReadyToRun.cs to remove the !toType.IsWellKnownType(WellKnownType.Enum) special case and its comment claiming a value type can become an enum.

Note

This issue was created with assistance from GitHub Copilot.

Activity

  1. added
    area-crossgen2-coreclr
    on Nov 6, 2026
  2. added
    untriaged
    on Nov 6, 2026
  3. jeffhandley commented on Nov 6, 2026

    @jeffhandley
    Member

    The doc change proposal is good with me. I defer to others assigned here for the implementation change decision.

  4. jkotas commented on Nov 6, 2026

    @jkotas
    Member

    Sounds good to me.

    I guess nobody thought about this theoretical case before.

  5. removed
    untriaged
    on Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-crossgen2-coreclr

Type

No type

Projects

  • Status
    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions