MichalStrehovsky / iltrim

MIT License
9 stars 1 forks source link

Keep virtual methods which are required by the base class #64

Open vitek-karas opened 2 years ago

vitek-karas commented 2 years ago

The logic which keeps virtual methods if the type is constructed and if the virtual method is used only works on constructed types (as expected). But if there's a non-constructed type which implements an abstract method, such method has to be kept in order for the metadata to be consistent. Currently we don't keep such a method.

To test, modify the VirtualMethods test to look like this:

    [Kept]
    public class VirtualMethods
    {
        [Kept]
        static void Main()
        {
            BaseType b = new DerivedType();
            b.Method1();

            NonConstructedType.DoSomethingStatic();
        }
    }

    [Kept]
    abstract class BaseType
    {
        [Kept]
        public BaseType() { }
        [Kept]
        public abstract void Method1();
        public abstract void Method2();
    }

    [Kept]
    [KeptBaseType(typeof(BaseType))]
    class DerivedType : BaseType
    {
        [Kept]
        public DerivedType() { }
        [Kept]
        public override void Method1() { }
        public override void Method2() { }
    }

    [Kept]
    [KeptBaseType(typeof(BaseType))]
    class NonConstructedType : BaseType
    {
        [Kept]
        public static void DoSomethingStatic() { }

        [Kept] // <- This fails - we don't keep Method1, even though we do keep the abstract method on the base class
        public override void Method1() { }
        public override void Method2() { }
    }

/cc @MichalStrehovsky

MichalStrehovsky commented 2 years ago

I know IL Linker does this, but I've been wondering whether this could also be fixed by turning on the abstract bit on types we don't see constructed.

vitek-karas commented 2 years ago

hmm - that is an interesting idea ... something like "reverse sealer" :wink: