dotnet / dotnet/fsharp

Overloading resolution of C# extension methods is affected by assembly definition order

Open
#5,526 3 comments 2 reactions 0 assignees View on GitHub
Area-Compiler-Checking Area-Compiler-Checking-Overloads Bug Impact-Low
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

Overloading resolution with C# extension methods that is overloaded with multiple class is caused unexpected behavior.
This means, renaming source code files is breaking-change.

#### Repro steps

C# extension method that is overloaded with multiple class, like this:

```csharp
namespace CSharpLib
{
public static class Extension1 { public static string A(this string _) => "string specific"; }
public static class Extension2 { public static string A(this T _) => $"generic({typeof(T)})"; }
}
```

call in F#:

```fsharp
open CSharpLib

let main _ =
let x, y = "abc", 123
printfn "x: %s, y: %s" (x.A()) (y.A())
0

// output:
// x: string specific, y: generic(System.Int32)
```

Swap the definition:

```csharp
// swap two class definition!
public static class Extension2 { public static string A(this T _) => $"generic({typeof(T)})"; }
public static class Extension1 { public static string A(this string _) => "string specific"; }

// F# output:
// x: generic(System.String), y: generic(System.Int32)
```

#### Expected behavior
Two have same output that should be `x: string specific, y: generic(System.Int32)`.

#### Actual behavior
Resolved with the FIRST definition in assembly.

#### Related information
[full testing code is here](https://github.com/acple/FSharpOverloadingTest)

* .NET Runtime, CoreCLR or Mono Version
I tested in:
* macOS + .NET Core (.NETCoreApp,Version=v2.1)
* Windows + .NET Core (.NETCoreApp,Version=v2.1)
* Windows + .NET Framework (.NETFramework,Version=v4.6.1)

* Editing Tools (e.g. Visual Studio Version)
VSCode 1.26.0
Run with dotnet-cli

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.