fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

`AttributeUsage` doesn't distinguish `AttributeTargets.Class` and `AttributeTargets.Struct`

Open auduchinok opened this issue 3 years ago • 2 comments

open System

[<AttributeUsage(AttributeTargets.Class)>]
type CustomClassAttribute() =
    inherit Attribute()

[<AttributeUsage(AttributeTargets.Struct)>]
type CustomStructAttribute() =
    inherit Attribute()

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

[<CustomClass; CustomStruct; CustomMethod>]
type Class() = class end

[<CustomClass; CustomStruct; CustomMethod>]
type Struct(x: int) = struct end

Struct and class attributes seem to be allowed to be used interchangeably:

Screenshot 2022-07-19 at 18 10 26

auduchinok avatar Jul 19 '22 16:07 auduchinok

@auduchinok Could you check the title of this bug please? It says .Constructor but doesn't mention that in the description?

Seems there are three problems:

  • CustomStruct can be targeted at class incorrectly
  • CustomClass can be targeted at struct incorrectly
  • CustomMethod can be targeted at a constructor incorrectly

This seem ok:

  • CustomConstructor can't be targeted at a method

Example this works but shouldn't:

open System
[<AttributeUsage(AttributeTargets.Method)>]
type CustomMethodAttribute() =
    inherit Attribute()

type Class [<CustomMethod>] () = class end

type Struct [<CustomMethod>] (x: int) = struct end```
and this works (as expected:
```fsharp

[<AttributeUsage(AttributeTargets.Constructor)>]
type CustomConstructorAttribute() =
    inherit Attribute()

type Class [<CustomConstructor>] () = class end

type Struct [<CustomConstructor>] (x: int) = struct end

dsyme avatar Sep 21 '22 14:09 dsyme

@dsyme Yes, I think it was about structs.

auduchinok avatar Sep 22 '22 13:09 auduchinok