F# compiler internal error FS0073 when merging duplicate property accessors for interface/class property implementation
- Dominant language
- F#
- Stars
- 4.3k
- Forks
- 876
- Avg merge
- 4d 22h
- Merged PRs (30d)
- 144
Description
## 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
```fsharp
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:
```text
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`.
```fsharp
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:
```fsharp
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
```fsharp
[]
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`.
Contributor guide
Research direction
Start in src/Compiler/CodeGen/IlxGen.fs by reading MergeOptions and the surrounding property-merging logic. Add the suggested regression test for the class and interface property implementation, then run it to confirm the code compiles without FS0073.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fsharp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100