Skip to content

F# compiler internal error FS0073 when merging duplicate property accessors for interface/class property implementation #20265

Description

@xperiandri

Summary

The F# compiler can emit an internal compiler error (FS0073) in debug builds when a property is implemented both as a normal class member and as an explicit interface implementation of the same property signature.

The failure is not caused by user code; it is caused by a debug-only assertion in IL generation.

Repro

module Test

type IValue =
    abstract Value: int with get

type Value() =
    member _.Value = 1
    interface IValue with
        member _.Value = 1

Compiling this code can fail with:

error FS0073: internal error: MergeOptions: two values given

with a source span on the property implementation line.

Root cause

The bug is in the IL property merging logic in src/Compiler/CodeGen/IlxGen.fs.

let MergeOptions m o1 o2 =
    match o1, o2 with
    | Some x, None
    | None, Some x -> Some x
    | None, None -> None
    | Some x, Some _ ->
#if DEBUG
        errorR (InternalError("MergeOptions: two values given", m))
#else
        ignore m
#endif
        Some x

This function is used when multiple property definitions with the same name/signature are merged. In this scenario, a valid implementation can legitimately contain both a regular property accessor and an interface-implementation accessor for the same property. The merge is legal and should not be treated as a compiler-internal invariant violation.

Why this is a compiler bug

The code path is handling a valid property shape produced by the compiler itself. The debug-only assertion is effectively turning a legal merge into an internal error. This is a false-positive FS0073.

Expected behavior

The compiler should compile the code successfully, because the merge of duplicate property accessors is semantically redundant and harmless.

Proposed fix

Treat duplicate values as idempotent during merge, not as an internal error.

Minimal fix:

let MergeOptions _m o1 o2 =
    match o1, o2 with
    | Some x, None
    | None, Some x -> Some x
    | None, None -> None
    | Some x, Some _ -> Some x

This preserves the existing value and avoids crashing the compiler while still allowing the property merge logic to complete.

Additional context

This is particularly easy to trigger in debug builds because the assertion is compiled only inside #if DEBUG, but the underlying bug is still present in the merge semantics itself. The correct behavior is to merge duplicate accessors instead of raising an internal error.

Suggested regression test

[<Fact>]
let ``Property implementation can merge interface and class accessors`` () =
    FSharp
        """
        module Test

        type IValue =
            abstract Value: int with get

        type Value() =
            member _.Value = 1
            interface IValue with
                member _.Value = 1
        """
    |> compileAssembly
    |> ignore

This should compile without FS0073.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status
    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions