Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/8.0.300.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Parser: fix pattern range for idents with trivia ([PR #16824](https://github.com/dotnet/fsharp/pull/16824))
* Fix broken code completion after a record type declaration ([PR #16813](https://github.com/dotnet/fsharp/pull/16813))
* Enforce AttributeTargets on enums ([PR #16887](https://github.com/dotnet/fsharp/pull/16887))
* Enforce AttributeTargets on delegates ([PR #16891](https://github.com/dotnet/fsharp/pull/16891))

### Added

Expand Down
3 changes: 2 additions & 1 deletion docs/release-notes/.FSharp.Core/8.0.300.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

* Preserve original stack traces in resumable state machines generated code if available. ([PR #16568](https://github.com/dotnet/fsharp/pull/16568))
* Enforce AttributeTargets on structs and classes. Also update `RequireQualifiedAccessAttribute` and `AutoOpenAttribute` to use `AttributeTargets.Struct` ([PR #16790](https://github.com/dotnet/fsharp/pull/16790))
* Enforce AttributeTargets on enums. Also update `RequireQualifiedAccessAttribute` to use `AttributeTargets.Enum` ([PR #16887](https://github.com/dotnet/fsharp/pull/16887))
* Enforce AttributeTargets on enums. Also update `RequireQualifiedAccessAttribute` to use `AttributeTargets.Enum` ([PR #16887](https://github.com/dotnet/fsharp/pull/16887))
* Enforce AttributeTargets on delegates. Also update `ReflectedDefinitionAttribute` to use `AttributeTargets.Delegate` ([PR #16891](https://github.com/dotnet/fsharp/pull/16891))
5 changes: 4 additions & 1 deletion src/Compiler/Checking/CheckDeclarations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2931,7 +2931,10 @@ module EstablishTypeDefinitionCores =
TcAttributesWithPossibleTargets false cenv envinner AttributeTargets.Class synAttrs |> ignore
TFSharpClass
| SynTypeDefnKind.Interface -> TFSharpInterface
| SynTypeDefnKind.Delegate _ -> TFSharpDelegate (MakeSlotSig("Invoke", g.unit_ty, [], [], [], None))
| SynTypeDefnKind.Delegate _ ->
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargets) then
TcAttributesWithPossibleTargets false cenv envinner AttributeTargets.Delegate synAttrs |> ignore
TFSharpDelegate (MakeSlotSig("Invoke", g.unit_ty, [], [], [], None))
| SynTypeDefnKind.Struct ->
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargets) then
TcAttributesWithPossibleTargets false cenv envinner AttributeTargets.Struct synAttrs |> ignore
Expand Down
3 changes: 2 additions & 1 deletion src/FSharp.Core/prim-types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ namespace Microsoft.FSharp.Core

[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Struct |||
AttributeTargets.Parameter ||| AttributeTargets.Method |||
AttributeTargets.Property ||| AttributeTargets.Constructor, AllowMultiple=false)>]
AttributeTargets.Property ||| AttributeTargets.Constructor |||
AttributeTargets.Delegate, AllowMultiple=false)>]
[<Sealed>]
type ReflectedDefinitionAttribute(includeValue: bool) =
inherit Attribute()
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Core/prim-types.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace Microsoft.FSharp.Core
/// for use at runtime.</summary>
///
/// <category>Attributes</category>
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Struct ||| AttributeTargets.Parameter ||| AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.Constructor,AllowMultiple=false)>]
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Struct ||| AttributeTargets.Parameter ||| AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.Constructor ||| AttributeTargets.Delegate, AllowMultiple=false)>]
[<Sealed>]
type ReflectedDefinitionAttribute =
inherit Attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
open System

[<AttributeUsage(AttributeTargets.Delegate)>]
type CustomDelegateAttribute() =
inherit Attribute()

[<CustomDelegate>]
type Delegate1 = delegate of int -> int
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,40 @@ module CustomAttributes_AttributeUsage =
(Error 842, Line 20, Col 3, Line 20, Col 14, "This attribute is not valid for use on this language element")
(Error 842, Line 21, Col 3, Line 21, Col 18, "This attribute is not valid for use on this language element")
(Error 842, Line 22, Col 3, Line 22, Col 17, "This attribute is not valid for use on this language element")
]

// SOURCE=AttributeTargetsIsDelegate01.fs # AttributeTargetsIsDelegate01.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"AttributeTargetsIsDelegate01.fs"|])>]
let ``AttributeTargetsIsDelegate01_fs`` compilation =
compilation
|> verifyCompile
|> shouldSucceed

// SOURCE=AttributeTargetsIsDelegate01.fs # AttributeTargetsIsDelegate01.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"AttributeTargetsIsDelegate01.fs"|])>]
let ``AttributeTargetsIsDelegate01_fs preview`` compilation =
compilation
|> withLangVersionPreview
|> verifyCompile
|> shouldSucceed

// SOURCE=E_AttributeTargetIsDelegate01.fs # E_AttributeTargetIsDelegate01.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"E_AttributeTargetIsDelegate01.fs"|])>]
let ``E_AttributeTargetIsDelegate01_fs`` compilation =
compilation
|> verifyCompile
|> shouldSucceed

// SOURCE=E_AttributeTargetIsDelegate01.fs # E_AttributeTargetIsDelegate01.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"E_AttributeTargetIsDelegate01.fs"|])>]
let ``E_AttributeTargetsIsDelegate01_fs preview`` compilation =
compilation
|> withLangVersionPreview
|> verifyCompile
|> shouldFail
|> withDiagnostics [
(Error 842, Line 19, Col 3, Line 19, Col 14, "This attribute is not valid for use on this language element")
(Error 842, Line 20, Col 3, Line 20, Col 15, "This attribute is not valid for use on this language element")
(Error 842, Line 21, Col 3, Line 21, Col 18, "This attribute is not valid for use on this language element")
(Error 842, Line 22, Col 3, Line 22, Col 13, "This attribute is not valid for use on this language element")
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
open System

[<AttributeUsage(AttributeTargets.Class)>]
type CustomClassAttribute() =
inherit Attribute()

[<AttributeUsage(AttributeTargets.Struct)>]
type CustomStructAttribute() =
inherit Attribute()

[<AttributeUsage(AttributeTargets.Interface)>]
type CustomInterfaceAttribute() =
inherit Attribute()

[<AttributeUsage(AttributeTargets.Enum)>]
type CustomEnumAttribute() =
inherit Attribute()

[<CustomClass>]
[<CustomStruct>]
[<CustomInterface>]
[<CustomEnum>]
type Delegate1 = delegate of int -> int