Skip to content

C#: Support for virtual dispatch for operators.#12200

Merged
michaelnebel merged 6 commits into
github:mainfrom
michaelnebel:csharp/viablestatic
Mar 16, 2023
Merged

C#: Support for virtual dispatch for operators.#12200
michaelnebel merged 6 commits into
github:mainfrom
michaelnebel:csharp/viablestatic

Conversation

@michaelnebel

@michaelnebel michaelnebel commented Feb 16, 2023

Copy link
Copy Markdown
Contributor

In this PR, we

  • Fix issue with dispatch to implementations of virtual interface members for generics.
  • Make support for virtual dispatch of operators (Operators are now considered Overridables).

From C# 11 it is possible to declare abstract and virtual operators (see the example below), which means that operator calls no longer only have static call targets. We have implemented support for dynamic call targeting for operators in C#.

public interface I<T> where T : I<T>
{
    static abstract T operator +(T x, T y);
    static virtual T operator -(T x, T y) => throw null;
}

public class C : I<C>
{
    public static C operator +(C x, C y) => throw null;
    public static C operator /(C x, C y) => throw null;
}

@github-actions github-actions Bot added the C# label Feb 16, 2023
@michaelnebel michaelnebel force-pushed the csharp/viablestatic branch 2 times, most recently from 0818067 to 8f1b5f9 Compare March 13, 2023 12:31
Comment thread csharp/ql/lib/semmle/code/csharp/dispatch/Dispatch.qll Fixed
@michaelnebel michaelnebel changed the title C#: Dispatch call improvements. C#: Support for virtual dispatch for operators. Mar 13, 2023
@michaelnebel michaelnebel added the no-change-note-required This PR does not need a change note label Mar 13, 2023
@michaelnebel

Copy link
Copy Markdown
Contributor Author

DCA looks good.

@michaelnebel michaelnebel marked this pull request as ready for review March 14, 2023 07:39
@michaelnebel michaelnebel requested a review from a team as a code owner March 14, 2023 07:39
@michaelnebel michaelnebel requested a review from hvitved March 14, 2023 07:39

@hvitved hvitved left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@michaelnebel michaelnebel merged commit a9e5b34 into github:main Mar 16, 2023
@michaelnebel michaelnebel deleted the csharp/viablestatic branch March 16, 2023 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C# no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants