Giter Site home page Giter Site logo

StaticAbstractMembersInInterfaces - Roslyn does not warn that a most-derived static interface method implementation will not be used about roslyn HOT 8 CLOSED

daiplusplus avatar daiplusplus commented on July 1, 2024
StaticAbstractMembersInInterfaces - Roslyn does not warn that a most-derived static interface method implementation will not be used

from roslyn.

Comments (8)

CyrusNajmabadi avatar CyrusNajmabadi commented on July 1, 2024

I expected the call to T.DoSomething() in GenericMethod(); to be routed to static void SubclassAB.DoSomething() but instead it's routed to SubclassA..

Because you created a method unrelated to the interface. It's not an interface impl, and it's def in its own slot (which is why if you remove new we'll warn you of that).

I assume the reason is that the generic-method-instantiation logic in the CLR performs a top-down search for binding static methods - instead of bottom-up

Neither of these are incorrect. This is a non-virtual method. So there is no 'search'ing happening at all. The method being called is determined statically at compile time and the CLR just invokes the method requested by you.

in which case I expect Roslyn to raise a diagnostic (i.e. a warning) at the GenericMethod(); call-site, with a message like: "Static methods in SubclassAB will not be invoked.

We can't warn here. This is an entirely reasonable piece of code to write. If you'd like a custom warning here, you'd need to write your own analyzer.

both with and without the new modifier

The 'new' modifier does not impact semantics at all. it's just a way for a user to say they understand that one method hides another, and has no virtual inheritance relationship with it. This is often a bug, so new is needed to indicate that "yes, that hiding was intended".

from roslyn.

daiplusplus avatar daiplusplus commented on July 1, 2024

So there is no 'search'ing happening at all.

The JIT surely has to search from SubclassAB to ClassA to find the IMyInterface implementation and bind the static call-sitesa, no?

This is an entirely reasonable piece of code to write

Why is it reasonable to specify a more-derived type as a type-parameter when the effect is the same as the less-derived type? When would anyone do that?

The 'new' modifier does not impact semantics at all.

Right - I wasn't suggesting that it does - but I think it's odd that shadow methods were deemed confusing enough to warrant a compiler warning, but subclassed static interface methods were not. It just feels abitrary to me.

...especially as this isn't documented at all right now. Hence it took me by surprise.

from roslyn.

CyrusNajmabadi avatar CyrusNajmabadi commented on July 1, 2024

Why is it reasonable to specify a more-derived type as a type-parameter

For any number of reasons. Including that the code in question might care about that type-parameter versus the base type.

when the effect is the same as the less-derived type?

There's no concept of "the effect is hte same". The compiler has no idea what happens inside a method body (remember that they can come from metadata, and we want uniform compilation behavior for source vs metadata).

from roslyn.

CyrusNajmabadi avatar CyrusNajmabadi commented on July 1, 2024

The JIT surely has to search from SubclassAB to ClassA to find the IMyInterface implementation and bind the static call-sitesa, no?

For the static-virtuals, yes. For the non-virtuals, no. If you use static-virtuals (and have a more derived impl, it will be called).

from roslyn.

daiplusplus avatar daiplusplus commented on July 1, 2024

If you use static-virtuals (and have a more derived impl, it will be called).

So regardless of the use of static abstract members, given a class A : IInterface and a class B : A, the class B is not considered a more-derived-implementation of IInterface because only A implements IInterface unless it's class B : A, IInterface?

from roslyn.

CyrusNajmabadi avatar CyrusNajmabadi commented on July 1, 2024

Correct. And we even warn you about that. but you suppressed the warning by putting 'new' on teh method :)

from roslyn.

daiplusplus avatar daiplusplus commented on July 1, 2024

Correct.

Thank you, that does clarify and explain things.

And we even warn you about that. but you suppressed the warning by putting 'new' on teh method :)

The new warning is the same for both class SubclassAB and class SubclassABI though.

warning CS0108: 'SubclassAB.DoSomething()' hides inherited member 'ClassA.DoSomething()'. Use the new keyword if hiding was intended.
warning CS0108: 'SubclassABI.DoSomething()' hides inherited member 'ClassA.DoSomething()'. Use the new keyword if hiding was intended.

...but only SubclassABI.DoSomething() is considered an implementation of IMyInterface.DoSomething.

from roslyn.

CyrusNajmabadi avatar CyrusNajmabadi commented on July 1, 2024

yup. :)

from roslyn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.