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

Improve experience when using stack-only types in interactive code #40213

Open
tmat opened this issue Dec 6, 2019 · 3 comments
Open

Improve experience when using stack-only types in interactive code #40213

tmat opened this issue Dec 6, 2019 · 3 comments

Comments

@tmat
Copy link
Member

tmat commented Dec 6, 2019

This doesn't work since script variables are not stack allocated:

> var span = new byte[] { 1, 2, 3 }.AsSpan();
(1,1): error CS8345: Field or auto-implemented property cannot be of type 'Span<byte>' unless it is an instance member of a ref struct.

This is by design, the message could be better though (something like Script variables cannot be of type 'Span<byte>'.).

Similarly for ref locals:

> ref int x;
(1,10): error CS1003: Syntax error, '(' expected
(1,10): error CS1026: ) expected
> 

We should parse the declaration and report semantic error.

This doesn't work since top-level code is implicitly async:

> {
.     var span = new byte[] { 1, 2, 3 }.AsSpan();
. }
(2,5): error CS4012: Parameters or locals of type 'Span<byte>' cannot be declared in async methods or lambda expressions.

Is there a reason why this can't be allowed for variables that do not need to be hoisted? Is it just to simplify handling of these cases?

If we don't make this work we should report a better error message (e.g. Locals of type 'Span<byte>' cannot be declared outside of methods in scripts.).

BTW, one can still work with spans in interactive, they just need to be declared within a method:

> void F()
. {
.     var span = new byte[] { 1, 2, 3 }.AsSpan();
. 
. }
@tmat tmat added this to the Backlog milestone Dec 6, 2019
@tmat
Copy link
Member Author

tmat commented Dec 6, 2019

@agocke

Is there a reason why this can't be allowed for variables that do not need to be hoisted?

@tmat
Copy link
Member Author

tmat commented Dec 12, 2019

@MadsTorgersen FYI

@tmat
Copy link
Member Author

tmat commented Apr 15, 2024

Partially addressed by #72664

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

No branches or pull requests

2 participants