-
Notifications
You must be signed in to change notification settings - Fork 859
Handle Const.Zero for primitive value types in custom attribute codegen #19484
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
Copilot
wants to merge
2
commits into
main
Choose a base branch
from
copilot/fix-internal-error-custom-attribute
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
150 changes: 150 additions & 0 deletions
150
...entTests/Conformance/BasicGrammarElements/CustomAttributes/Basic/OptionalAttributeArgs.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,150 @@ | ||
| // #Regression #Conformance #DeclarationElements #Attributes | ||
| // Regression test for https://github.com/dotnet/fsharp/issues/8353 | ||
| // Verify that custom attributes with [<Optional>] parameters (no DefaultParameterValue) compile for all value types | ||
| // and that the emitted default values are correct at runtime. | ||
|
|
||
| open System | ||
| open System.Runtime.InteropServices | ||
|
|
||
| type BoolAttribute(name: string, flag: bool) = | ||
| inherit Attribute() | ||
| member _.Flag = flag | ||
| new([<Optional>] flag: bool) = BoolAttribute("", flag) | ||
|
|
||
| type IntAttribute(name: string, value: int) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: int) = IntAttribute("", value) | ||
|
|
||
| type ByteAttribute(name: string, value: byte) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: byte) = ByteAttribute("", value) | ||
|
|
||
| type SByteAttribute(name: string, value: sbyte) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: sbyte) = SByteAttribute("", value) | ||
|
|
||
| type Int16Attribute(name: string, value: int16) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: int16) = Int16Attribute("", value) | ||
|
|
||
| type Int64Attribute(name: string, value: int64) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: int64) = Int64Attribute("", value) | ||
|
|
||
| type UInt16Attribute(name: string, value: uint16) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: uint16) = UInt16Attribute("", value) | ||
|
|
||
| type UInt32Attribute(name: string, value: uint32) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: uint32) = UInt32Attribute("", value) | ||
|
|
||
| type UInt64Attribute(name: string, value: uint64) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: uint64) = UInt64Attribute("", value) | ||
|
|
||
| type FloatAttribute(name: string, value: float) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: float) = FloatAttribute("", value) | ||
|
|
||
| type SingleAttribute(name: string, value: float32) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: float32) = SingleAttribute("", value) | ||
|
|
||
| type CharAttribute(name: string, value: char) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: char) = CharAttribute("", value) | ||
|
|
||
| // Enum types with different underlying types | ||
| type MyIntEnum = A = 0 | B = 1 | ||
|
|
||
| type MyByteEnum = A = 0uy | B = 1uy | ||
|
|
||
| type EnumIntAttribute(name: string, value: MyIntEnum) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: MyIntEnum) = EnumIntAttribute("", value) | ||
|
|
||
| type EnumByteAttribute(name: string, value: MyByteEnum) = | ||
| inherit Attribute() | ||
| member _.Value = value | ||
| new([<Optional>] value: MyByteEnum) = EnumByteAttribute("", value) | ||
|
|
||
| [<Bool>] | ||
| type T1() = class end | ||
|
|
||
| [<Int>] | ||
| type T2() = class end | ||
|
|
||
| [<Byte>] | ||
| type T3() = class end | ||
|
|
||
| [<Float>] | ||
| type T4() = class end | ||
|
|
||
| [<Single>] | ||
| type T5() = class end | ||
|
|
||
| [<Char>] | ||
| type T6() = class end | ||
|
|
||
| [<SByte>] | ||
| type T7() = class end | ||
|
|
||
| [<Int16>] | ||
| type T8() = class end | ||
|
|
||
| [<Int64>] | ||
| type T9() = class end | ||
|
|
||
| [<UInt16>] | ||
| type T10() = class end | ||
|
|
||
| [<UInt32>] | ||
| type T11() = class end | ||
|
|
||
| [<UInt64>] | ||
| type T12() = class end | ||
|
|
||
| [<EnumInt>] | ||
| type T13() = class end | ||
|
|
||
| [<EnumByte>] | ||
| type T14() = class end | ||
|
|
||
| // Verify default values at runtime via reflection | ||
| let inline getAttr<'a when 'a :> Attribute> (t: Type) = t.GetCustomAttributes(typeof<'a>, false).[0] :?> 'a | ||
|
|
||
| let check (name: string) (actual: 'a) (expected: 'a) = | ||
| if actual <> expected then | ||
| failwithf "%s: expected %A but got %A" name expected actual | ||
|
|
||
| [<EntryPoint>] | ||
| let main _ = | ||
| check "bool" (getAttr<BoolAttribute>(typeof<T1>)).Flag false | ||
| check "int" (getAttr<IntAttribute>(typeof<T2>)).Value 0 | ||
| check "byte" (getAttr<ByteAttribute>(typeof<T3>)).Value 0uy | ||
| check "float" (getAttr<FloatAttribute>(typeof<T4>)).Value 0.0 | ||
| check "single" (getAttr<SingleAttribute>(typeof<T5>)).Value 0.0f | ||
| check "char" (getAttr<CharAttribute>(typeof<T6>)).Value '\000' | ||
| check "sbyte" (getAttr<SByteAttribute>(typeof<T7>)).Value 0y | ||
| check "int16" (getAttr<Int16Attribute>(typeof<T8>)).Value 0s | ||
| check "int64" (getAttr<Int64Attribute>(typeof<T9>)).Value 0L | ||
| check "uint16" (getAttr<UInt16Attribute>(typeof<T10>)).Value 0us | ||
| check "uint32" (getAttr<UInt32Attribute>(typeof<T11>)).Value 0u | ||
| check "uint64" (getAttr<UInt64Attribute>(typeof<T12>)).Value 0UL | ||
| check "enum_int" (getAttr<EnumIntAttribute>(typeof<T13>)).Value MyIntEnum.A | ||
| check "enum_byte" (getAttr<EnumByteAttribute>(typeof<T14>)).Value MyByteEnum.A | ||
| 0 | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.