From 54b00ab77725a457d39573c32a3f3e5b4647325f Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 26 Feb 2024 16:21:41 +0000 Subject: [PATCH 1/8] Enforce union-cases AttributeTargets --- src/Compiler/Checking/CheckDeclarations.fs | 9 +++- src/Compiler/Checking/CheckExpressions.fs | 22 +++++--- .../AttributeTargetsIsMethod01.fs | 7 +++ .../AttributeUsage/AttributeUsage.fs | 52 ++++++++++++++++++- .../E_AttributeTargetIsField03.fs | 16 ++++++ .../E_AttributeTargetIsProperty01.fs | 15 ++++++ 6 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField03.fs create mode 100644 tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsProperty01.fs diff --git a/src/Compiler/Checking/CheckDeclarations.fs b/src/Compiler/Checking/CheckDeclarations.fs index fc2e5e9d19b..ee2c456624f 100644 --- a/src/Compiler/Checking/CheckDeclarations.fs +++ b/src/Compiler/Checking/CheckDeclarations.fs @@ -511,7 +511,14 @@ module TcRecdUnionAndEnumDeclarations = let TcUnionCaseDecl (cenv: cenv) env parent thisTy thisTyInst tpenv hasRQAAttribute (SynUnionCase(Attributes synAttrs, SynIdent(id, _), args, xmldoc, vis, m, _)) = let g = cenv.g - let attrs = TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs // the attributes of a union case decl get attached to the generated "static factory" method + let attrs = + // The attributes of a union case decl get attached to the generated "static factory" method + // Enforce that the union-cases can only be targeted by attributes with AttributeTargets.Method + if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsOnFunctions) then + TcAttributes cenv env AttributeTargets.Method synAttrs + else + TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs + let vis, _ = ComputeAccessAndCompPath env None m vis None parent let vis = CombineReprAccess parent vis diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs index 3e3e90a72fe..f043ed1894d 100644 --- a/src/Compiler/Checking/CheckExpressions.fs +++ b/src/Compiler/Checking/CheckExpressions.fs @@ -10874,17 +10874,23 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt errorR(Error(FSComp.SR.tcLiteralCannotHaveGenericParameters(), mBinding)) if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsOnFunctions) && memberFlagsOpt.IsNone && not attrs.IsEmpty then - let rhsIsFunction = isFunTy g overallPatTy - let lhsIsFunction = isFunTy g overallExprTy - let attrTgt = - match rhsIsFunction, lhsIsFunction with - | false, false when declaredTypars.IsEmpty -> AttributeTargets.Field ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue - | _, _ -> AttributeTargets.Method ||| AttributeTargets.ReturnValue - - TcAttributesWithPossibleTargets false cenv env attrTgt attrs |> ignore + TcAttributeTargetsOnLetBindings cenv env attrs overallPatTy overallExprTy declaredTypars.IsEmpty CheckedBindingInfo(inlineFlag, valAttribs, xmlDoc, tcPatPhase2, explicitTyparInfo, nameToPrelimValSchemeMap, rhsExprChecked, argAndRetAttribs, overallPatTy, mBinding, debugPoint, isCompGen, literalValue, isFixed), tpenv +// Note: +// - Let bound values can only have attributes that uses AttributeTargets.Field ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue +// - Let function bindings can only have attributes that uses AttributeTargets.Method ||| AttributeTargets.ReturnValue +and TcAttributeTargetsOnLetBindings (cenv: cenv) env attrs overallPatTy overallExprTy hasDeclTypars = + let rhsIsFunction = isFunTy cenv.g overallPatTy + let lhsIsFunction = isFunTy cenv.g overallExprTy + let attrTgt = + match rhsIsFunction, lhsIsFunction with + | false, false when hasDeclTypars -> AttributeTargets.Field ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue + | _, _ -> AttributeTargets.Method ||| AttributeTargets.ReturnValue + + TcAttributes cenv env attrTgt attrs |> ignore + and TcLiteral (cenv: cenv) overallTy env tpenv (attrs, synLiteralValExpr) = let g = cenv.g diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeTargetsIsMethod01.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeTargetsIsMethod01.fs index b8de3b3349a..4018ae74be8 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeTargetsIsMethod01.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeTargetsIsMethod01.fs @@ -48,3 +48,10 @@ let test1 = |> (=) 6 if not test1 then failwith "Failed: 1" + +[] +type MethodLevelAttribute() = + inherit Attribute() + +type SomeUnion = +| [] Case1 of int 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 c7861ee3e56..16ec35160fd 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs @@ -64,6 +64,14 @@ module CustomAttributes_AttributeUsage = |> verifyCompileAndRun |> shouldSucceed + // SOURCE=AttributeTargetsIsMethod01.fs # AttributeTargetsIsMethod01.fs + [] + let ``AttributeTargetsIsMethod01_fs preview`` compilation = + compilation + |> withLangVersionPreview + |> verifyCompileAndRun + |> shouldSucceed + // SOURCE=ConditionalAttribute.fs # ConditionalAttribute.fs [] let ``ConditionalAttribute_fs`` compilation = @@ -352,5 +360,45 @@ module CustomAttributes_AttributeUsage = |> withDiagnostics [ (Warning 2003, Line 5, Col 59, Line 5, Col 68, "The attribute System.Reflection.AssemblyFileVersionAttribute specified version '9.8.*.6', but this value is invalid and has been ignored") ] - - + + // SOURCE=E_AttributeTargetIsField03.fs # E_AttributeTargetIsField03.fs + [] + let ``E_AttributeTargetIsField03_fs`` compilation = + compilation + |> verifyCompile + |> shouldFail + |> withDiagnostics [ + (Error 842, Line 14, Col 5, Line 14, Col 15, "This attribute is not valid for use on this language element") + ] + + // SOURCE=E_AttributeTargetIsField03.fs # E_AttributeTargetIsField03.fs + [] + let ``E_AttributeTargetIsField03_fs preview`` compilation = + compilation + |> withLangVersionPreview + |> verifyCompile + |> shouldFail + |> withDiagnostics [ + (Error 842, Line 14, Col 5, Line 14, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 15, Col 5, Line 15, Col 25, "This attribute is not valid for use on this language element") + ] + + + // SOURCE=E_AttributeTargetIsProperty01.fs # E_AttributeTargetIsField03.fs + [] + let ``E_AttributeTargetIsProperty01_fs`` compilation = + compilation + |> verifyCompile + |> shouldSucceed + + // SOURCE=E_AttributeTargetIsProperty01.fs # E_AttributeTargetIsField03.fs + [] + let ``E_AttributeTargetIsProperty01_fs preview`` compilation = + compilation + |> withLangVersionPreview + |> verifyCompile + |> shouldFail + |> withDiagnostics [ + (Error 842, Line 14, Col 5, Line 14, Col 18, "This attribute is not valid for use on this language element") + (Error 842, Line 15, Col 5, Line 15, Col 25, "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_AttributeTargetIsField03.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField03.fs new file mode 100644 index 00000000000..cc5fb20b495 --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField03.fs @@ -0,0 +1,16 @@ +// This tests that AttributeTargets.Field is not allowed in union-case declaration + +open System + +[] +type FieldLevelAttribute() = + inherit Attribute() + +[] +type PropertyOrFieldLevelAttribute() = + inherit Attribute() + +type SomeUnion = +| [] Case1 of int // Should fail +| [] Case2 of int // Should fail + diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsProperty01.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsProperty01.fs new file mode 100644 index 00000000000..4627c3680dd --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsProperty01.fs @@ -0,0 +1,15 @@ +// This tests that AttributeTargets.Property is not allowed in union-case declaration + +open System + +[] +type PropertyLevelAttribute() = + inherit Attribute() + +[] +type PropertyOrFieldLevelAttribute() = + inherit Attribute() + +type SomeUnion = +| [] Case1 of int // Should fail +| [] Case2 of int // Should fail \ No newline at end of file From fbcedc42ec2741f6b6470a9d1325d4a8a2aa7f2d Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 26 Feb 2024 20:39:38 +0000 Subject: [PATCH 2/8] 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 786a741dbfa..c3c02695b3e 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/8.0.300.md +++ b/docs/release-notes/.FSharp.Compiler.Service/8.0.300.md @@ -12,6 +12,7 @@ * Fix discriminated union initialization. ([#PR 16661](https://github.com/dotnet/fsharp/pull/16661)) * Allow calling method with both Optional and ParamArray. ([#PR 16688](https://github.com/dotnet/fsharp/pull/16688), [suggestions #1120](https://github.com/fsharp/fslang-suggestions/issues/1120)) * Enforce AttributeTargets on let values and functions. ([PR #16692](https://github.com/dotnet/fsharp/pull/16692)) +* Enforce AttributeTargets on union case declarations. ([PR #16764](https://github.com/dotnet/fsharp/pull/16764)) ### Added From e3d7c8fa3b72cdf9dc1f97c7f531a3551586be7f Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 26 Feb 2024 20:41:12 +0000 Subject: [PATCH 3/8] LanguageFeature.EnforceAttributeTargetsUnionCaseDeclarations --- src/Compiler/Checking/CheckDeclarations.fs | 2 +- src/Compiler/FSComp.txt | 1 + src/Compiler/Facilities/LanguageFeatures.fs | 4 ++++ src/Compiler/Facilities/LanguageFeatures.fsi | 1 + src/Compiler/xlf/FSComp.txt.cs.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.de.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.es.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.fr.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.it.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.ja.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.ko.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.pl.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.pt-BR.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.ru.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.tr.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.zh-Hans.xlf | 5 +++++ src/Compiler/xlf/FSComp.txt.zh-Hant.xlf | 5 +++++ 17 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/Compiler/Checking/CheckDeclarations.fs b/src/Compiler/Checking/CheckDeclarations.fs index ee2c456624f..f607e5539f4 100644 --- a/src/Compiler/Checking/CheckDeclarations.fs +++ b/src/Compiler/Checking/CheckDeclarations.fs @@ -514,7 +514,7 @@ module TcRecdUnionAndEnumDeclarations = let attrs = // The attributes of a union case decl get attached to the generated "static factory" method // Enforce that the union-cases can only be targeted by attributes with AttributeTargets.Method - if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsOnFunctions) then + if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsUnionCaseDeclarations) then TcAttributes cenv env AttributeTargets.Method synAttrs else TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs diff --git a/src/Compiler/FSComp.txt b/src/Compiler/FSComp.txt index a1086629fbe..636ad2caff0 100644 --- a/src/Compiler/FSComp.txt +++ b/src/Compiler/FSComp.txt @@ -1595,6 +1595,7 @@ featureChkTailCallAttrOnNonRec,"Raises warnings if the 'TailCall' attribute is u featureUnionIsPropertiesVisible,"Union case test properties" featureBooleanReturningAndReturnTypeDirectedPartialActivePattern,"Boolean-returning and return-type-directed partial active patterns" featureEnforceAttributeTargetsOnFunctions,"Enforce AttributeTargets on functions" +featureEnforceAttributeTargetsUnionCaseDeclarations,"Enforce AttributeTargets on union case declarations" featureLowerInterpolatedStringToConcat,"Optimizes interpolated strings in certain cases, by lowering to concatenation" 3354,tcNotAFunctionButIndexerNamedIndexingNotYetEnabled,"This value supports indexing, e.g. '%s.[index]'. The syntax '%s[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation." 3354,tcNotAFunctionButIndexerIndexingNotYetEnabled,"This expression supports indexing, e.g. 'expr.[index]'. The syntax 'expr[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation." diff --git a/src/Compiler/Facilities/LanguageFeatures.fs b/src/Compiler/Facilities/LanguageFeatures.fs index 6e2d91bc6cd..dab5b70bc57 100644 --- a/src/Compiler/Facilities/LanguageFeatures.fs +++ b/src/Compiler/Facilities/LanguageFeatures.fs @@ -86,6 +86,7 @@ type LanguageFeature = | WarningWhenTailCallAttrOnNonRec | BooleanReturningAndReturnTypeDirectedPartialActivePattern | EnforceAttributeTargetsOnFunctions + | EnforceAttributeTargetsUnionCaseDeclarations | LowerInterpolatedStringToConcat /// LanguageVersion management @@ -200,6 +201,7 @@ type LanguageVersion(versionText) = LanguageFeature.UnionIsPropertiesVisible, previewVersion LanguageFeature.BooleanReturningAndReturnTypeDirectedPartialActivePattern, previewVersion LanguageFeature.EnforceAttributeTargetsOnFunctions, previewVersion + LanguageFeature.EnforceAttributeTargetsUnionCaseDeclarations, previewVersion LanguageFeature.LowerInterpolatedStringToConcat, previewVersion ] @@ -345,6 +347,8 @@ type LanguageVersion(versionText) = | LanguageFeature.BooleanReturningAndReturnTypeDirectedPartialActivePattern -> FSComp.SR.featureBooleanReturningAndReturnTypeDirectedPartialActivePattern () | LanguageFeature.EnforceAttributeTargetsOnFunctions -> FSComp.SR.featureEnforceAttributeTargetsOnFunctions () + | LanguageFeature.EnforceAttributeTargetsUnionCaseDeclarations -> + FSComp.SR.featureEnforceAttributeTargetsUnionCaseDeclarations () | LanguageFeature.LowerInterpolatedStringToConcat -> FSComp.SR.featureLowerInterpolatedStringToConcat () /// Get a version string associated with the given feature. diff --git a/src/Compiler/Facilities/LanguageFeatures.fsi b/src/Compiler/Facilities/LanguageFeatures.fsi index 4888479d867..5be15a88284 100644 --- a/src/Compiler/Facilities/LanguageFeatures.fsi +++ b/src/Compiler/Facilities/LanguageFeatures.fsi @@ -77,6 +77,7 @@ type LanguageFeature = | WarningWhenTailCallAttrOnNonRec | BooleanReturningAndReturnTypeDirectedPartialActivePattern | EnforceAttributeTargetsOnFunctions + | EnforceAttributeTargetsUnionCaseDeclarations | LowerInterpolatedStringToConcat /// LanguageVersion management diff --git a/src/Compiler/xlf/FSComp.txt.cs.xlf b/src/Compiler/xlf/FSComp.txt.cs.xlf index d4142d783fa..a5455db516b 100644 --- a/src/Compiler/xlf/FSComp.txt.cs.xlf +++ b/src/Compiler/xlf/FSComp.txt.cs.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides Vyvolá chyby pro přepsání jiných než virtuálních členů diff --git a/src/Compiler/xlf/FSComp.txt.de.xlf b/src/Compiler/xlf/FSComp.txt.de.xlf index fa0904ef50f..8eb4cfc5977 100644 --- a/src/Compiler/xlf/FSComp.txt.de.xlf +++ b/src/Compiler/xlf/FSComp.txt.de.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides Löst Fehler für Außerkraftsetzungen nicht virtueller Member aus. diff --git a/src/Compiler/xlf/FSComp.txt.es.xlf b/src/Compiler/xlf/FSComp.txt.es.xlf index 2d5ada63574..4b2213f165f 100644 --- a/src/Compiler/xlf/FSComp.txt.es.xlf +++ b/src/Compiler/xlf/FSComp.txt.es.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides Genera errores para invalidaciones de miembros no virtuales diff --git a/src/Compiler/xlf/FSComp.txt.fr.xlf b/src/Compiler/xlf/FSComp.txt.fr.xlf index da3c6f4b9b6..74a19dde824 100644 --- a/src/Compiler/xlf/FSComp.txt.fr.xlf +++ b/src/Compiler/xlf/FSComp.txt.fr.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides Déclenche des erreurs pour les remplacements de membres non virtuels diff --git a/src/Compiler/xlf/FSComp.txt.it.xlf b/src/Compiler/xlf/FSComp.txt.it.xlf index 010e34a81a2..4ee713505a7 100644 --- a/src/Compiler/xlf/FSComp.txt.it.xlf +++ b/src/Compiler/xlf/FSComp.txt.it.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides Genera errori per gli override dei membri non virtuali diff --git a/src/Compiler/xlf/FSComp.txt.ja.xlf b/src/Compiler/xlf/FSComp.txt.ja.xlf index 8a92ac2ea9e..18fc0e9b7ac 100644 --- a/src/Compiler/xlf/FSComp.txt.ja.xlf +++ b/src/Compiler/xlf/FSComp.txt.ja.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides 仮想メンバー以外のオーバーライドに対してエラーを発生させます diff --git a/src/Compiler/xlf/FSComp.txt.ko.xlf b/src/Compiler/xlf/FSComp.txt.ko.xlf index 51778bd4145..82ae28fb525 100644 --- a/src/Compiler/xlf/FSComp.txt.ko.xlf +++ b/src/Compiler/xlf/FSComp.txt.ko.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides 비가상 멤버 재정의에 대한 오류 발생 diff --git a/src/Compiler/xlf/FSComp.txt.pl.xlf b/src/Compiler/xlf/FSComp.txt.pl.xlf index a8a3f05ba9a..ce864bc1237 100644 --- a/src/Compiler/xlf/FSComp.txt.pl.xlf +++ b/src/Compiler/xlf/FSComp.txt.pl.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides Zgłasza błędy w przypadku przesłonięć elementów innych niż wirtualne diff --git a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf index 1f62f9a3d04..a348ccb242a 100644 --- a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf +++ b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides Gera erros para substituições de membros não virtuais diff --git a/src/Compiler/xlf/FSComp.txt.ru.xlf b/src/Compiler/xlf/FSComp.txt.ru.xlf index 64ce92b6125..890ca47a920 100644 --- a/src/Compiler/xlf/FSComp.txt.ru.xlf +++ b/src/Compiler/xlf/FSComp.txt.ru.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides Вызывает ошибки при переопределениях невиртуальных элементов diff --git a/src/Compiler/xlf/FSComp.txt.tr.xlf b/src/Compiler/xlf/FSComp.txt.tr.xlf index d77dfd672ec..46c2f5ee72d 100644 --- a/src/Compiler/xlf/FSComp.txt.tr.xlf +++ b/src/Compiler/xlf/FSComp.txt.tr.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides Sanal olmayan üyelerde geçersiz kılmalar için hatalar oluştur diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf index 5393d25202a..149ace7283c 100644 --- a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf +++ b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides 引发非虚拟成员替代的错误 diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf index f75fc155fde..68deae6a779 100644 --- a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf +++ b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf @@ -297,6 +297,11 @@ Enforce AttributeTargets on functions + + Enforce AttributeTargets on union case declarations + Enforce AttributeTargets on union case declarations + + Raises errors for non-virtual members overrides 引發非虛擬成員覆寫的錯誤 From 75ef665c0e051f4c4da89b361ff41f6d79e03af2 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 26 Feb 2024 20:46:38 +0000 Subject: [PATCH 4/8] release notes --- docs/release-notes/.Language/preview.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/release-notes/.Language/preview.md b/docs/release-notes/.Language/preview.md index f39dd172561..a4dd48f213d 100644 --- a/docs/release-notes/.Language/preview.md +++ b/docs/release-notes/.Language/preview.md @@ -9,6 +9,7 @@ * Allow extension methods without type attribute work for types from imported assemblies. ([PR #16368](https://github.com/dotnet/fsharp/pull/16368)) * Enforce AttributeTargets on let values and functions. ([PR #16692](https://github.com/dotnet/fsharp/pull/16692)) +* Enforce AttributeTargets on union case declarations. ([PR #16764](https://github.com/dotnet/fsharp/pull/16764)) ### Changed From bdd617ff0d5ecfc28bc59cc7c51934b3dbedd3af Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 26 Feb 2024 21:45:21 +0000 Subject: [PATCH 5/8] format code --- src/Compiler/Facilities/LanguageFeatures.fs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Compiler/Facilities/LanguageFeatures.fs b/src/Compiler/Facilities/LanguageFeatures.fs index dab5b70bc57..dd7133224e8 100644 --- a/src/Compiler/Facilities/LanguageFeatures.fs +++ b/src/Compiler/Facilities/LanguageFeatures.fs @@ -347,8 +347,7 @@ type LanguageVersion(versionText) = | LanguageFeature.BooleanReturningAndReturnTypeDirectedPartialActivePattern -> FSComp.SR.featureBooleanReturningAndReturnTypeDirectedPartialActivePattern () | LanguageFeature.EnforceAttributeTargetsOnFunctions -> FSComp.SR.featureEnforceAttributeTargetsOnFunctions () - | LanguageFeature.EnforceAttributeTargetsUnionCaseDeclarations -> - FSComp.SR.featureEnforceAttributeTargetsUnionCaseDeclarations () + | LanguageFeature.EnforceAttributeTargetsUnionCaseDeclarations -> FSComp.SR.featureEnforceAttributeTargetsUnionCaseDeclarations () | LanguageFeature.LowerInterpolatedStringToConcat -> FSComp.SR.featureLowerInterpolatedStringToConcat () /// Get a version string associated with the given feature. From b4781664c3dc6c008aa3247ef77cba98f381a964 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Tue, 27 Feb 2024 13:37:38 +0000 Subject: [PATCH 6/8] improve naming --- src/Compiler/Checking/CheckExpressions.fs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs index f043ed1894d..a4cafbdefbd 100644 --- a/src/Compiler/Checking/CheckExpressions.fs +++ b/src/Compiler/Checking/CheckExpressions.fs @@ -10874,19 +10874,19 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt errorR(Error(FSComp.SR.tcLiteralCannotHaveGenericParameters(), mBinding)) if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsOnFunctions) && memberFlagsOpt.IsNone && not attrs.IsEmpty then - TcAttributeTargetsOnLetBindings cenv env attrs overallPatTy overallExprTy declaredTypars.IsEmpty + TcAttributeTargetsOnLetBindings cenv env attrs overallPatTy overallExprTy (not declaredTypars.IsEmpty) CheckedBindingInfo(inlineFlag, valAttribs, xmlDoc, tcPatPhase2, explicitTyparInfo, nameToPrelimValSchemeMap, rhsExprChecked, argAndRetAttribs, overallPatTy, mBinding, debugPoint, isCompGen, literalValue, isFixed), tpenv // Note: // - Let bound values can only have attributes that uses AttributeTargets.Field ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue // - Let function bindings can only have attributes that uses AttributeTargets.Method ||| AttributeTargets.ReturnValue -and TcAttributeTargetsOnLetBindings (cenv: cenv) env attrs overallPatTy overallExprTy hasDeclTypars = +and TcAttributeTargetsOnLetBindings (cenv: cenv) env attrs overallPatTy overallExprTy areTyparsDeclared = let rhsIsFunction = isFunTy cenv.g overallPatTy let lhsIsFunction = isFunTy cenv.g overallExprTy let attrTgt = match rhsIsFunction, lhsIsFunction with - | false, false when hasDeclTypars -> AttributeTargets.Field ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue + | false, false when not areTyparsDeclared -> AttributeTargets.Field ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue | _, _ -> AttributeTargets.Method ||| AttributeTargets.ReturnValue TcAttributes cenv env attrTgt attrs |> ignore From 7fe37a15d39286ddf80aef6f825ef884b9f60637 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Thu, 29 Feb 2024 19:05:58 +0000 Subject: [PATCH 7/8] Update src/Compiler/Checking/CheckExpressions.fs Co-authored-by: Brian Rourke Boll --- src/Compiler/Checking/CheckExpressions.fs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs index a4cafbdefbd..53f21bf1747 100644 --- a/src/Compiler/Checking/CheckExpressions.fs +++ b/src/Compiler/Checking/CheckExpressions.fs @@ -10882,12 +10882,20 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt // - Let bound values can only have attributes that uses AttributeTargets.Field ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue // - Let function bindings can only have attributes that uses AttributeTargets.Method ||| AttributeTargets.ReturnValue and TcAttributeTargetsOnLetBindings (cenv: cenv) env attrs overallPatTy overallExprTy areTyparsDeclared = - let rhsIsFunction = isFunTy cenv.g overallPatTy - let lhsIsFunction = isFunTy cenv.g overallExprTy let attrTgt = - match rhsIsFunction, lhsIsFunction with - | false, false when not areTyparsDeclared -> AttributeTargets.Field ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue - | _, _ -> AttributeTargets.Method ||| AttributeTargets.ReturnValue + if + // It's a type function: + // let x<'a> = … + areTyparsDeclared + // It's a regular function-valued binding: + // let f x = … + // let f = fun x -> … + || isFunTy cenv.g overallPatTy + || isFunTy cenv.g overallExprTy + then + AttributeTargets.ReturnValue ||| AttributeTargets.Method + else + AttributeTargets.ReturnValue ||| AttributeTargets.Field ||| AttributeTargets.Property TcAttributes cenv env attrTgt attrs |> ignore From 80cbd2a3d0d43203d900738771e601815bfb6f94 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Fri, 1 Mar 2024 11:46:00 +0000 Subject: [PATCH 8/8] Fix merge conflict --- src/Compiler/Checking/CheckDeclarations.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compiler/Checking/CheckDeclarations.fs b/src/Compiler/Checking/CheckDeclarations.fs index ffb3eb9e2d5..c82a5edf6f3 100644 --- a/src/Compiler/Checking/CheckDeclarations.fs +++ b/src/Compiler/Checking/CheckDeclarations.fs @@ -519,7 +519,7 @@ module TcRecdUnionAndEnumDeclarations = else TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs - let vis, _ = ComputeAccessAndCompPath env None m vis None parent + let vis, _ = ComputeAccessAndCompPath g env None m vis None parent let vis = CombineReprAccess parent vis CheckUnionCaseName cenv id hasRQAAttribute