diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 43036829fd283..496b5558186fb 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -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" diff --git a/docs/csharp/language-reference/compiler-messages/cs9050.md b/docs/csharp/language-reference/compiler-messages/cs9050.md deleted file mode 100644 index 90da0e2a66f60..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs9050.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -description: "Compiler Error CS9050" -title: "Compiler Error CS9050" -ms.date: 9/19/2022 -f1_keywords: - - "CS9050" -helpviewer_keywords: - - "CS9050" ---- -# 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`). - -## Example - - 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; -} -``` - -## To correct this error - -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; -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md b/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md index 6ce302a7d93a1..437eaae719101 100644 --- a/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md +++ b/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md @@ -32,7 +32,6 @@ f1_keywords: - "CS8332" - "CS8337" - "CS8338" - - "CS8345" - "CS8351" - "CS8373" - "CS8374" @@ -98,7 +97,6 @@ helpviewer_keywords: - "CS8332" - "CS8337" - "CS8338" - - "CS8345" - "CS8351" - "CS8373" - "CS8374" @@ -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.* diff --git a/docs/csharp/language-reference/compiler-messages/ref-struct-errors.md b/docs/csharp/language-reference/compiler-messages/ref-struct-errors.md index 169c29dca0a36..820523fd27ff8 100644 --- a/docs/csharp/language-reference/compiler-messages/ref-struct-errors.md +++ b/docs/csharp/language-reference/compiler-messages/ref-struct-errors.md @@ -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" @@ -14,6 +18,10 @@ f1_keywords: helpviewer_keywords: - "CS8343" - "CS8344" + - "CS8345" + - "CS9048" + - "CS9050" + - "CS9059" - "CS9241" - "CS9242" - "CS9243" @@ -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.* @@ -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; +} +``` diff --git a/docs/csharp/language-reference/keywords/ref.md b/docs/csharp/language-reference/keywords/ref.md index 9aee88f037fd7..be288cf2b1d07 100644 --- a/docs/csharp/language-reference/keywords/ref.md +++ b/docs/csharp/language-reference/keywords/ref.md @@ -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. diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 69e3912f9e0d1..3b2d150c7468b 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -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: > @@ -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 diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index 5ba6cb4349ccb..5ced5e04384fc 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -582,7 +582,6 @@ f1_keywords: - "CS9045" - "CS9046" - "CS9047" - - "CS9048" - "CS9049" - "CS9051" - "CS9052" @@ -590,7 +589,6 @@ f1_keywords: - "CS9054" - "CS9056" - "CS9057" - - "CS9059" - "CS9060" - "CS9061" - "CS9062"