Skip to content

Commit 001a7dd

Browse files
authored
AttributeTargets.Class and AttributeTargets.Struct can be targeted in an enum incorrectly (#16887)
1 parent 307627b commit 001a7dd

File tree

8 files changed

+81
-4
lines changed

8 files changed

+81
-4
lines changed

docs/release-notes/.FSharp.Compiler.Service/8.0.300.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Enforce AttributeTargets on structs and classes ([PR #16790](https://github.com/dotnet/fsharp/pull/16790))
2121
* Parser: fix pattern range for idents with trivia ([PR #16824](https://github.com/dotnet/fsharp/pull/16824))
2222
* Fix broken code completion after a record type declaration ([PR #16813](https://github.com/dotnet/fsharp/pull/16813))
23+
* Enforce AttributeTargets on enums ([PR #16887](https://github.com/dotnet/fsharp/pull/16887))
2324

2425
### Added
2526

docs/release-notes/.FSharp.Core/8.0.300.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
### Fixed
77

88
* Preserve original stack traces in resumable state machines generated code if available. ([PR #16568](https://github.com/dotnet/fsharp/pull/16568))
9-
* Enforce AttributeTargets on structs and classes. Also update `RequireQualifiedAccessAttribute` and `AutoOpenAttribute` to use `AttributeTargets.Struct` ([PR #16790](https://github.com/dotnet/fsharp/pull/16790))
9+
* Enforce AttributeTargets on structs and classes. Also update `RequireQualifiedAccessAttribute` and `AutoOpenAttribute` to use `AttributeTargets.Struct` ([PR #16790](https://github.com/dotnet/fsharp/pull/16790))
10+
* Enforce AttributeTargets on enums. Also update `RequireQualifiedAccessAttribute` to use `AttributeTargets.Enum` ([PR #16887](https://github.com/dotnet/fsharp/pull/16887))

src/Compiler/Checking/CheckDeclarations.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2940,7 +2940,9 @@ module EstablishTypeDefinitionCores =
29402940

29412941
TFSharpTyconRepr (Construct.NewEmptyFSharpTyconData kind)
29422942

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

29462948
// OK, now fill in the (partially computed) type representation

src/FSharp.Core/prim-types.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ namespace Microsoft.FSharp.Core
350350
type RequiresExplicitTypeArgumentsAttribute() =
351351
inherit Attribute()
352352

353-
[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Struct, AllowMultiple=false)>]
353+
[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Struct ||| AttributeTargets.Enum, AllowMultiple=false)>]
354354
[<Sealed>]
355355
type RequireQualifiedAccessAttribute() =
356356
inherit Attribute()

src/FSharp.Core/prim-types.fsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ namespace Microsoft.FSharp.Core
898898
/// type require explicit qualified access.</summary>
899899
///
900900
/// <category>Attributes</category>
901-
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Struct, AllowMultiple=false)>]
901+
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Struct ||| AttributeTargets.Enum, AllowMultiple=false)>]
902902
[<Sealed>]
903903
type RequireQualifiedAccessAttribute =
904904
inherit Attribute
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
open System
2+
3+
[<AttributeUsage(AttributeTargets.Enum)>]
4+
type CustomEnumAttribute() =
5+
inherit Attribute()
6+
7+
[<CustomEnum>]
8+
type Color =
9+
| Red = 0
10+
| Green = 1
11+
| Blue = 2

tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,40 @@ module CustomAttributes_AttributeUsage =
519519
(Error 842, Line 11, Col 16, Line 11, Col 28, "This attribute is not valid for use on this language element")
520520
(Error 842, Line 14, Col 15, Line 14, Col 27, "This attribute is not valid for use on this language element")
521521
(Error 842, Line 17, Col 16, Line 17, Col 28, "This attribute is not valid for use on this language element")
522+
]
523+
524+
// SOURCE=AttributeTargetsIsEnum01.fs # AttributeTargetsIsEnum01.fs
525+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"AttributeTargetsIsEnum01.fs"|])>]
526+
let ``AttributeTargetsIsEnum01_fs`` compilation =
527+
compilation
528+
|> verifyCompile
529+
|> shouldSucceed
530+
531+
// SOURCE=AttributeTargetsIsEnum01.fs # AttributeTargetsIsEnum01.fs
532+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"AttributeTargetsIsEnum01.fs"|])>]
533+
let ``AttributeTargetsIsEnum01_fs preview`` compilation =
534+
compilation
535+
|> withLangVersionPreview
536+
|> verifyCompile
537+
|> shouldSucceed
538+
539+
// SOURCE=E_AttributeTargetIsEnum01.fs # E_AttributeTargetIsEnum01.fs
540+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"E_AttributeTargetIsEnum01.fs"|])>]
541+
let ``E_AttributeTargetIsEnum01_fs`` compilation =
542+
compilation
543+
|> verifyCompile
544+
|> shouldSucceed
545+
546+
// SOURCE=E_AttributeTargetIsEnum01.fs # E_AttributeTargetIsEnum01.fs
547+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"E_AttributeTargetIsEnum01.fs"|])>]
548+
let ``E_AttributeTargetIsEnum01_fs preview`` compilation =
549+
compilation
550+
|> withLangVersionPreview
551+
|> verifyCompile
552+
|> shouldFail
553+
|> withDiagnostics [
554+
(Error 842, Line 19, Col 3, Line 19, Col 15, "This attribute is not valid for use on this language element")
555+
(Error 842, Line 20, Col 3, Line 20, Col 14, "This attribute is not valid for use on this language element")
556+
(Error 842, Line 21, Col 3, Line 21, Col 18, "This attribute is not valid for use on this language element")
557+
(Error 842, Line 22, Col 3, Line 22, Col 17, "This attribute is not valid for use on this language element")
522558
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
open System
2+
3+
[<AttributeUsage(AttributeTargets.Struct)>]
4+
type CustomStructAttribute() =
5+
inherit Attribute()
6+
7+
[<AttributeUsage(AttributeTargets.Class)>]
8+
type CustomClassAttribute() =
9+
inherit Attribute()
10+
11+
[<AttributeUsage(AttributeTargets.Interface)>]
12+
type CustomInterfaceAttribute() =
13+
inherit Attribute()
14+
15+
[<AttributeUsage(AttributeTargets.Delegate)>]
16+
type CustomDelegateAttribute() =
17+
inherit Attribute()
18+
19+
[<CustomStruct>]
20+
[<CustomClass>]
21+
[<CustomInterface>]
22+
[<CustomDelegate>]
23+
type Color =
24+
| Red = 0
25+
| Green = 1
26+
| Blue = 2

0 commit comments

Comments
 (0)