dotnet / dotnet/linker

Fix virtual/interface method resolution

Open
#1,187 0 comments 1 reaction 0 assignees View on GitHub
area-Linker: Steps
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

The virtual method resolution algorithm in use within linker is quite wrong and very difficult to map to the spec. One doesn't even go into the IL-level corner cases to get the algorithm to do wrong things.

For example, this is going to generate invalid outputs after linking:

```csharp
interface IFoo
{
void Frob(int x);
}

class Base
{
// Linker thinks this method implements IFoo.Frob in Derived
protected virtual void Frob(int x) => Console.WriteLine("Unrelated");
public virtual void Frob(T x) => Console.WriteLine("Actual");
}

class Derived : Base, IFoo
{
}
```

This is going to keep more methods than necessary:

```csharp
interface IFoo
{
void Frob();
}

class Base : IFoo
{
// Linker thinks both methods implement the interface
public virtual void Frob() => Console.WriteLine("NameAndSig");
void IFoo.Frob() => Console.WriteLine("MethodImpl");
}
```

This will also keep more methods than necessary:

```csharp
class Base
{
protected virtual void Frob() => Console.WriteLine("Nobody calls me");
}

class Derived : Base
{
// Linker thinks this overrides Base.Frob, but they’re unrelated
public new virtual void Frob() => Console.WriteLine("I am used");
}
```

This needs to be reimplemented according to the spec.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.