Repository navigation
Description
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
structtoclassand 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:
asCorInfoTypeand ABI / calling conventions (CorInfoImpl.cs:1030-1071):- In
asCorInfoType, anenumis unwrapped to its primitive underlyingCorInfoType(CORINFO_TYPE_INT,CORINFO_TYPE_BYTE, etc.), whereas astructisCORINFO_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 aCheckTypeLayoutfixup to guard against astructbecoming anenum.
- In
ICorJitInfo::isEnum(CorInfoImpl.cs:3313):CorInfoImpl.isEnumunconditionally returnsCorInfoIsEnumResult.MustorCorInfoIsEnumResult.MustNotfromtype.IsEnumwithout checkingVersionsWithType(type).- Consequently,
typeof(ExternalStruct).IsEnum(NI_System_Type_get_IsEnum) andtypeof(ExternalStruct).IsPrimitive(NI_System_Type_get_IsPrimitive) foldMustNotdirectly into R2R code today.
- Comparer and generic sharing behavior (
ComparerIntrinsics.cs):EqualityComparer<T>andComparer<T>selectEnumEqualityComparer<T>/EnumComparer<T>vs struct comparers based onIsEnum, and generic canonicalization / array covariance rules treat enums and structs differently.
Proposal
Rather than trying to make the R2R compiler treat struct enum as a versionable change:
- Update
docs/coding-guidelines/breaking-change-rules.md(and any API compatibility tooling rules, if applicable) to explicitly state that changing astructto anenumor vice versa is a disallowed breaking change. - Simplify
IsNegativeCastResultVersionResilientinCorInfoImpl.ReadyToRun.csto 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.
Metadata
Metadata
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
jeffhandley commentedon Nov 6, 2026
The doc change proposal is good with me. I defer to others assigned here for the implementation change decision.
jkotas commentedon Nov 6, 2026
Sounds good to me.
I guess nobody thought about this theoretical case before.