Repository navigation
Description
This new attribute will be used by the covariant return types and records C# language features posed for C# 9.0.
Today when we have an explicit override to a method (using MethodImpls), this override gets a new vtable slot on the type, and the slot for the overriden method gets updated to point to the new overriding method. Example:
// Pseudo code with some IL
class A {
void VirtualFunction() { }
}
class B : A {
void VirtualFunction() { .override A.VirtualFunction }
}
// VTable for type A:
// [0]: A.VirtualFunction
// VTable for type B:
// [0]: B.VirtualFunction
// [1]: B.VirtualFunctionNow when a new derived type explicitly overrides A.VirtualFunction, it would also get a new vtable slot, and only the vtable slot of the method being overriden gets updated with the new override. Example:
class C : B {
void VirtualFunction() { .override A.VirtualFunction }
}
// VTable for type A:
// [0]: A.VirtualFunction
//
// VTable for type B:
// [0]: B.VirtualFunction
// [1]: B.VirtualFunction
//
// VTable for type C:
// [0]: C.VirtualFunction
// [1]: B.VirtualFunction -> This slot does not get overriden
// [2]: C.VirtualFunctionWith the new records and covariant return feature, a change of behavior is required to vtable slot overrides when a method gets explictly overriden. In the example above, the second slot on type C would also need to be updated to point to the latest override of the method:
// VTable for type C:
// [0]: C.VirtualFunction
// [1]: C.VirtualFunction -> This slot needs to be updated as well
// [2]: C.VirtualFunctionThe reason for this change is to ensure that any virtual call to the method, whether it uses the base signature or derived signature of the method, we always execute the most derived override:
// Given an object of type C:
callvirt A.VirtualFunction -> Executes C.VirtualFunction
callvirt B.VirtualFunction -> Executes C.VirtualFunction
callvirt C.VirtualFunction -> Executes C.VirtualFunctionTo achieve this, a new attribute will be introduced and applied to the MethodImpls that need to propagate to all applicable vtable slots: RequireMethodImplToRemainInEffect attribute. Example:
class A {
void VirtualFunction() { }
}
class B : A {
void VirtualFunction() { .override A.VirtualFunction }
}
class C : B {
[RequireMethodImplToRemainInEffect]
void VirtualFunction() { .override A.VirtualFunction }
}This attribute will only be applicable to methods with explicit overrides (MethodImpls), and takes no arguments. The proposed namespace for this attribute is the System.Runtime.CompilerServices namespace. Here is the proposed implementation of the attribute:
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class RequireMethodImplToRemainInEffectAttribute : Attribute
{
}
}
Activity
fadimounir commentedon Apr 23, 2020
Quick clarification as well: we do not expect any developer to directly use this attribute. It will be placed by the Roslyn compiler appropriately on methods.
[-]ValidateMethodImplRemainsInEffectAttribute[/-][+]RequireMethodImplToRemainInEffectAttribute[/+]fadimounir commentedon Apr 28, 2020
cc @dotnet/crossgen-contrib
davidwrighton commentedon Apr 29, 2020
@vargaz @lambdageek should be looped in on this issue.
fadimounir commentedon May 5, 2020
cc @janvorli
lambdageek commentedon May 15, 2020
We will need to teach the IL linker to keep this attribute /cc @vitek-karas
MichalStrehovsky commentedon May 15, 2020
IL Linker will not strip attributes it knows nothing about because it would be a bug farm. dotnet/linker#952 (comment)
terrajobst commentedon May 26, 2020
Video
A few questions:
voidvirtualfunctionsRequireMethodImplToRemainInEffectAttributethis name seems a bit abstract. Can make this a bit clearer, likePreserveBaseOverridesAttribute?MethodImplAttributes?davidwrighton commentedon May 26, 2020
PreserveBaseOverridesAttributenaming to be reasonable as well, and if that makes more sense to the api review board, I'm all for it.TestClassDerived2to override the originalMethod()onTestClassWithVirtualMethod.1 remaining item
MichalStrehovsky commentedon May 27, 2020
We don't have bits in the MethodAttribute column anymore, unless we want to do creative combinations (
mdVirtual+mdUnmanagedExport?), or rev the file format version and reinterpret legacy useless bits differently (mdHasSecurityormdRequireSecObject).Doesn't the same concern apply to the custom attribute? Roslyn already enfoces that user can't set bits in the MethodImpl flag (e.g. you can't set bit 1 and say the method body is native code).
We'll need to touch ILASM/ILDASM for covariant returns anyway: #37045.
terrajobst commentedon May 29, 2020
Video
Let's go with
PreserveBaseOverridesAttributethen.rename to PreserveBaseOverridesAttribute
[mono][metadata] Add a method custom attribute scanning function
[mono][metadata] Add a method custom attribute scanning function (#19880
[mono][metadata] Add a method custom attribute scanning function (#37172
jkotas commentedon Jul 29, 2020
Fixed by #35308