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

error CS8640: Expression tree cannot contain value of ref struct or restricted type 'ReadOnlySpan' too restrictive #31957

Open
Tornhoof opened this issue Dec 20, 2018 · 2 comments
Labels
Area-Compilers Feature Request New Language Feature - Ref Locals and Returns Ref Locals and Returns Tenet-Compatibility Violation of forwards/backwards compatibility in a design-time piece.
Milestone

Comments

@Tornhoof
Copy link

Version Used:
3.0.100-preview-009844
Steps to Reproduce:
In Preview1 and in previous versions the following code snippet produced a valid MethodInfo for the generic static method with a byref struct, since #30776 it does not compile anymore.

Expression<Action> functor = () => MemoryMarshal.AsBytes(new ReadOnlySpan<char>());
var asBytesMethodInfo = (functor.Body as MethodCallExpression).Method;

Motivation:
As many probably know, finding the correct MethodInfo for reflection (or in this case Expression Trees in SpanJson) for extension methods and/or methods with generic arguments is a bit of work, especially if the there are multiple overloads with the same name (LINQ's IEnumerable...)
One of the more generally accepted shortcuts is to use an expression tree to describe the method and obtain the MethodInfo like above. This works nicely with pretty much everything.
Unfortunately in #30776 you now prevent this via the following compilation error:
error CS8640: Expression tree cannot contain value of ref struct or restricted type 'ReadOnlySpan'.

Isn't there maybe a less restrictive bugfix which still allows the above way to work, as the above Expression is never executed or compiled?

Expected Behavior:
I get the MethodInfo for AsBytes for the generic argument charand the parameter ReadOnlySpan<char>.
Actual Behavior:
error CS8640: Expression tree cannot contain value of ref struct or restricted type 'ReadOnlySpan'.

@Tornhoof Tornhoof changed the title error CS8640: Expression tree cannot contain value of ref struct or restricted type 'ReadOnlySpan' to restrictive error CS8640: Expression tree cannot contain value of ref struct or restricted type 'ReadOnlySpan' too restrictive Dec 20, 2018
@AArnott
Copy link
Contributor

AArnott commented Dec 27, 2018

This compile error blocks me from building my https://github.com/aarnott/nerdbank.streams repo with VS 2019.

@LeroyK
Copy link

LeroyK commented Apr 24, 2019

I am running into this issue as well. This expression compiles in VS2017 15.9.11, but fails to compile in VS 2019 16.1 Preview 2:

x => x.AsSpan().ToArray()

@gafter gafter added this to the Compiler.Next milestone Jul 14, 2019
@gafter gafter added the Tenet-Compatibility Violation of forwards/backwards compatibility in a design-time piece. label Jul 14, 2019
@jaredpar jaredpar modified the milestones: Compiler.Next, Backlog Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature Request New Language Feature - Ref Locals and Returns Ref Locals and Returns Tenet-Compatibility Violation of forwards/backwards compatibility in a design-time piece.
Projects
None yet
Development

No branches or pull requests

6 participants