dotnet / dotnet/csharpstandard

23.4 Fixed and moveable variables

Open
#190 2 comments 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

The language spec (23.4 Fixed and moveable variables) says

> In precise terms, a fixed variable is one of the following:
> - A variable resulting from a simple-name (§7.6.2) that refers to a local variable or a value parameter, unless the variable is captured by an anonymous function.
> - A variable resulting from a member-access (§7.6.4) of the form V.I, where V is a fixed variable of a struct-type.
> - A variable resulting from a pointer-indirection-expression (§18.5.1) of the form *P, a pointer-member-access (§18.5.2) of the form P->I, or a pointer-element-access (§18.5.3) of the form P[E].
>
> All other variables are classified as moveable variables.

But testing this on the following program

```csharp
using System;
unsafe struct S
{
public fixed int buffer[1];
public int i;
}

unsafe class Test
{
private void example1()
{
S data = new S();
Func lambda = () => data;
fixed (int* p = &data.i) // ok; data is captured
{
}
fixed (int* p = data.buffer) // ok; data is captured
{
}
}
}
```

We find that both the native compiler and Roslyn reject both attempts, complaining that you can't take the address of a variable that has been captured by a lambda, AND that you can't use the fixed statement to take the address of an already fixed expression.

We should probably modify the language spec to agree with the implementation.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.