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 @@ -20,6 +20,7 @@
* Enforce AttributeTargets on structs and classes ([PR #16790](https://github.com/dotnet/fsharp/pull/16790))
* 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))

### 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 @@ -6,4 +6,5 @@
### Fixed

* 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 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))
4 changes: 3 additions & 1 deletion src/Compiler/Checking/CheckDeclarations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2940,7 +2940,9 @@ module EstablishTypeDefinitionCores =

TFSharpTyconRepr (Construct.NewEmptyFSharpTyconData kind)

| SynTypeDefnSimpleRepr.Enum _ ->
| SynTypeDefnSimpleRepr.Enum _ ->
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargets) then
TcAttributesWithPossibleTargets false cenv envinner AttributeTargets.Enum synAttrs |> ignore
TFSharpTyconRepr (Construct.NewEmptyFSharpTyconData TFSharpEnum)

// OK, now fill in the (partially computed) type representation
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Core/prim-types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ namespace Microsoft.FSharp.Core
type RequiresExplicitTypeArgumentsAttribute() =
inherit Attribute()

[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Struct, AllowMultiple=false)>]
[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Struct ||| AttributeTargets.Enum, AllowMultiple=false)>]
[<Sealed>]
type RequireQualifiedAccessAttribute() =
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 @@ -898,7 +898,7 @@ namespace Microsoft.FSharp.Core
/// type require explicit qualified access.</summary>
///
/// <category>Attributes</category>
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Struct, AllowMultiple=false)>]
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Struct ||| AttributeTargets.Enum, AllowMultiple=false)>]
[<Sealed>]
type RequireQualifiedAccessAttribute =
inherit Attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
open System

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

[<CustomEnum>]
type Color =
| Red = 0
| Green = 1
| Blue = 2
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,40 @@ module CustomAttributes_AttributeUsage =
(Error 842, Line 11, Col 16, Line 11, Col 28, "This attribute is not valid for use on this language element")
(Error 842, Line 14, Col 15, Line 14, Col 27, "This attribute is not valid for use on this language element")
(Error 842, Line 17, Col 16, Line 17, Col 28, "This attribute is not valid for use on this language element")
]

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

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

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

// SOURCE=E_AttributeTargetIsEnum01.fs # E_AttributeTargetIsEnum01.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"E_AttributeTargetIsEnum01.fs"|])>]
let ``E_AttributeTargetIsEnum01_fs preview`` compilation =
compilation
|> withLangVersionPreview
|> verifyCompile
|> shouldFail
|> withDiagnostics [
(Error 842, Line 19, Col 3, Line 19, Col 15, "This attribute is not valid for use on this language element")
(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")
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
open System

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

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

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

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

[<CustomStruct>]
[<CustomClass>]
[<CustomInterface>]
[<CustomDelegate>]
type Color =
| Red = 0
| Green = 1
| Blue = 2