From 044934860d9efcd70cca34d90e0dc68e0457a811 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 18 Mar 2024 12:00:50 +0000 Subject: [PATCH 1/3] AttributeTargets.Class and AttributeTargets.Struct can be targeted in an enum incorrectly --- src/Compiler/Checking/CheckDeclarations.fs | 4 ++- .../AttributeTargetsIsEnum01.fs | 11 ++++++ .../AttributeUsage/AttributeUsage.fs | 36 +++++++++++++++++++ .../E_AttributeTargetIsEnum01.fs | 26 ++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeTargetsIsEnum01.fs create mode 100644 tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsEnum01.fs diff --git a/src/Compiler/Checking/CheckDeclarations.fs b/src/Compiler/Checking/CheckDeclarations.fs index 442a21e3601..501d7927ec2 100644 --- a/src/Compiler/Checking/CheckDeclarations.fs +++ b/src/Compiler/Checking/CheckDeclarations.fs @@ -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 diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeTargetsIsEnum01.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeTargetsIsEnum01.fs new file mode 100644 index 00000000000..292f19d070a --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeTargetsIsEnum01.fs @@ -0,0 +1,11 @@ +open System + +[] +type CustomEnumAttribute() = + inherit Attribute() + +[] +type Color = + | Red = 0 + | Green = 1 + | Blue = 2 diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs index d34f6d797c1..15851371cb5 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs @@ -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 + [] + let ``AttributeTargetsIsEnum01_fs`` compilation = + compilation + |> verifyCompile + |> shouldSucceed + + // SOURCE=AttributeTargetsIsEnum01.fs # AttributeTargetsIsEnum01.fs + [] + let ``AttributeTargetsIsEnum01_fs preview`` compilation = + compilation + |> withLangVersionPreview + |> verifyCompile + |> shouldSucceed + + // SOURCE=E_AttributeTargetIsEnum01.fs # E_AttributeTargetIsEnum01.fs + [] + let ``E_AttributeTargetIsEnum01_fs`` compilation = + compilation + |> verifyCompile + |> shouldSucceed + + // SOURCE=E_AttributeTargetIsEnum01.fs # 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") ] \ No newline at end of file diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsEnum01.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsEnum01.fs new file mode 100644 index 00000000000..5d8f0a81b3e --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsEnum01.fs @@ -0,0 +1,26 @@ +open System + +[] +type CustomStructAttribute() = + inherit Attribute() + +[] +type CustomClassAttribute() = + inherit Attribute() + +[] +type CustomInterfaceAttribute() = + inherit Attribute() + +[] +type CustomDelegateAttribute() = + inherit Attribute() + +[] +[] +[] +[] +type Color = + | Red = 0 + | Green = 1 + | Blue = 2 \ No newline at end of file From 0972c5f4bf1b4c492660b7783f8c5edbc60153e9 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 18 Mar 2024 12:06:53 +0000 Subject: [PATCH 2/3] Update release-notes --- docs/release-notes/.FSharp.Compiler.Service/8.0.300.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/release-notes/.FSharp.Compiler.Service/8.0.300.md b/docs/release-notes/.FSharp.Compiler.Service/8.0.300.md index 6a2c4414491..e68afa41dc8 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/8.0.300.md +++ b/docs/release-notes/.FSharp.Compiler.Service/8.0.300.md @@ -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 From ca2f2ce74c545f5d6c3c43a4252e87e4a83913b0 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 18 Mar 2024 12:43:37 +0000 Subject: [PATCH 3/3] update RequireQualifiedAccessAttribute to use AttributeTargets.Enum --- docs/release-notes/.FSharp.Core/8.0.300.md | 3 ++- src/FSharp.Core/prim-types.fs | 2 +- src/FSharp.Core/prim-types.fsi | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/release-notes/.FSharp.Core/8.0.300.md b/docs/release-notes/.FSharp.Core/8.0.300.md index 60b25705745..d4d7036b296 100644 --- a/docs/release-notes/.FSharp.Core/8.0.300.md +++ b/docs/release-notes/.FSharp.Core/8.0.300.md @@ -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)) \ No newline at end of file +* 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)) \ No newline at end of file diff --git a/src/FSharp.Core/prim-types.fs b/src/FSharp.Core/prim-types.fs index 777a31b8e15..2eca8c20333 100644 --- a/src/FSharp.Core/prim-types.fs +++ b/src/FSharp.Core/prim-types.fs @@ -350,7 +350,7 @@ namespace Microsoft.FSharp.Core type RequiresExplicitTypeArgumentsAttribute() = inherit Attribute() - [] + [] [] type RequireQualifiedAccessAttribute() = inherit Attribute() diff --git a/src/FSharp.Core/prim-types.fsi b/src/FSharp.Core/prim-types.fsi index fc2b4d756a9..3e88abdea32 100644 --- a/src/FSharp.Core/prim-types.fsi +++ b/src/FSharp.Core/prim-types.fsi @@ -898,7 +898,7 @@ namespace Microsoft.FSharp.Core /// type require explicit qualified access. /// /// Attributes - [] + [] [] type RequireQualifiedAccessAttribute = inherit Attribute