Skip to content

[mono][metadata] Fixes for static virtual methods with interface variance - #91664

Closed
BrzVlad wants to merge 2 commits into
dotnet:mainfrom
BrzVlad:fix-virtual
Closed

BrzVlad wants to merge 2 commits into
dotnet:mainfrom
BrzVlad:fix-virtual

Conversation

@BrzVlad

@BrzVlad BrzVlad commented Sep 6, 2023 •

Copy link
Copy Markdown
Member

Fixes #88689
Contributes to #49904

…rent class first

When we lookup the vtable slot for a generic interface method, we first go through the set of implemented interfaces in the klass and look for an exact match. If the klass is not implementing this exact interface, we then look for any interface implementation that is variant compatible. Before this change we were iterating starting from the base implementations which is incorrect. We now iterate starting from the interfaces explicitly implemented by the most derived type (matching first interface in declaration order) and then continuing with the base types.

Scenario covered in ComplexHierarchyPositive.cs
…aces

As an example from the now enabled test.

```
interface InterfaceScenario2<in T> {
	static abstract int Method ();
}

class BaseScenario2 : InterfaceScenario2<string, InterfaceScenario2<object> {
	public static int Method() {
//		.override method int32 class InterfaceScenario2`1<string>::Method()
		return 2;
	}

	public static int32 Method_Obj() {
//		.override method int32 class InterfaceScenario2`1<object>::Method()
		return 3;
	}
}

class DerivedScenario2 : BaseScenario2 {
	public static int Method () {
//		.override method int32 class InterfaceScenario2`1<object>::Method()
		return 6;
	}
}

public static string Test_Scenario2_1<T, ImplType>() where ImplType : InterfaceScenario2<T> {
	return ImplType.Method ().ToString ();
}

Test_Scenario2_1<string, DerivedScenario2> ());
```

This test calls InterfaceScenario2<string> on DerviedScenario2. This should call DerivedScenario2.Method (not BaseScenario2.Method) because this method overrides (in BaseScenario2) both the obvious slot for InterfaceScenario2<object> as well as the slot for variant compatible interface InterfaceScenario2<string>.
@SamMonoRT

Copy link
Copy Markdown
Member

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@BrzVlad

BrzVlad commented Oct 5, 2023

Copy link
Copy Markdown
Member Author

@lambdageek Could you take a look at this ?

@kg

kg commented Jul 9, 2024

Copy link
Copy Markdown
Contributor

The actual algorithm and everything look right here, but I'm concerned that applying the variance at the vtable level is wrong as a general approach, since the spec seems to require the check to be done at call sites. Or does every specific generic instantiation of a given interface have its own slot in every class's vtable? I was under the impression that wasn't true, and that was part of the motivation for the IMT in minijit.

On the other hand, for the specific scenario of static virtual methods, the vtable approach seems like it's probably safe.

@BrzVlad BrzVlad closed this May 5, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variant static interface dispatch behaves incorrectly on Mono

3 participants