Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for unsupported, runtime-dependent features. #47

Open
AvremelM opened this issue Jan 16, 2023 · 12 comments
Open

Documentation for unsupported, runtime-dependent features. #47

AvremelM opened this issue Jan 16, 2023 · 12 comments
Labels
documentation Improvements or additions to documentation

Comments

@AvremelM
Copy link

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

@DreadLordMikey
Copy link

DreadLordMikey commented Jan 16, 2023

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];

@Sergio0694
Copy link
Owner

Sergio0694 commented Jan 16, 2023

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 Sergio0694 added the documentation Improvements or additions to documentation label Jan 16, 2023
@DreadLordMikey
Copy link

DreadLordMikey commented Jan 16, 2023

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

@Sergio0694
Copy link
Owner

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 🙂

@AvremelM
Copy link
Author

AvremelM commented Jan 17, 2023

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?

@Balinth
Copy link

Balinth commented Jan 17, 2023

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.

@GetCurious
Copy link

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

@Sergio0694
Copy link
Owner

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

@GetCurious
Copy link

GetCurious commented Feb 21, 2023

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

@AvremelM
Copy link
Author

AvremelM commented May 17, 2023

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

@skarllot
Copy link

skarllot commented Jul 29, 2023

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

@YoshiRulz
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

7 participants