Skip to content

Commit 90d9034

Browse files
authored
[metadata] Skip null vtable entries when checking covariant return overrides (#76323)
* Add test for covariant reabstraction * [metadata] Skip null vtable entires when checking covariant overrides When there are covariant return overrides, we check the vtable slot in every parent class for signature compatability with the proposed override. However if one of the ancestor classes is abstract, it could have an abstract override for hte method: class Base { public virtual Base Method() => this; } public abstract Intermediate : Base { public override abstract Base Method(); } public Leaf : Intermediate { public override Leaf Method() => this; } In this case when we're checking that Leaf.Method is compatible with the vtable slot in Intermediate, we will see a null method (since Intermediate is abstract). In such cases we can skip over the class and continue with its parent. Fixes #76312
1 parent c8f9f29 commit 90d9034

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/mono/mono/metadata/class-setup-vtable.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,10 @@ check_vtable_covariant_override_impls (MonoClass *klass, MonoMethod **vtable, in
16561656
break;
16571657
MonoMethod *prev_impl = cur_class->vtable[slot];
16581658

1659+
// if the current class re-abstracted the method, it may not be there.
1660+
if (!prev_impl)
1661+
continue;
1662+
16591663
if (prev_impl != last_checked_prev_override) {
16601664
/*
16611665
* the new impl should be subsumed by the prior one, ie this
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
6+
7+
namespace ReproMAUI6811;
8+
9+
public static class Program
10+
{
11+
public static int Main()
12+
{
13+
Leaf l = new Leaf();
14+
15+
if (l.getI().ToString() != "Leaf")
16+
return 1;
17+
if (((Intermediate)l).getI().ToString() != "Leaf")
18+
return 2;
19+
if (((PseudoBase)l).getI().ToString() != "Leaf")
20+
return 3;
21+
if (((Base)l).getI().ToString() != "Leaf")
22+
return 4;
23+
return 100;
24+
}
25+
}
26+
27+
public abstract class Base {
28+
public abstract I getI();
29+
}
30+
31+
public class PseudoBase : Base {
32+
public override I getI() => new C ("PseudoBase");
33+
}
34+
35+
public abstract class Intermediate : PseudoBase {
36+
public override abstract I getI();
37+
}
38+
39+
public class Leaf : Intermediate {
40+
public Leaf() {}
41+
public override C getI() { return new C ("Leaf"); }
42+
}
43+
44+
public interface I {}
45+
46+
public class C : I {
47+
private readonly string _repr;
48+
public C(string s) { _repr = s; }
49+
public override string ToString() => _repr;
50+
}
51+
52+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<Compile Include="$(MSBuildProjectName).cs" />
7+
</ItemGroup>
8+
</Project>

0 commit comments

Comments
 (0)