Skip to content

AttributeTargets.Class and AttributeTargets.Struct can be targeted in an enum incorrectly #16843

@edgarfgp

Description

@edgarfgp

Please provide a succinct description of the issue.

Repro steps

open System

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

[<CustomStruct>]
[<CustomClass>]
type Color =
    | Red = 0
    | Green = 1
    | Blue = 2

enum compiles down to sharplab

[CompilationMapping(SourceConstructFlags.Module)]
public static class @_
{
    [Serializable]
    [AttributeUsage(AttributeTargets.Struct)]
    [CompilationMapping(SourceConstructFlags.ObjectType)]
    public class CustomStructAttribute : Attribute
    {
    }


    [Serializable]
    [AttributeUsage(AttributeTargets.Class)]
    [CompilationMapping(SourceConstructFlags.ObjectType)]
    public class CustomClassAttribute : Attribute
    {
    }


    [Serializable]
    [CustomStruct]
    [CustomClass]
    [CompilationMapping(SourceConstructFlags.ObjectType)]
    public enum Color
    {
        Red,
        Green,
        Blue
    }
}

Expected behavior

Should not compile and Error 842 "This attribute is not valid for use on this language element" should be reported.

Actual behavior

Compiles

Known workarounds

Provide a description of any known workarounds.

Related information

Provide any related information (optional):

  • Operating system
  • .NET Runtime kind (.NET Core, .NET Framework, Mono)
  • Editing Tools (e.g. Visual Studio Version, Visual Studio)

Metadata

Metadata

Assignees

Labels

Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingBreaking-changeDescribes a bug which is also a breaking change.Bug

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions