Skip to content

F# 7.0 generate incorrect program in release config. #14492

@flarelee

Description

@flarelee

F# 7.0 compiled program failed to run .

Repro steps

// fail to run in release config
// runtime: .net framework 4.7
// F# version: 7.0 12.0.5.0 
// Visual Studio 2022
(*
error message: System.TypeLoadException: 
'Method 'Specialize' on type 'memoizeLatestRef@5-1' from 
assembly 'FSharpCompilerBugs, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 
tried to implicitly override a method with weaker type parameter constraints.'
*)

let inline refEquals<'a when 'a : not struct> (a : 'a) (b : 'a) = obj.ReferenceEquals (a, b)

let inline tee f x = 
    f x
    x

let memoizeLatestRef (f: 'a -> 'b) =
    let cell = ref None
    let f' (x: 'a) =
        match !cell with
        | Some (x', value) when refEquals x' x -> value
        | _ -> f x |> tee (fun y -> cell := Some (x, y))
    f'

module BugInReleaseConfig = 

    let test f x = 
        printfn "%s" (f x)

    let f: string -> string = memoizeLatestRef id

    let run () = test f "ok"

[<EntryPoint>]
let main argv =
    let culture = System.Globalization.CultureInfo("en")
    System.Globalization.CultureInfo.CurrentCulture <- culture
    System.Globalization.CultureInfo.CurrentUICulture <- culture

    BugInReleaseConfig.run ()

    System.Console.ReadLine() |> ignore
    0

Expected behavior

It should print "ok".

Actual behavior

A System.TypeLoadException was throwed.

Source code
FSharpCompilerBugs.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-CodeGenIlxGen, ilwrite and things at the backendBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    No fields configured for Bug.

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions