Skip to content

Commit

Permalink
Update union-case declaration AttributeTargets.Property (dotnet#16807)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarfgp authored Mar 5, 2024
1 parent 9877cfe commit ce1a1b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Compiler/Checking/CheckDeclarations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,6 @@ 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 =
// 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.EnforceAttributeTargetsUnionCaseDeclarations) then
TcAttributes cenv env AttributeTargets.Method synAttrs
else
TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs

let vis, _ = ComputeAccessAndCompPath g env None m vis None parent
let vis = CombineReprAccess parent vis

Expand Down Expand Up @@ -571,6 +563,23 @@ 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.EnforceAttributeTargetsUnionCaseDeclarations) then
let target = if rfields.IsEmpty then AttributeTargets.Property else AttributeTargets.Method
TcAttributes cenv env target synAttrs
else
TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs

Construct.NewUnionCase id rfields recordTy attrs xmlDoc vis

let TcUnionCaseDecls (cenv: cenv) env (parent: ParentRef) (thisTy: TType) (thisTyInst: TypeInst) hasRQAAttribute tpenv unionCases =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

open System

[<AttributeUsage(AttributeTargets.Property)>]
type PropertyLevelAttribute() =
inherit Attribute()

type U =
| [<PropertyLevel>] A
| [<PropertyLevel>] B
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ module CustomAttributes_AttributeUsage =
|> withLangVersionPreview
|> verifyCompileAndRun
|> shouldSucceed

// SOURCE=AttributeTargetsIsProperty.fs # AttributeTargetsIsProperty.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"AttributeTargetsIsProperty.fs"|])>]
let ``AttributeTargetsIsProperty_fs`` compilation =
compilation
|> verifyCompile
|> shouldSucceed

// SOURCE=AttributeTargetsIsProperty.fs # AttributeTargetsIsProperty.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"AttributeTargetsIsProperty.fs"|])>]
let ``AttributeTargetsIsProperty_fs preview`` compilation =
compilation
|> withLangVersionPreview
|> verifyCompile
|> shouldSucceed

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

0 comments on commit ce1a1b9

Please sign in to comment.