From 4e85c3a81ccae955af115928d0e88ae53a4940cf Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 5 May 2025 13:49:45 +0100 Subject: [PATCH] raise an opt-in warning attribute not valid for union case with fields --- src/Compiler/Checking/CheckDeclarations.fs | 43 ++++++++++++------- src/Compiler/Driver/CompilerDiagnostics.fs | 1 + src/Compiler/FSComp.txt | 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 +++ .../AttributeUsage/AttributeUsage.fs | 7 +-- 17 files changed, 95 insertions(+), 22 deletions(-) diff --git a/src/Compiler/Checking/CheckDeclarations.fs b/src/Compiler/Checking/CheckDeclarations.fs index 78acf62bb98..69f339c5c24 100644 --- a/src/Compiler/Checking/CheckDeclarations.fs +++ b/src/Compiler/Checking/CheckDeclarations.fs @@ -577,22 +577,33 @@ module TcRecdUnionAndEnumDeclarations = let checkXmlDocs = cenv.diagnosticOptions.CheckXmlDocs let xmlDoc = xmldoc.ToXmlDoc(checkXmlDocs, Some names) - let attrs = - (* - The attributes of a union case decl get attached to the generated "static factory" method. - Enforce union-cases AttributeTargets: - - AttributeTargets.Method - type SomeUnion = - | Case1 of int // Compiles down to a static method - - AttributeTargets.Property - type SomeUnion = - | Case1 // Compiles down to a static property - *) - if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargets) then - let target = if rfields.IsEmpty then AttributeTargets.Property else AttributeTargets.Method - TcAttributes cenv env target synAttrs - else - TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs + let attrs = TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs + (* + The attributes of a union case decl get attached to the generated "static factory" method. + Enforce union-cases AttributeTargets: + - AttributeTargets.Method + type SomeUnion = + | Case1 of int // Compiles down to a static method + - AttributeTargets.Property + type SomeUnion = + | Case1 // Compiles down to a static property + *) + if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargets) then + let attrTargets = + attrs + |> List.collect (fun attr -> + attr.TyconRef.Attribs + |> List.choose (fun attr -> + match attr with + | Attrib(unnamedArgs = [ AttribInt32Arg validOn ]) -> Some validOn + | _ -> None)) + + attrTargets + |> List.iter (fun target -> + // If the union case has fields, and the target is not AttributeTargets.Method || AttributeTargets.All. Then we raise a separate opt-in warning + let targetEnum = enum target + if targetEnum <> AttributeTargets.Method && targetEnum <> AttributeTargets.All then + warning(Error(FSComp.SR.tcAttributeIsNotValidForUnionCaseWithFields(), id.idRange))) Construct.NewUnionCase id rfields recordTy attrs xmlDoc vis diff --git a/src/Compiler/Driver/CompilerDiagnostics.fs b/src/Compiler/Driver/CompilerDiagnostics.fs index 68813e69a83..3da96b19af6 100644 --- a/src/Compiler/Driver/CompilerDiagnostics.fs +++ b/src/Compiler/Driver/CompilerDiagnostics.fs @@ -402,6 +402,7 @@ type PhasedDiagnostic with | 3579 -> false // alwaysUseTypedStringInterpolation - off by default | 3582 -> false // infoIfFunctionShadowsUnionCase - off by default | 3570 -> false // tcAmbiguousDiscardDotLambda - off by default + | 3875 -> false // tcAttributeIsNotValidForUnionCaseWithFields - off by default | _ -> match x.Exception with | DiagnosticEnabledWithLanguageFeature(_, _, _, enabled) -> enabled diff --git a/src/Compiler/FSComp.txt b/src/Compiler/FSComp.txt index 9bfa12ce963..1626a16eb71 100644 --- a/src/Compiler/FSComp.txt +++ b/src/Compiler/FSComp.txt @@ -1795,6 +1795,7 @@ featureUseTypeSubsumptionCache,"Use type conversion cache during compilation" featureDontWarnOnUppercaseIdentifiersInBindingPatterns,"Don't warn on uppercase identifiers in binding patterns" 3873,chkDeprecatePlacesWhereSeqCanBeOmitted,"This construct is deprecated. Sequence expressions should be of the form 'seq {{ ... }}'" 3874,tcExpectedTypeParamMarkedWithUnitOfMeasureAttribute,"Expected unit-of-measure type parameter must be marked with the [] attribute." +3875,tcAttributeIsNotValidForUnionCaseWithFields,"This attribute is not valid for use on union cases with fields." featureDeprecatePlacesWhereSeqCanBeOmitted,"Deprecate places where 'seq' can be omitted" featureSupportValueOptionsAsOptionalParameters,"Support ValueOption as valid type for optional member parameters" featureSupportWarnWhenUnitPassedToObjArg,"Warn when unit is passed to a member accepting `obj` argument, e.g. `Method(o:obj)` will warn if called via `Method()`." diff --git a/src/Compiler/xlf/FSComp.txt.cs.xlf b/src/Compiler/xlf/FSComp.txt.cs.xlf index 7d64601c303..7ef1a1e84cc 100644 --- a/src/Compiler/xlf/FSComp.txt.cs.xlf +++ b/src/Compiler/xlf/FSComp.txt.cs.xlf @@ -1357,6 +1357,11 @@ Pole {0} se v tomto anonymním typu záznamu vyskytuje vícekrát. + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. Atributy nejde použít pro rozšíření typů. diff --git a/src/Compiler/xlf/FSComp.txt.de.xlf b/src/Compiler/xlf/FSComp.txt.de.xlf index 67f4270377e..25dbe4e488b 100644 --- a/src/Compiler/xlf/FSComp.txt.de.xlf +++ b/src/Compiler/xlf/FSComp.txt.de.xlf @@ -1357,6 +1357,11 @@ Das Feld "{0}" ist in diesem anonymen Datensatztyp mehrmals vorhanden. + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. Attribute können nicht auf Typerweiterungen angewendet werden. diff --git a/src/Compiler/xlf/FSComp.txt.es.xlf b/src/Compiler/xlf/FSComp.txt.es.xlf index 9478e3d2c55..435451353fe 100644 --- a/src/Compiler/xlf/FSComp.txt.es.xlf +++ b/src/Compiler/xlf/FSComp.txt.es.xlf @@ -1357,6 +1357,11 @@ El campo "{0}" aparece varias veces en este tipo de registro anónimo. + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. Los atributos no se pueden aplicar a las extensiones de tipo. diff --git a/src/Compiler/xlf/FSComp.txt.fr.xlf b/src/Compiler/xlf/FSComp.txt.fr.xlf index 57fade5d250..61f3c03243a 100644 --- a/src/Compiler/xlf/FSComp.txt.fr.xlf +++ b/src/Compiler/xlf/FSComp.txt.fr.xlf @@ -1357,6 +1357,11 @@ Le champ '{0}' apparaît plusieurs fois dans ce type d'enregistrement anonyme. + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. Impossible d'appliquer des attributs aux extensions de type. diff --git a/src/Compiler/xlf/FSComp.txt.it.xlf b/src/Compiler/xlf/FSComp.txt.it.xlf index 14d670e8455..463590f2675 100644 --- a/src/Compiler/xlf/FSComp.txt.it.xlf +++ b/src/Compiler/xlf/FSComp.txt.it.xlf @@ -1357,6 +1357,11 @@ Il campo '{0}' viene visualizzato più volte in questo tipo di record anonimo. + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. Gli attributi non possono essere applicati a estensioni di tipo. diff --git a/src/Compiler/xlf/FSComp.txt.ja.xlf b/src/Compiler/xlf/FSComp.txt.ja.xlf index 78acb0fd944..de974fa04c0 100644 --- a/src/Compiler/xlf/FSComp.txt.ja.xlf +++ b/src/Compiler/xlf/FSComp.txt.ja.xlf @@ -1357,6 +1357,11 @@ この匿名レコードの種類に、フィールド '{0}' が複数回出現します。 + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. 属性を型拡張に適用することはできません。 diff --git a/src/Compiler/xlf/FSComp.txt.ko.xlf b/src/Compiler/xlf/FSComp.txt.ko.xlf index 604ac431e4a..6fe073a8b24 100644 --- a/src/Compiler/xlf/FSComp.txt.ko.xlf +++ b/src/Compiler/xlf/FSComp.txt.ko.xlf @@ -1357,6 +1357,11 @@ '{0}' 필드가 이 익명 레코드 형식에서 여러 번 나타납니다. + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. 형식 확장에 특성을 적용할 수 없습니다. diff --git a/src/Compiler/xlf/FSComp.txt.pl.xlf b/src/Compiler/xlf/FSComp.txt.pl.xlf index fdee3d82f7d..b8a17f1a3ed 100644 --- a/src/Compiler/xlf/FSComp.txt.pl.xlf +++ b/src/Compiler/xlf/FSComp.txt.pl.xlf @@ -1357,6 +1357,11 @@ Pole „{0}” występuje wielokrotnie w tym anonimowym typie rekordu. + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. Atrybutów nie można stosować do rozszerzeń typu. diff --git a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf index 2f86c57d960..f431f9c4968 100644 --- a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf +++ b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf @@ -1357,6 +1357,11 @@ O campo '{0}' aparece várias vezes nesse tipo de registro anônimo. + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. Os atributos não podem ser aplicados às extensões de tipo. diff --git a/src/Compiler/xlf/FSComp.txt.ru.xlf b/src/Compiler/xlf/FSComp.txt.ru.xlf index fefd5255a0b..927dbd0fefa 100644 --- a/src/Compiler/xlf/FSComp.txt.ru.xlf +++ b/src/Compiler/xlf/FSComp.txt.ru.xlf @@ -1357,6 +1357,11 @@ Поле "{0}" появляется несколько раз в этом типе анонимной записи. + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. Атрибуты не могут быть применены к расширениям типа. diff --git a/src/Compiler/xlf/FSComp.txt.tr.xlf b/src/Compiler/xlf/FSComp.txt.tr.xlf index 5325f0ab09f..978bad0de9f 100644 --- a/src/Compiler/xlf/FSComp.txt.tr.xlf +++ b/src/Compiler/xlf/FSComp.txt.tr.xlf @@ -1357,6 +1357,11 @@ '{0}' alanı bu anonim kayıt türünde birden fazla yerde görünüyor. + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. Öznitelikler tür uzantılarına uygulanamaz. diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf index 2e8b957d810..345049cfe8c 100644 --- a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf +++ b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf @@ -1357,6 +1357,11 @@ 字段“{0}”在此匿名记录类型中多次出现。 + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. 属性不可应用于类型扩展。 diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf index f0924b3d30f..9afda41de3f 100644 --- a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf +++ b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf @@ -1357,6 +1357,11 @@ 欄位 '{0}' 在這個匿名記錄類型中出現多次。 + + This attribute is not valid for use on union cases with fields. + This attribute is not valid for use on union cases with fields. + + Attributes cannot be applied to type extensions. 屬性無法套用到類型延伸模組。 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 c7c2cf72eca..fe2db9c7d74 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs @@ -492,7 +492,6 @@ module CustomAttributes_AttributeUsage = |> 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 @@ -509,11 +508,7 @@ module CustomAttributes_AttributeUsage = 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") - ] + |> shouldSucceed // SOURCE=E_AttributeTargetIsCtor01.fs # E_AttributeTargetIsCtor01.fs []