Skip to content

Commit

Permalink
incorporate additional ref struct error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
BillWagner committed Jul 31, 2024
1 parent d76eacd commit e443130
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 57 deletions.
4 changes: 4 additions & 0 deletions .openpublishing.redirection.csharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8964.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch"
},
{
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs9050.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-struct-errors"
},
{
"source_path_from_root": "/docs/csharp/language-reference/compiler-options/addmodule-compiler-option.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-options/inputs"
Expand Down
45 changes: 0 additions & 45 deletions docs/csharp/language-reference/compiler-messages/cs9050.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ f1_keywords:
- "CS8332"
- "CS8337"
- "CS8338"
- "CS8345"
- "CS8351"
- "CS8373"
- "CS8374"
Expand Down Expand Up @@ -98,7 +97,6 @@ helpviewer_keywords:
- "CS8332"
- "CS8337"
- "CS8338"
- "CS8345"
- "CS8351"
- "CS8373"
- "CS8374"
Expand Down Expand Up @@ -172,7 +170,6 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**CS8332**](#writable-reference-variables-require-a-writable-referent): *Cannot assign to a member of variable or use it as the right hand side of a `ref` assignment because it is a readonly variable*
- [**CS8337**](#reference-variable-restrictions): *The first parameter of a '`ref`' extension method must be a value type or a generic type constrained to struct.*
- [**CS8338**](#reference-variable-restrictions): *The first '`in`' or '`ref readonly`' parameter of the extension method must be a concrete (non-generic) value type.*
- [**CS8345**](#ref-safety-violations): *Field or auto-implemented property cannot be of type unless it is an instance member of a `ref struct`.*
- [**CS8351**](#ref-safety-violations): *Branches of a `ref` conditional operator cannot refer to variables with incompatible declaration scopes*
- [**CS8373**](#incorrect-syntax): *The left-hand side of a `ref` assignment must be a ref variable.*
- [**CS8374**](#ref-safety-violations): *Cannot ref-assign source has a narrower escape scope than destination.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ description: Learn about errors and warnings related to `ref struct` types. The
f1_keywords:
- "CS8343"
- "CS8344"
- "CS8345"
- "CS9048"
- "CS9050"
- "CS9059"
- "CS9241"
- "CS9242"
- "CS9243"
Expand All @@ -14,6 +18,10 @@ f1_keywords:
helpviewer_keywords:
- "CS8343"
- "CS8344"
- "CS8345"
- "CS9048"
- "CS9050"
- "CS9059"
- "CS9241"
- "CS9242"
- "CS9243"
Expand All @@ -27,6 +35,10 @@ ms.date: 07/30/2024

- [**CS8343**](#ref-struct-interface-implementations): *`ref structs` cannot implement interfaces*
- [**CS8344**](#ref-struct-interface-implementations): *`foreach` statement cannot operate on enumerators in async or iterator methods because type is a `ref struct` or a type parameter that allows `ref struct`.*
- [**CS8345**](#ref-safety-violations): *Field or auto-implemented property cannot be of type unless it is an instance member of a `ref struct`.*
- [**CS9048**](#ref-safety-violations): *The `scoped` modifier can be used for refs and `ref struct` values only.*
- [**CS9050**](#compiler-error-cs9050): *A `ref` field cannot refer to a `ref struct`.*
- [**CS9059**](#ref-safety-violations): *A ref field can only be declared in a ref struct.*
- [**CS9241**](#ref-struct-interface-implementations): *'ref struct' is already specified.*
- [**CS9242**](#ref-struct-interface-implementations): *The 'allows' constraint clause must be the last constraint specified.*
- [**CS9243**](#ref-struct-interface-implementations): *Cannot allow ref structs for a type parameter known from other constraints to be a class.*
Expand All @@ -38,3 +50,40 @@ ms.date: 07/30/2024
## ref struct interface implementations

More content here.

## ref safety violations

Even more content goes here.

## Compiler Error CS9050

A `ref` field cannot refer to a `ref struct`.

The compiler does not support the `ref` modifier on a field within a struct (to declare a stack-allocated field) of a type already declared stack-allocated (`ref struct`).

The following sample generates CS9050:

```csharp
// CS9050.cs (0,0)
ref struct Color
{
public float r, g, b;
}
ref struct Group
{
public ref Color color;
}
```

In this example, it is most likely a typo to have included a `ref` modifier on a field of a `ref struct` type within the declaration of a `ref struct`. Removing the `ref` modifier corrects this error.

```csharp
ref struct Color
{
public float r, g, b;
}
ref struct Group
{
public Color color;
}
```
1 change: 1 addition & 0 deletions docs/csharp/language-reference/keywords/ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ You use the `ref` keyword in the following contexts:
- As the part of a [conditional ref expression](../operators/conditional-operator.md#conditional-ref-expression) or a [ref assignment operator](../operators/assignment-operator.md#ref-assignment).
- In a `struct` declaration, to declare a `ref struct`. For more information, see the [`ref` structure types](../builtin-types/ref-struct.md) article.
- In a `ref struct` definition, to declare a `ref` field. For more information, see the [`ref` fields](../builtin-types/ref-struct.md#ref-fields) section of the [`ref` structure types](../builtin-types/ref-struct.md) article.
- In a generic type declaration to specify that a type parameter [`allows ref struct`](../../programming-guide/generics/constraints-on-type-parameters.md#allows-ref-struct) types.
12 changes: 5 additions & 7 deletions docs/csharp/language-reference/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,15 @@ items:
displayName: >
ref safety,
CS0192, CS0199, CS0206, CS0631, CS0767, CS1510, CS1605, CS1623, CS1649, CS1651, CS1655, CS1657, CS1741, CS1939, CS1988,
CS7084, CS8166, CS8167, CS8168, CS8169. CS8325, CS8326, CS8327, CS8329, CS8330, CS8331, CS8332, CS8337, CS8338, CS8345,
CS8351, CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, CS9087, CS9089, CS9091, CS9092,
CS9093, CS9094, CS9095, CS9096, CS9097, CS9101, CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197,
CS9198, CS9199, CS9200, CS9201
CS7084, CS8166, CS8167, CS8168, CS8169. CS8325, CS8326, CS8327, CS8329, CS8330, CS8331, CS8332, CS8337, CS8338, CS8351,
CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, CS9087, CS9089, CS9091, CS9092, CS9093,
CS9094, CS9095, CS9096, CS9097, CS9101, CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197, CS9198,
CS9199, CS9200, CS9201
- name: "`ref struct` types"
href: ./compiler-messages/ref-struct-errors.md
displayName: >
ref struct,
CS8343, CS8344, CS9241, CS9242, CS9243, CS9244, CS9245, CS9246, CS9247
CS8343, CS8344, CS8345, CS9048, CS9050, CS9059, CS9241, CS9242, CS9243, CS9244, CS9245, CS9246, CS9247
- name: Iterator methods
href: ./compiler-messages/iterator-yield.md
displayName: >
Expand Down Expand Up @@ -2017,8 +2017,6 @@ items:
href: ./compiler-messages/CS8817.md
- name: CS9043
href: ./compiler-messages/cs9043.md
- name: CS9050
href: ./compiler-messages/cs9050.md
- name: Level 1 warning messages
items:
- name: CS0183
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,13 @@ f1_keywords:
- "CS9045"
- "CS9046"
- "CS9047"
- "CS9048"
- "CS9049"
- "CS9051"
- "CS9052"
- "CS9053"
- "CS9054"
- "CS9056"
- "CS9057"
- "CS9059"
- "CS9060"
- "CS9061"
- "CS9062"
Expand Down

0 comments on commit e443130

Please sign in to comment.