Skip to content

Commit ce1a1b9

Browse files
authored
Update union-case declaration AttributeTargets.Property (#16807)
1 parent 9877cfe commit ce1a1b9

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

src/Compiler/Checking/CheckDeclarations.fs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,6 @@ module TcRecdUnionAndEnumDeclarations =
511511

512512
let TcUnionCaseDecl (cenv: cenv) env parent thisTy thisTyInst tpenv hasRQAAttribute (SynUnionCase(Attributes synAttrs, SynIdent(id, _), args, xmldoc, vis, m, _)) =
513513
let g = cenv.g
514-
let attrs =
515-
// The attributes of a union case decl get attached to the generated "static factory" method
516-
// Enforce that the union-cases can only be targeted by attributes with AttributeTargets.Method
517-
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsUnionCaseDeclarations) then
518-
TcAttributes cenv env AttributeTargets.Method synAttrs
519-
else
520-
TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs
521-
522514
let vis, _ = ComputeAccessAndCompPath g env None m vis None parent
523515
let vis = CombineReprAccess parent vis
524516

@@ -571,6 +563,23 @@ module TcRecdUnionAndEnumDeclarations =
571563

572564
let checkXmlDocs = cenv.diagnosticOptions.CheckXmlDocs
573565
let xmlDoc = xmldoc.ToXmlDoc(checkXmlDocs, Some names)
566+
let attrs =
567+
(*
568+
The attributes of a union case decl get attached to the generated "static factory" method.
569+
Enforce union-cases AttributeTargets:
570+
- AttributeTargets.Method
571+
type SomeUnion =
572+
| Case1 of int // Compiles down to a static method
573+
- AttributeTargets.Property
574+
type SomeUnion =
575+
| Case1 // Compiles down to a static property
576+
*)
577+
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsUnionCaseDeclarations) then
578+
let target = if rfields.IsEmpty then AttributeTargets.Property else AttributeTargets.Method
579+
TcAttributes cenv env target synAttrs
580+
else
581+
TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs
582+
574583
Construct.NewUnionCase id rfields recordTy attrs xmlDoc vis
575584

576585
let TcUnionCaseDecls (cenv: cenv) env (parent: ParentRef) (thisTy: TType) (thisTyInst: TypeInst) hasRQAAttribute tpenv unionCases =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
open System
3+
4+
[<AttributeUsage(AttributeTargets.Property)>]
5+
type PropertyLevelAttribute() =
6+
inherit Attribute()
7+
8+
type U =
9+
| [<PropertyLevel>] A
10+
| [<PropertyLevel>] B

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ module CustomAttributes_AttributeUsage =
7171
|> withLangVersionPreview
7272
|> verifyCompileAndRun
7373
|> shouldSucceed
74+
75+
// SOURCE=AttributeTargetsIsProperty.fs # AttributeTargetsIsProperty.fs
76+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"AttributeTargetsIsProperty.fs"|])>]
77+
let ``AttributeTargetsIsProperty_fs`` compilation =
78+
compilation
79+
|> verifyCompile
80+
|> shouldSucceed
81+
82+
// SOURCE=AttributeTargetsIsProperty.fs # AttributeTargetsIsProperty.fs
83+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"AttributeTargetsIsProperty.fs"|])>]
84+
let ``AttributeTargetsIsProperty_fs preview`` compilation =
85+
compilation
86+
|> withLangVersionPreview
87+
|> verifyCompile
88+
|> shouldSucceed
7489

7590
// SOURCE=ConditionalAttribute.fs # ConditionalAttribute.fs
7691
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"ConditionalAttribute.fs"|])>]

0 commit comments

Comments
 (0)