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

Champion "Permit stackalloc in nested expressions" (16.3, Core 3) #1412

Open
3 of 5 tasks
Tracked by #829 ...
gafter opened this issue Mar 23, 2018 · 3 comments
Open
3 of 5 tasks
Tracked by #829 ...

Champion "Permit stackalloc in nested expressions" (16.3, Core 3) #1412

gafter opened this issue Mar 23, 2018 · 3 comments
Assignees
Labels
Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification Proposal champion Proposal Smallish Feature
Milestone

Comments

@gafter
Copy link
Member

gafter commented Mar 23, 2018

  • Proposal added
  • Discussed in LDM
  • Decision in LDM
  • Finalized (done, rejected, inactive)
  • Spec'ed

The Roslyn compilers have the machinery to rewrite expressions so that they occur in non-nested contexts if necessary. We use that mechanism as part of the translation of await. We could use it to easily permit stackalloc in contexts where they are not simply the initializer of a local declaration.

The specification would be something like:

Stack allocation

We modify the section Stack allocation of the C# language specification to relax the places when a stackalloc expression may appear. We delete

local_variable_initializer_unsafe
    : stackalloc_initializer
    ;

stackalloc_initializer
    : 'stackalloc' unmanaged_type '[' expression ']'
    ;

and replace them with one production in which the left-hand-side is primary_no_array_creation_expression and the right-hand-side is the stackalloc expression.

primary_no_array_creation_expression
    : 'stackalloc' unmanaged_type '[' expression ']'
    ;

Other changes to the stackalloc expression that occurred in C# 7.3 should be reflected as well.

@alrz
Copy link
Member

alrz commented Mar 24, 2018

this would be the groundwork for a hypothetical params Span<T> feature. We will need to spill the stack before we emit stackalloc for a params Span<T>.

@MadsTorgersen MadsTorgersen added this to the 8.0 candidate milestone Mar 28, 2018
@gafter
Copy link
Member Author

gafter commented Jul 27, 2018

@alrz I think params Span<T> would be inherently dangerous. Just using such an API inside a loop would likely cause stack overflow. That makes me doubt we'd add it as a language feature.

@gafter
Copy link
Member Author

gafter commented Jul 30, 2018

On second thought, we could reuse the same Span for the same call point in the code in subsequent iterations of the loop.

@jcouv jcouv changed the title Champion "Permit stackalloc in nested expressions" Champion "Permit stackalloc in nested expressions" (16.3, Core 3) Jul 18, 2019
@333fred 333fred added the Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification label Oct 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification Proposal champion Proposal Smallish Feature
Projects
None yet
Development

No branches or pull requests

4 participants