Skip to content

NullReferenceException when calling a virtual Object method on a value type from inline function #8098

@ForNeVeR

Description

@ForNeVeR

Repro steps

Write the following F# program, and run it:

let inline toString (x: ^a) =
    (^a : (member ToString : unit -> string) x)

[<EntryPoint>]
let main argv =
    let s = toString 123
    printfn "%s" s
    0

Expected behavior

This program should print a string 123.

Actual behavior

This program throws a NullReferenceException:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Program.main(String[] argv) in T:\Temp\ConsoleApp12\ConsoleApp12\Program.fs:line 6

It works on reference types though.

Known workarounds

Do not use an inline function there.

Related information

$ dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.100
 Commit:    cd82f021f4

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18362
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.100\

Host (useful for support):
  Version: 3.1.0
  Commit:  65f04fb6db

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-SRTPbugs in SRTP inference, resolution, witness passing, code genBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    No fields configured for Bug.

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions