Skip to content

Preserve enum type in custom attribute argument of type obj (#995)#19975

Open
edgarfgp wants to merge 9 commits into
dotnet:mainfrom
edgarfgp:fix-995-enum-attr-obj
Open

Preserve enum type in custom attribute argument of type obj (#995)#19975
edgarfgp wants to merge 9 commits into
dotnet:mainfrom
edgarfgp:fix-995-enum-attr-obj

Conversation

@edgarfgp

@edgarfgp edgarfgp commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Problem

Assigning an enum to a custom attribute argument of type obj loses the enum type. The value is stored in metadata as the underlying int32, so reading the attribute back gives a plain int rather than the enum. C# stores the same thing as the enum, so this is also a cross-language inconsistency.

Fixes #995.

Before

open System

type MyAttribute() =
    inherit Attribute()
    let mutable prop : obj = null
    member _.Prop with get () : obj = prop and set (v: obj) = prop <- v

type MyEnum = A = 1 | B = 2

[<My(Prop = MyEnum.B)>]
type C = class end

let v = (typeof<C>.GetCustomAttributes(false)[0] :?> MyAttribute).Prop
v.GetType()                // System.Int32
Convert.ToString v         // "2"

After

v.GetType()                // MyEnum
Convert.ToString v         // "B"

The enum type is now written into the attribute blob using the ECMA-335 enum tag (0x55 + type name), the same encoding C# produces, so it round-trips as the enum. Reading that encoding back (which previously threw) is also handled, and decode/encode are kept symmetric so static linking preserves it.

Quotations

The original triage worried this needed quotation/TAST changes. It doesn't: the fix only reads the enum type (already on the Const node) when emitting the attribute. Quotations are untouched and already keep the enum type — added a test to confirm.

@github-actions

github-actions Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

❗ Release notes required

You can open this PR in browser to add release notes: open in github.dev


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/11.0.100.md

@edgarfgp edgarfgp force-pushed the fix-995-enum-attr-obj branch from 9649085 to 013cb43 Compare June 21, 2026 17:45
@github-actions github-actions Bot added the ⚠️ Affects-Compiler-Output Tooling check: PR touches IL emission or codegen label Jun 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔍 Tooling Safety Check — Affects-Compiler-Output
Affects-Compiler-Output: changes custom attribute blob encoding (enum 0x55 tag in il.fs/IlxGen.fs)

Generated by PR Tooling Safety Check · opus46 3.2M ·

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚠️ Affects-Compiler-Output Tooling check: PR touches IL emission or codegen

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

Enum type is lost when used in attribute

1 participant