Skip to content

Compiler ignores AttributeUsage for Attributes attached to union cases #3112

@0x53A

Description

@0x53A

While browsing stackoverflow for something unrelated, I found an old mention of some compiler error.

(ref https://stackoverflow.com/questions/24884814/serializing-f-discriminated-unions-with-protobuf)

open System

[<AttributeUsage(AttributeTargets.Method)>]
type MethodLevelAttribute() =
    inherit Attribute()

[<AttributeUsage(AttributeTargets.Property ||| AttributeTargets.Field)>]
type PropertyOrFieldLevelAttribute() =
    inherit Attribute()

// correctly fails
//[<MethodLevelAttribute>]
//type SomeType() = class end

type SomeUnion =
| [<PropertyOrFieldLevelAttribute>] Case1 of int
| [<PropertyOrFieldLevelAttribute>] Case2 of string

module Program =

    [<EntryPoint>]
    let main argv = 
        printfn "%A" argv
        0 // return an integer exit code

Expected:

Compiler Error

Actual:

The compiler stuck the attributes to the static construction methods:

image

Version:

<package id="FSharp.Compiler.Tools" version="4.1.17" targetFramework="net452" />
(also repros with current nightly)

Note:

I only checked this one case, there may be other cases where it is also not correctly checked.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingBugImpact-Low(Internal MS Team use only) Describes an issue with limited impact on existing code.

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions