Skip to content

AttributeUsage doesn't distinguish AttributeTargets.Class and AttributeTargets.Struct #13526

Description

@auduchinok
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

Activity

  1. dsyme commented on Sep 21, 2022

    @dsyme
    Contributor

    @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
  2. added
    Impact-Low(Internal MS Team use only) Describes an issue with limited impact on existing code.
    on Sep 21, 2022
  3. auduchinok commented on Sep 22, 2022

    @auduchinok
    MemberAuthor

    @dsyme Yes, I think it was about structs.

  4. changed the title [-]`AttributeUsage` doesn't distinguish `AttributeTargets.Class` and `AttributeTargets.Constructor`[/-] [+]`AttributeUsage` doesn't distinguish `AttributeTargets.Class` and `AttributeTargets.Struct`[/+] on Sep 22, 2022
  5. self-assigned this
    on Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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.good first issuehelp wanted

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions