Skip to content

Delegates allowed to target AttributeTargets.Class AttributeTargets.Struct AttributeTargets.Interface incorrectly #16847

@edgarfgp

Description

@edgarfgp

F# delegates should only be able to target AttributeTargets.Delegate and show compiler error Error 842, "This attribute is not valid for use on this language element" otherwise.

Repro steps

open System

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


[<CustomClass>]
[<CustomStruct>]
[<CustomInterface>]
type Delegate1 = delegate of int -> int

Compiles downs to

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


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


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

    [Serializable]
    [CustomClass]
    [CustomStruct]
    [CustomInterface]
    [CompilationMapping(SourceConstructFlags.ObjectType)]
    public delegate int Delegate1(int);
}

sharplab

Expected behavior

Not compile and show (Error 842, Line 17, Col 16, Line 17, Col 28, "This attribute is not valid for use on this language element")

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.BugImpact-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