-
Notifications
You must be signed in to change notification settings - Fork 865
Preserve enum type in custom attribute argument of type obj (#995) #19975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edgarfgp
wants to merge
14
commits into
dotnet:main
Choose a base branch
from
edgarfgp:fix-995-enum-attr-obj
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−25
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
013cb43
Add PR link to release note
edgarfgp a832f00
Merge branch 'main' into fix-995-enum-attr-obj
edgarfgp 7c298d0
Remove unrelated whitespace changes
edgarfgp fa33419
Fix formatting and add quotation regression test for enum preservation
edgarfgp 21727a7
Add cross-language test: C#-defined enum in an F# attribute arg of ty…
edgarfgp a311000
Update FCS surface area baseline for ILAttribElem.Enum
edgarfgp a8fa4a0
Merge branch 'main' into fix-995-enum-attr-obj
edgarfgp 3ee9599
Merge branch 'main' into fix-995-enum-attr-obj
edgarfgp b65b775
Merge branch 'main' into fix-995-enum-attr-obj
edgarfgp 0ca1338
Merge branch 'main' into fix-995-enum-attr-obj
edgarfgp de66dd8
Merge branch 'main' into fix-995-enum-attr-obj
edgarfgp 7c088ce
Only wrap genuine enums (0x55 tag) as ILAttribElem.Enum on decode
edgarfgp 2e80ce0
Fix formatting in decodeILAttribData enum-wrap branch
edgarfgp 0dcfa99
Merge branch 'main' into fix-995-enum-attr-obj
edgarfgp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ntTests/Conformance/BasicGrammarElements/CustomAttributes/Basic/EnumValueAsObjectArg01.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // #Conformance #DeclarationElements #Attributes | ||
| // Regression test for https://github.com/dotnet/fsharp/issues/995 | ||
| // An enum assigned to an attribute argument of type 'obj' must keep its enum type in the | ||
| // emitted metadata, instead of being stored as the underlying int32. | ||
|
|
||
| open System | ||
|
|
||
| type MyAttribute() = | ||
| inherit Attribute() | ||
| let mutable prop : obj = null | ||
| member _.Prop | ||
| with get () : obj = prop | ||
| and set (value: obj) = prop <- value | ||
|
|
||
| type MyEnum = | ||
| | A = 1 | ||
| | B = 2 | ||
|
|
||
| // An enum with a non-int32 underlying type, to exercise the encoded value width. | ||
| type LongEnum = | ||
| | P = 1L | ||
| | Q = 2L | ||
|
|
||
| [<My(Prop = MyEnum.B)>] | ||
| type MyClass = class end | ||
|
|
||
| [<My(Prop = LongEnum.Q)>] | ||
| type MyClassLong = class end | ||
|
|
||
| let propOf<'T> () = (typeof<'T>.GetCustomAttributes(false)[0] :?> MyAttribute).Prop | ||
|
|
||
| let intProp = propOf<MyClass> () | ||
| if intProp.GetType() <> typeof<MyEnum> then failwith "MyEnum type was lost" | ||
| if Convert.ToString(intProp, Globalization.CultureInfo.InvariantCulture) <> "B" then failwith "expected \"B\"" | ||
|
|
||
| let longProp = propOf<MyClassLong> () | ||
| if longProp.GetType() <> typeof<LongEnum> then failwith "LongEnum type was lost" | ||
| if Convert.ToString(longProp, Globalization.CultureInfo.InvariantCulture) <> "Q" then failwith "expected \"Q\"" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Encode/decode width is asymmetric for non-
int32boxed enums. The new encode path (GenAttribArg→ILAttribElem.Enum(enumIlTy, underlyingElem)→encodeCustomAttrPrimValue) emits the underlying value at its true width — 8 bytes for anint64enum like theLongEnumadded inEnumValueAsObjectArg01.fs. But when that blob is read back throughdecodeILAttribData, a boxed enum decodes toILType.Value <enumName>(via the0x55case) and lands in this fallback, which unconditionally readsint32(4 bytes).Consequences for any non-
int32boxed enum:sigptradvances 4 bytes instead of 8, desyncing the parse of any following named argument (silent blob corruption).The
LongEnumtest only exercises the CLR's metadata reader at runtime, so it doesn't cover this path. The comment here acknowledges the int32 assumption but understates the impact — the PR description claims decode/encode are "kept symmetric so static linking preserves it," which doesn't hold forint64/byte/int16/… enums. Consider reading the value at the enum's underlying width (resolving it from the0x55type) or, at minimum, not silently advancing by the wrong amount.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a pre-existing, documented assumption of decodeILAttribData