PolySharp icon indicating copy to clipboard operation
PolySharp copied to clipboard

Documentation for unsupported, runtime-dependent features.

Open AvremelM opened this issue 2 years ago • 12 comments

Is there any documentation here (or elsewhere) on which C# language features depend on runtime support, and therefore can never be polyfilled?

I know the readme mentions static abstract members, and I'm pretty sure default interface methods are out too.

It would be helpful to have some list of the features that we should not expect this project to ever be able to polyfill, (if only to preempt people opening feature requests for them 😆).

AvremelM avatar Jan 16 '23 16:01 AvremelM

Default interface implementations are definitely out. I'm working on a git repo now that tests each C# language feature from 8.0 forward to test which polyfills are available. (The project targets .NET 4.7.2 with C# 11).

It looks like ranges are also not supported. When I attempt to use them, I am presented with the following error:

CS0656: Missing compiler required member 'System.Runtime.CompilerServices.RuntimeHelpers.GetSubArray.'

I am unable to add a direct reference to System.Runtime to the project, and there's already a reference to System.Runtime.CompilerServices.Unsafe.

The code I'm using comes directly from Microsoft's documentation and is as follows:

int[] numbers = new[] { 0, 10, 20, 30, 40, 50 };
int start = 1;
int amountToTake = 3;
int[] subset = numbers[start..(start + amountToTake)];

Oddly, the index from end operator works just fine. For example, the code below compiles and runs without error:

int[] xs = new[] { 0, 10, 20, 30, 40 };
int last = xs[^1];

DreadLordMikey avatar Jan 16 '23 23:01 DreadLordMikey

Ranges are only not supported when slicing arrays. If you try to slice eg. a Span<T>, they'll work just fine. In general, they'll work any time you're trying to slice a type that exposes a count (Length or Count) and a Slice(int, int) method. This also includes through extension methods, so you can also make this work with your own types, if you wanted (or you could enable this for other types if you write an extension method targeting that type).

Sergio0694 avatar Jan 16 '23 23:01 Sergio0694

Sergio - Thank you. I'll note that in the git repo I'm working on. (I'm trying to convince our company to use the package.)

DreadLordMikey avatar Jan 16 '23 23:01 DreadLordMikey

I will also say, from the point of view of users, it's actually pretty straightforward. If something can be used, it'll just work. If something requires runtime support and that's not available, Roslyn will just emit an error with a nice explanation about it 🙂

Sergio0694 avatar Jan 16 '23 23:01 Sergio0694

Sergio: Hmm, I see that that actually does work. Thanks! Still, would be good to have a list, even if only a partial list that can be updated over time.


[CS8701] Target runtime doesn't support default interface implementation.

Actually, I wonder how that functions for class libraries targeting .NET Standard 2.0 (which obviously isn't, itself, a runtime). Is Roslyn just smart enough to know that no runtime that implements .NET Standard 2.0 also supports default interface implementations?

AvremelM avatar Jan 17 '23 14:01 AvremelM

Actually, I wonder how that functions for class libraries targeting .NET Standard 2.0 (which obviously isn't, itself, a runtime). Is Roslyn just smart enough to know that no runtime that implements .NET Standard 2.0 also supports default interface implementations?

.Net Standard is a "contract" for runtimes. All runtimes that support default inheritance also support the .Net Standard 2.0 API-s, but not all runtimes that support .Net Standard 2.0 have default interface implementations. Hence Roslyn is careful enough to not allow using default interface implementations if the target is .Net Standard 2.0.

Great library btw, makes one wonder why Microsoft didn't bother with something like this.

Balinth avatar Jan 17 '23 18:01 Balinth

I couldnt get expression-bodied property to work on a legacy project using .Net Framework 4.0 I assume it is not doable?

GetCurious avatar Feb 21 '23 14:02 GetCurious

Those will work just fine, but you need to set the C# language version to at least C# 7 🙂

Sergio0694 avatar Feb 21 '23 14:02 Sergio0694

I did. This simple refactor broke in runtime. image

    public bool IsReusable => false;

    // public bool IsReusable
    // {
    //     get { return false; }
    // }

EDIT: i think it's is because the file i refactored was a .ashx and not .cs

GetCurious avatar Feb 21 '23 14:02 GetCurious

@GetCurious That should still work, I think, if you're have the latest Microsoft.CodeDom.Providers.DotNetCompilerPlatform package installed and configured in your web.config's <system.codedom>. (You might possibly need to explicitly set /langversion:7 in the compilerOptions attribute.)

But that's all out of the scope of PolySharp, afaik.

AvremelM avatar May 17 '23 20:05 AvremelM

It looks like ranges are also not supported. When I attempt to use them, I am presented with the following error:

CS0656: Missing compiler required member 'System.Runtime.CompilerServices.RuntimeHelpers.GetSubArray.'

@DreadLordMikey Try creating the following class in your project: https://gist.github.com/bgrainger/fb2c18659c2cdfce494c82a8c4803360

skarllot avatar Jul 29 '23 13:07 skarllot

Feel free to use my notes as a base for this.

YoshiRulz avatar Oct 10 '23 00:10 YoshiRulz