Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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