Skip to content

Regression in 16.10.0 release results in System.BadImageFormatException #11620

@kentcb

Description

@kentcb

Since the 16.10.0 VS update, the below code produces a BadImageFormatException. Prior to that release, it works as expected.

Repro steps

module Repro

type Data = interface end
and Service<'Data when 'Data :> Data>() = class end

type IThing = interface end
and Thing<'T> = { Metadata: 'T } with interface IThing

let createService metadata = (Service<'Data>())

let getCreateServiceCallback<'T> (thing: IThing) =
    let upcastThing =
        thing
        :?> Thing<'T>
    let getService () = createService upcastThing.Metadata
    (fun () -> getService)

[<EntryPoint>]
let main _ =
    let dummyThing : Thing<int> = { Thing.Metadata = 42 }
    // crash occurs on the following line
    let callback = getCreateServiceCallback<int> dummyThing
    let resolvedService = callback ()
    printfn "Resolved service: %A" resolvedService
    System.Console.ReadKey() |> ignore
    0

Expected behavior

Code should work as it did prior to 16.10.0, or at least produce a compiler error.

Actual behavior

Crashes with BadImageFormatException on the indicated line.

Known workarounds

  • Marking the getService function as inline
  • Explicitly annotating the getService return type as Service<Data>
  • Run in release mode

Related information

  • Windows 10
  • .NET 5
  • Visual Studio 2019

Metadata

Metadata

Assignees

Labels

Impact-High(Internal MS Team use only) Describes an issue with extreme impact on existing code.Regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions