Skip to content

[<return: X>] not respected on class members #19020

@DragonSA

Description

@DragonSA

The [<return: X>] approach to attaching an attribute to the return property of a method/function does not work on class (static) methods.

open System.Reflection
open Microsoft.FSharp.Quotations.Patterns

type SomeAttribute() =
    inherit System.Attribute()

module Module =
    let rec ``type`` =
        match <@ ``type`` @> with
        | PropertyGet (_, propertyInfo, _) -> propertyInfo.DeclaringType

    [<return: Some>]
    let func a = a + 1

type Class() =
    [<return: Some>]
    static member ``static member`` a = a + 1

    [<return: Some>]
    member _.``member`` a = a + 1

let printReturnAttr (typ: System.Type) name =
    typ.GetMethod(name).ReturnParameter.GetCustomAttribute<SomeAttribute>()
    |> printfn "%s: %O" name

printReturnAttr (Module.``type``) (nameof Module.func)
printReturnAttr typeof<Class> ("member")
printReturnAttr typeof<Class> (nameof Class.``static member``)

Actual output:

func: Program+SomeAttribute
member: <null>
static member: <null>

Looking at the IL, the "return" bit is being dropped. missingattr.txt

dotnet 9.0.305 and 10.0.100-rc.2.25502.107 on macOS 15.7.1 (24G231)

Sponsored by CP Dynamics

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions