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

Remove constraints on stackalloc (support new locations) #1211

Open
wants to merge 4 commits into
base: draft-v8
Choose a base branch
from

Conversation

gafter
Copy link
Member

@gafter gafter commented Nov 20, 2024

No description provided.

@gafter gafter added the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Nov 20, 2024
@gafter gafter marked this pull request as ready for review November 20, 2024 18:27
@gafter gafter added this to the C# 8.0 milestone Nov 20, 2024
@gafter
Copy link
Member Author

gafter commented Nov 20, 2024

This change reflects the following behavior.

This is illegal:

    public void M() {
        var p = stackalloc nint[3];
    }

Because a stackalloc expression used as an initializer is of type T* (an unsafe pointer type) which requires an unsafe context. However, these are legal:

    public void M() {
        Span<nint> p = stackalloc nint[3];
    }

and

    public void M() {
        var p = (stackalloc nint[3]);
    }

(Note the addition of parens)

@gafter
Copy link
Member Author

gafter commented Nov 20, 2024

I don't understand the following behavior:

This is illegal:

    public void M() {
        var p = stackalloc nint[3][1];
    }

I don't understand why. Perhaps because `nint[3] is parsed as a type. I get the error

•	error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('nint[]')
•	error CS1575: A stackalloc expression requires [] after type

Yet this is legal:

    public void M() {
        var p = (stackalloc nint[3])[1];
    }

@gafter gafter changed the title Remove constraints on stackalloc Remove constraints on stackalloc (support new locations) Nov 20, 2024
@gafter gafter added the Review: pending Proposal is available for review label Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meeting: discuss This issue should be discussed at the next TC49-TG2 meeting Review: pending Proposal is available for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants