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
BadImageFormatException : Bad IL format when using base
#13926
Comments
Reproduces on module Testing
open System.Text.Json
open System.Text.Json.Serialization
type StringTrimJsonSerializer(o: JsonSerializerOptions) =
inherit JsonConverter<string>()
override this.Read(reader, _, _) =
match reader.TokenType with
| JsonTokenType.String -> reader.GetString().Trim()
| _ -> JsonException("Type is not a string") |> raise
override this.Write(writer, objectToWrite, options) = base.Write(writer, objectToWrite, options)
type SomeType = { Foo: string}
let serialize item =
let options = JsonSerializerOptions()
StringTrimJsonSerializer options |> options.Converters.Add
JsonSerializer.Serialize(item, options)
[<EntryPoint>]
let main _ =
{ Foo = "a" } |> serialize
0
Does not reproduce on: module Testing
open System.Text.Json
open System.Text.Json.Serialization
type StringTrimJsonSerializer(o: JsonSerializerOptions) =
inherit JsonConverter<string>()
override this.Read(reader, _, _) =
match reader.TokenType with
| JsonTokenType.String -> reader.GetString().Trim()
| _ -> JsonException("Type is not a string") |> raise
override this.Write(writer, objectToWrite, options) = base.Write(writer, objectToWrite, options)
type SomeType = { Foo: int }
let serialize item =
let options = JsonSerializerOptions()
StringTrimJsonSerializer options |> options.Converters.Add
JsonSerializer.Serialize(item, options)
[<EntryPoint>]
let main _ =
{ Foo = 1 } |> serialize
0 Both release and debug |
@vzarytovskii The latter never calls the The problem seems to be that The same is true here, but for some reason, F# doesn't consider it an abstract method. |
…16773) * Fix #13926 - BadImageFormatException : Bad IL format when using base * fantomas * Update src/Compiler/AbstractIL/il.fs Co-authored-by: Tomas Grosup <tomasgrosup@microsoft.com> * Update src/Compiler/FSharp.Compiler.Service.fsproj Co-authored-by: Tomas Grosup <tomasgrosup@microsoft.com> * Update 8.0.md --------- Co-authored-by: Tomas Grosup <tomasgrosup@microsoft.com> Co-authored-by: Petr <psfinaki@users.noreply.github.com>
Using
base.XXX
to call a base method causes aBadImageFormatException
to be raised.EDIT: it looks like F# allowing me to call an abstract method, as the
base
class is abstract. This, I believe, should not be allowed and JIT therefore throws aBadImageFormatException
. If you try to write code in F# with anAbstractClass
and a derived class, you'd receive a normal compile-time error.Repro steps
The following code (requires Giraffe and FsUnit to run the test) throws a
BadImageFormatException
Expected behavior
Should call the base method or give compile error if such method doesn't exist.
Actual behavior
Throws:
Known workarounds
Don't use
base
in a derived method.Related information
On dotnet 6, using
System.Text.Json
serialization BCL classes, Windows 10/11.EDIT, the IL of the offending code looks as follows (Release build):
The text was updated successfully, but these errors were encountered: