Skip to content

Calling a member inferred from a reference-typed generic constraint unnecessarily boxes the value. #10499

Open
@teo-tsirpanis

Description

@teo-tsirpanis

When calling a method on a generic value that is constrained to be a reference type, the compiler mistakenly emits a box and unbox.any sequence of commands.

Repro steps

Compile the following function:

let getDelegateMethod<'TDelegate when 'TDelegate :> System.Delegate> (del: 'TDelegate) = del.Method

Expected behavior

The generated IL should be:

ldarg.0
callvirt instance class [System.Private.CoreLib]System.Reflection.MethodInfo [System.Private.CoreLib]System.Delegate::get_Method()
ret

Actual behavior

The generated IL is:

ldarg.0
box !!TDelegate
unbox.any [System.Private.CoreLib]System.Delegate
callvirt instance class [System.Private.CoreLib]System.Reflection.MethodInfo [System.Private.CoreLib]System.Delegate::get_Method()
ret

Known workarounds

None

Related information

Calling the method using constrained.callvirt is not necessary; we surely have a reference type and just a callvirt suffices.

  • .NET Runtime kind: .NET SDK 5.0.100

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions