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
* first pass at safety rules
This mostly incorporates the feature spec language.
* Add rule for `out` parameters
Out parameters have a ref safe to escape scope of the current method, not the calling method.
* Add readonly rule.
* fix headers
no code fences in headers
* Apply suggestions from code review
Co-authored-by: Jon Skeet <[email protected]>
* updates from reviews
Updates from code review
* style fix
* respond to feedback
Ref like fields do have storage, but that storage may refer to a variable that is a struct parameter or varialbe.
* respond to feedback
Respond to existing feedback.
* Introduce definitions
Introduce better definitions for the "lifetime" of reference variables. I avoided the term "lifetime' because of its runtime connotation. Instead, I used the "scope" where a "variable declaration" is valid.
From there, next define the safe scopes and the ref safe scopes for different variable classifications.
Finally, I shortened the terms *safe-to-escape-scope* and *ref-safe-to-escape-scope* to "*safe-scope* and *ref-safe-scope*. The text doesn't make it clear why the "escape" term is used, and it doesn't seem to add clarity.
* add examples
* respond to feedback.
* Apply suggestions from code review
Co-authored-by: Jon Skeet <[email protected]>
* address review comments
This addresses most of the comments in the most recent reviews.
The next commit will make an attempt to use a single term for *ref_safe_scope*.
* Remove *safe_scope* rules
Once I push these, I'll add notes about which rules must be added to #601
* Update standard/variables.md
Co-authored-by: Nigel-Ecma <[email protected]>
* Update standard/variables.md
* Respond to review comments.
* Feedback from April meeting
This include comments, and fixing the formatting after consulting with Rex.
* remove ref struct descriptions
These belong in the PR for ref structs, and in the section on ref structs.
* Apply suggestions from code review
Co-authored-by: Jon Skeet <[email protected]>
* respond to feedback.
* Apply suggestions from code review
Co-authored-by: Nigel-Ecma <[email protected]>
* remove missing xref
This needs to be added to #213 once this PR is merged.
* Updates from 5/17 meeting.
Updates from the 5/17 meeting.
* fix build warning
---------
Co-authored-by: Jon Skeet <[email protected]>
Co-authored-by: Nigel-Ecma <[email protected]>
Copy file name to clipboardExpand all lines: standard/variables.md
+174Lines changed: 174 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -988,3 +988,177 @@ variable_reference
988
988
## 9.6 Atomicity of variable references
989
989
990
990
Reads and writes of the following data types shall be atomic: `bool`, `char`, `byte`, `sbyte`, `short`, `ushort`, `uint`, `int`, `float`, and reference types. In addition, reads and writes of enum types with an underlying type in the previous list shall also be atomic. Reads and writes of other types, including `long`, `ulong`, `double`, and `decimal`, as well as user-defined types, need not be atomic. Aside from the library functions designed for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or decrement.
991
+
992
+
## §ref-span-safety Reference variables and returns
993
+
994
+
### §ref-span-safety-general General
995
+
996
+
A ***reference variable*** is a variable that refers to another variable, called the ***referent***. A reference variable does not store the value of its referent. When a reference variable is used where a value is required its referent's value is returned; similarly when a reference variable is the target of an assignment it is the referent which is assigned to. The variable to which a reference variable refers, i.e. its referent, can be changed using a ref assignment (`= ref`). A reference variable is a local variable declared with the `ref` modifier.
997
+
998
+
> *Example:* The following example demonstrates a local reference variable whose referent is an element of an array:
999
+
>
1000
+
> ```csharp
1001
+
>publicclassC
1002
+
> {
1003
+
>
1004
+
> publicvoidM()
1005
+
> {
1006
+
>int[] arr=newint[10];
1007
+
>// element is a reference variable that refers to arr[5]
1008
+
>refintelement=refarr[5];
1009
+
>element+=5; // arr[5] has been incremented by 5
1010
+
> }
1011
+
> }
1012
+
> ```
1013
+
>
1014
+
>*endexample*
1015
+
1016
+
A***referencereturn***istheexpressionreturnedbyreferencefromamethodwhosereturntypeincludesthe `ref` or `refreadonly` modifiers (§15.6.1). Thevariableexpressionofthereferencereturnisthereferentofthereferencereturn.
-If `p` isa `ref`, or `in` parameter, itsref-safe-contextisthecaller-context. Itissafe-to-ref-return. If `p` isan `in` parameter, itcan't be returned as a writable `ref` but can be returned as `ref readonly`.
A `new` expressionthatinvokesaconstructorobeysthesamerulesasamethodinvocation (§ref-span-safety-function-invocation) thatisconsideredtoreturnthetypebeingconstructed.
1157
+
1158
+
#### §ref-span-safety-limitations Limitations on reference variables
0 commit comments