You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The language spec (18.3 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
using System;unsafestructS{publicfixedintbuffer[1];publicinti;}unsafeclassTest{privatevoidexample1(){Sdata=new S();Func<S>lambda=()=> data;
fixed (int*p=&data.i)// ok; data is captured{}
fixed (int*p= data.buffer)// ok; data is captured{}}}
We find that 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.
Why does the language forbid using a fixed statement for a fixed variable? It would just be a no-op. Allowing it would sidestep having to specify which variables are captured in an async method. We could just say that there are no fixed variables in an async method, and force people to use the fixed statement (which might sometimes be a no-op).
The text was updated successfully, but these errors were encountered:
The language spec (18.3 Fixed and moveable variables) says
But testing this on the following program
We find that 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.
Why does the language forbid using a fixed statement for a fixed variable? It would just be a no-op. Allowing it would sidestep having to specify which variables are captured in an async method. We could just say that there are no fixed variables in an async method, and force people to use the fixed statement (which might sometimes be a no-op).
The text was updated successfully, but these errors were encountered: