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

CS1666 when creating a span from a stack-only buffer #57583

Closed
koszeggy opened this issue Nov 4, 2021 · 1 comment
Closed

CS1666 when creating a span from a stack-only buffer #57583

koszeggy opened this issue Nov 4, 2021 · 1 comment
Labels
Area-Compilers Resolution-Duplicate The described behavior is tracked in another issue

Comments

@koszeggy
Copy link

koszeggy commented Nov 4, 2021

Reproduction:

private unsafe ref struct MyRefStruct
{
    private readonly BinaryWriter writer;
    private int bufferLength;

    // we are in a ref struct so this never goes on the heap
    private fixed byte buffer[255];

    private void Dump()
    {
        Debug.Assert(bufferLength is > 0 and <= 255);
        writer.Write((byte)bufferLength);

        // CS1666: You cannot use fixed size buffers contained in unfixed expressions.
        //writer.Write(new ReadOnlySpan<byte>(buffer, bufferLength));

        // However, this works without a fixed context, even in a non-ref struct
        for (int i = 0; i < bufferLength; i++)
            writer.Write(buffer[i]);

        bufferLength = 0;
    }

    // other members here
}

Expected Behavior:

Being in a ref struct, the fixed buffer never gets relocated so creating the span should work.

Remark:

Maybe related: #39632

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 4, 2021
@jaredpar
Copy link
Member

jaredpar commented Nov 8, 2021

Closing as a duplicate of dotnet/csharplang#1792

The compiler is operating to the language spec today. We need to change the language design first before we can change the compiler to take advantage of this behavior.

@jaredpar jaredpar closed this as completed Nov 8, 2021
@jaredpar jaredpar added Resolution-Duplicate The described behavior is tracked in another issue and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

2 participants