Skip to content

Qualify anonymous enum references with their backing class for exclude-using-statics-for-enums#733

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:fix-anonymous-enum-reference-qualification
Jul 13, 2026
Merged

Qualify anonymous enum references with their backing class for exclude-using-statics-for-enums#733
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:fix-anonymous-enum-reference-qualification

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Fixes #681.

With exclude-using-statics-for-enums, a cross-context enum constant reference is emitted as EnumName.Member. For an anonymous enum there is no generated enum type — the members are lowered to const fields on the enum's backing class (Methods here) — so qualifying with the synthesized __AnonymousEnum_* name referenced a type that doesn't exist:

public enum MyEnum2
{
    MyEnum2_Value1 = __AnonymousEnum_header_L1_C1.MyEnum1_Value1,  // doesn't compile
}

VisitDeclRefExpr's DontUseUsingStaticsForEnums branch wrote enumName unconditionally. The using-statics branch already special-cases anonymous enums via GetClass(enumName); this mirrors that, so the reference now qualifies with the backing class:

public enum MyEnum2
{
    MyEnum2_Value1 = Methods.MyEnum1_Value1,
}

Verified live:

  • anonymous enum + exclude-using-statics-for-enumsMethods.MyEnum1_Value1 (was __AnonymousEnum_*.)
  • default (using-statics) path unchanged → using static ...Methods; + unqualified MyEnum1_Value1
  • named cross-enum ref + exclude-using-statics-for-enums unchanged → MyEnum1.MyEnum1_Value1

Added AnonymousEnumReferenceExcludeUsingStaticsTest. Build 0 warning / 0 error; full suite green with no golden changes.

Note

This PR description and the code change were drafted by Copilot on my behalf.

…e-using-statics-for-enums

When exclude-using-statics-for-enums is set, a cross-context enum constant
reference is emitted as EnumName.Member. For an anonymous enum there is no
generated enum type -- the members are emitted as constants on the enum's
backing class (e.g. Methods) -- so qualifying with the __AnonymousEnum_* name
produced a reference to a type that doesn't exist and didn't compile. Qualify
with the backing class instead, mirroring the anonymous-enum handling on the
using-statics path.

Fixes dotnet#681

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding tannergooding merged commit c571327 into dotnet:main Jul 13, 2026
14 checks passed
@tannergooding tannergooding deleted the fix-anonymous-enum-reference-qualification branch July 13, 2026 06:12
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.

Reference to anonymous enum when using exclude-using-statics-for-enums

1 participant