Skip to content

RequireMethodImplToRemainInEffectAttribute #35315

Closed

Description

@fadimounir

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.VirtualFunction

Now 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.VirtualFunction

With 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.VirtualFunction

The 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.VirtualFunction

To 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

  1. added
    blocking
    on Apr 22, 2020
  2. added
    untriaged
    on Apr 22, 2020
  3. fadimounir commented on Apr 23, 2020

    @fadimounir
    ContributorAuthor

    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.

  4. changed the title [-]ValidateMethodImplRemainsInEffectAttribute[/-] [+]RequireMethodImplToRemainInEffectAttribute[/+] on Apr 28, 2020
  5. fadimounir commented on Apr 28, 2020

    @fadimounir
    ContributorAuthor

    cc @dotnet/crossgen-contrib

  6. davidwrighton commented on Apr 29, 2020

    @davidwrighton
    Member

    @vargaz @lambdageek should be looped in on this issue.

  7. fadimounir commented on May 5, 2020

    @fadimounir
    ContributorAuthor
  8. lambdageek commented on May 15, 2020

    @lambdageek
    Member

    We will need to teach the IL linker to keep this attribute /cc @vitek-karas

  9. MichalStrehovsky commented on May 15, 2020

    @MichalStrehovsky
    Member

    We will need to teach the IL linker to keep this attribute /cc @vitek-karas

    IL Linker will not strip attributes it knows nothing about because it would be a bug farm. dotnet/linker#952 (comment)

  10. added
    api-needs-work
    and removed on May 26, 2020
  11. terrajobst commented on May 26, 2020

    @terrajobst
    Contributor

    Video

    A few questions:

    • We're not entirely sure what this attribute does:
      • The issue talks about covariant returns but all samples use void
      • None of the methdos use virtual functions
      • What are the behavior differences of with and without the attribute? If this can't be expressed in C#, then ILDASM code would help too
    • RequireMethodImplToRemainInEffectAttribute this name seems a bit abstract. Can make this a bit clearer, like PreserveBaseOverridesAttribute?
    • Does it have to be a new attribute or could be a new flag on MethodImplAttributes?
    • How does this attribute play with default interface methods?
    • How does this play with versioning where overrides are added to the middle of the hierarchy later?
  12. davidwrighton commented on May 26, 2020

    @davidwrighton
    Member
    • This is a feature to adjust the behavior of MethodImpl records, but really this is designed to support the covariant scenario. The reason that the examples are written in terms of void is to emphasize that this is an independent feature at the runtime level from covariant returns, although it is likely that in the near term, it will only be used by the C# compiler when compiling covariant return overrides.
    • The name was written as a placeholder as I am notoriously terrible at naming stuff. I find the PreserveBaseOverridesAttribute naming to be reasonable as well, and if that makes more sense to the api review board, I'm all for it.
    • It does not have to be a new attribute. A new flag MethodImpl would also work if the question were merely about changes in the file format. However, we need to be slightly careful with that as it needs to be hidden from the C# developer, as the flag is NOT something that is acceptable to adjust by setting a flag on the attribute from the C# language. It would need to be set by the compiler itself. Also, using a MethodImpl flag would imply that we need to update ildasm, and ilasm, which otherwise do not require adjustment. Overall, a MethodImpl flag would be "fine".
    • This only applies to MethodImpl records used to override a class virtual method with another class virtual method. It does not interact with default virtual methods.
    • This method actually enhances that experience. It allows there to be considered a single slot that defines how the override works, and changes the override logic so that the covariance will be guaranteed to exist thus preventing a non-covariant override by a further derived type. For instance, consider the following. The new attribute is used to cause the runtime to produce a type load exception instead of allowing the TestClassDerived2 to override the original Method() on TestClassWithVirtualMethod.
    class BaseType {}
    class DerivedType : BaseType {}
    
    class TestClassWithVirtualMethod
    {
        public virtual BaseType Method() {}
    }
    class TestClassDerived1 : TestClassWithVirtualMethod
    {
        public override DerivedType Method() {}
    }
    class TestClassDerived2 : TestClassDerived1
    {
    // ERROR!, TypeLoadException!!
        public override BaseType Method() {}
    }
    
  13. 1 remaining item

  14. MichalStrehovsky commented on May 27, 2020

    @MichalStrehovsky
    Member

    If we choose to encode this in metadata, it should be a bit in the MethodAttributes column

    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 (mdHasSecurity or mdRequireSecObject).

    However, we need to be slightly careful with that as it needs to be hidden from the C# developer, as the flag is NOT something that is acceptable to adjust by setting a flag on the attribute from the C# language. It would need to be set by the compiler itself

    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).

    Also, using a MethodImpl flag would imply that we need to update ildasm, and ilasm, which otherwise do not require adjustment.

    We'll need to touch ILASM/ILDASM for covariant returns anyway: #37045.

  15. added
    api-approved
    and removed
    api-needs-work
    blocking
    on May 29, 2020
  16. terrajobst commented on May 29, 2020

    @terrajobst
    Contributor

    Video

    Let's go with PreserveBaseOverridesAttribute then.

    namespace System.Runtime.CompilerServices
    {
        [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
        public sealed class PreserveBaseOverridesAttribute : Attribute
        {
        }
    }
  17. removed
    untriaged
    on Jul 6, 2020
  18. added this to the Future milestone on Jul 6, 2020
  19. jkotas commented on Jul 29, 2020

    @jkotas
    Member

    Fixed by #35308

  20. ghost locked as resolved and limited conversation to collaborators on Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Runtime

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions