Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad image when calling op_Addition on interface #14012

Closed
gusty opened this issue Oct 3, 2022 · 5 comments · Fixed by #17021
Closed

Bad image when calling op_Addition on interface #14012

gusty opened this issue Oct 3, 2022 · 5 comments · Fixed by #17021
Assignees
Labels
Area-Compiler-ImportAndInterop import of .NET DLLs and interop Bug Impact-Low (Internal MS Team use only) Describes an issue with limited impact on existing code.
Milestone

Comments

@gusty
Copy link
Contributor

gusty commented Oct 3, 2022

Calling op_Addition on interface results in a Bad Image error.

Repro steps

  1. open System.Numerics;;

  2. IAdditionOperators.op_Addition (3, 6) ;;

Expected behavior

Compiles with val it: int = 9

Actual behavior

Fails compilation with:

> IAdditionOperators.op_Addition (3, 6) ;;
System.BadImageFormatException: Bad IL format.
   at <StartupCode$FSI_0012>.$FSI_0012.main@()
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
Stopped due to error

Known workarounds

Don't call directly on the interface.

Related information

Other operations, like op_CheckedAddition work fine:

> IAdditionOperators.op_CheckedAddition (3, 6) ;;
val it: int = 9

Test on:

Microsoft (R) F# Interactive version 12.0.5.0 for F# 7.0

Microsoft Visual Studio Community 2022 (64-bit) - Preview
Version 17.4.0 Preview 2.1

@vzarytovskii
Copy link
Member

Checked version is virtual member, and normal is abstract. I think it should be disallowed in general - calling methods directly on the interfaces, wdyt @dsyme?

@dsyme
Copy link
Contributor

dsyme commented Oct 27, 2022

@vzarytovskii Curious, does C# allow this? I presume not? I assume we must be missing a check to disallow this?

@vzarytovskii
Copy link
Member

@vzarytovskii Curious, does C# allow this? I presume not? I assume we must be missing a check to disallow this?

Nope, it doesn't for operators. It may for normal static methods with default implementation

@vzarytovskii vzarytovskii moved this from Not Planned to Planned in F# Compiler and Tooling Oct 31, 2022
@vzarytovskii vzarytovskii removed their assignment Jan 5, 2023
@vzarytovskii vzarytovskii added this to the Backlog milestone Feb 6, 2023
@anpin
Copy link

anpin commented May 5, 2023

I'm getting the same exception running this in fsi

dotnet fsi repro.fsx

/home/a/repro.fsx(4,3): warning FS3535: Declaring "interfaces with static abstract methods" is an advanced feature. See https://aka.ms/fsharp-iwsams for guidance. You can disable this warning by using '#nowarn "3535"' or '--nowarn:3535'.

System.BadImageFormatException: Bad IL format.
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
Stopped due to error
open System

type x2<'a> =
  static abstract x2: 'a -> double

type random() =
  member _.next = Random.Shared.NextDouble()

  interface x2<random> with
    static member x2 __ = __.next * 2.0

random() |> x2.x2 |> printf "%A"

@vzarytovskii
Copy link
Member

vzarytovskii commented Nov 22, 2023

Just for historical reasons, this is why it's crashing (abstract vs virtual):
https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Numerics/IAdditionOperators.cs,5b77e6b404819054

Check will be introduced to ensure we generate call on virtual members, can't call abstracts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler-ImportAndInterop import of .NET DLLs and interop Bug Impact-Low (Internal MS Team use only) Describes an issue with limited impact on existing code.
Projects
Archived in project
5 participants