diff --git a/src/Compiler/Checking/CheckDeclarations.fs b/src/Compiler/Checking/CheckDeclarations.fs index 92368118f3a..4543ced5a4d 100644 --- a/src/Compiler/Checking/CheckDeclarations.fs +++ b/src/Compiler/Checking/CheckDeclarations.fs @@ -576,22 +576,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 aab4de2a554..b4750c5162c 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 adb2b01d334..242ad3d5071 100644 --- a/src/Compiler/xlf/FSComp.txt.cs.xlf +++ b/src/Compiler/xlf/FSComp.txt.cs.xlf @@ -1362,6 +1362,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 7deaf880012..d3abef8190a 100644 --- a/src/Compiler/xlf/FSComp.txt.de.xlf +++ b/src/Compiler/xlf/FSComp.txt.de.xlf @@ -1362,6 +1362,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 e8090e5579e..7b0eaa97b37 100644 --- a/src/Compiler/xlf/FSComp.txt.es.xlf +++ b/src/Compiler/xlf/FSComp.txt.es.xlf @@ -1362,6 +1362,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 25faaecb3a4..60e1eb93649 100644 --- a/src/Compiler/xlf/FSComp.txt.fr.xlf +++ b/src/Compiler/xlf/FSComp.txt.fr.xlf @@ -1362,6 +1362,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 30d68a3330b..fc9359a1ecb 100644 --- a/src/Compiler/xlf/FSComp.txt.it.xlf +++ b/src/Compiler/xlf/FSComp.txt.it.xlf @@ -1362,6 +1362,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 9f2709fa559..c3f4a02058c 100644 --- a/src/Compiler/xlf/FSComp.txt.ja.xlf +++ b/src/Compiler/xlf/FSComp.txt.ja.xlf @@ -1362,6 +1362,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 2c80d361ef6..bcedac7f720 100644 --- a/src/Compiler/xlf/FSComp.txt.ko.xlf +++ b/src/Compiler/xlf/FSComp.txt.ko.xlf @@ -1362,6 +1362,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 0bd80d75a72..6a7f8f9bf98 100644 --- a/src/Compiler/xlf/FSComp.txt.pl.xlf +++ b/src/Compiler/xlf/FSComp.txt.pl.xlf @@ -1362,6 +1362,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 c0e79885fed..aaef5596c1e 100644 --- a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf +++ b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf @@ -1362,6 +1362,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 3a4610e93df..5ca95036ec7 100644 --- a/src/Compiler/xlf/FSComp.txt.ru.xlf +++ b/src/Compiler/xlf/FSComp.txt.ru.xlf @@ -1362,6 +1362,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 6d1bee2476b..aeeaec2804b 100644 --- a/src/Compiler/xlf/FSComp.txt.tr.xlf +++ b/src/Compiler/xlf/FSComp.txt.tr.xlf @@ -1362,6 +1362,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 a60e3188af7..910b088442a 100644 --- a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf +++ b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf @@ -1362,6 +1362,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 3253c4e45f7..66aa0920d0c 100644 --- a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf +++ b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf @@ -1362,6 +1362,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 []