Skip to content

Commit a4fa2cd

Browse files
authored
Add, update compiler error and warning messages for ref struct interfaces (#41948)
* Add, update messages in dotnet/roslyn#73567 The PR for `ref struct` interfaces added new compiler error and warning messages. It also updates a few messages. This commit creates the stubs for the new messages, and updates the text for updated messages. * incorporate additional ref struct error conditions * Edit pass
1 parent 598f078 commit a4fa2cd

File tree

9 files changed

+109
-64
lines changed

9 files changed

+109
-64
lines changed

.openpublishing.redirection.csharp.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@
467467
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8964.md",
468468
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch"
469469
},
470+
{
471+
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs9050.md",
472+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-struct-errors"
473+
},
470474
{
471475
"source_path_from_root": "/docs/csharp/language-reference/compiler-options/addmodule-compiler-option.md",
472476
"redirect_url": "/dotnet/csharp/language-reference/compiler-options/inputs"

docs/csharp/language-reference/compiler-messages/cs9050.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

docs/csharp/language-reference/compiler-messages/feature-version-errors.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ f1_keywords:
5353
- "CS9194"
5454
- "CS9202"
5555
- "CS9211"
56+
- "CS9240"
5657
helpviewer_keywords:
5758
- "CS0171"
5859
- "CS0188"
@@ -105,6 +106,7 @@ helpviewer_keywords:
105106
- "CS9194"
106107
- "CS9202"
107108
- "CS9211"
109+
- "CS9240"
108110
ms.date: 11/02/2023
109111
---
110112
# Resolve warnings related to language features and versions
@@ -146,6 +148,7 @@ That's be design. The text closely matches the text of the compiler error / warn
146148
- **CS9194**: *Argument may not be passed with the `ref` keyword. To pass `ref` arguments to `in` parameters, upgrade to language version 12 or greater.*
147149
- **CS9202**: *Feature is not available in C# 12.0. Please use newer language version*
148150
- **CS9211**: *The diagnosticId argument to the 'Experimental' attribute must be a valid identifier.*
151+
- **CS9240**: *Target runtime doesn't support by-ref-like generics.*
149152

150153
In addition, the following errors and warnings relate to struct initialization changes in recent versions:
151154

docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ f1_keywords:
3232
- "CS8332"
3333
- "CS8337"
3434
- "CS8338"
35-
- "CS8345"
3635
- "CS8351"
3736
- "CS8373"
3837
- "CS8374"
@@ -98,7 +97,6 @@ helpviewer_keywords:
9897
- "CS8332"
9998
- "CS8337"
10099
- "CS8338"
101-
- "CS8345"
102100
- "CS8351"
103101
- "CS8373"
104102
- "CS8374"
@@ -172,7 +170,6 @@ That's by design. The text closely matches the text of the compiler error / warn
172170
- [**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*
173171
- [**CS8337**](#reference-variable-restrictions): *The first parameter of a '`ref`' extension method must be a value type or a generic type constrained to struct.*
174172
- [**CS8338**](#reference-variable-restrictions): *The first '`in`' or '`ref readonly`' parameter of the extension method must be a concrete (non-generic) value type.*
175-
- [**CS8345**](#ref-safety-violations): *Field or auto-implemented property cannot be of type unless it is an instance member of a `ref struct`.*
176173
- [**CS8351**](#ref-safety-violations): *Branches of a `ref` conditional operator cannot refer to variables with incompatible declaration scopes*
177174
- [**CS8373**](#incorrect-syntax): *The left-hand side of a `ref` assignment must be a ref variable.*
178175
- [**CS8374**](#ref-safety-violations): *Cannot ref-assign source has a narrower escape scope than destination.*
@@ -183,8 +180,8 @@ That's by design. The text closely matches the text of the compiler error / warn
183180
- [**CS9078**](#ref-safety-violations): *Cannot return by reference a member of parameter through a `ref` parameter; it can only be returned in a return statement*
184181
- [**CS9079**](#ref-safety-violations): *Cannot ref-assign because source can only escape the current method through a return statement.*
185182
- [**CS9096**](#ref-safety-violations): *Cannot ref-assign because source has a wider value escape scope than destination allowing assignment through source of values with narrower escapes scopes than destination.*
186-
- [**CS9101**](#unscoped-ref-restrictions): *UnscopedRefAttribute can only be applied to struct instance methods and properties, and cannot be applied to constructors or init-only members.*
187-
- [**CS9102**](#unscoped-ref-restrictions): *UnscopedRefAttribute cannot be applied to an interface implementation.*
183+
- [**CS9101**](#unscoped-ref-restrictions): *UnscopedRefAttribute can only be applied to struct or virtual interface instance methods and properties, and cannot be applied to constructors or init-only members.*
184+
- [**CS9102**](#unscoped-ref-restrictions): *UnscopedRefAttribute cannot be applied to an interface implementation because implemented member doesn't have this attribute.*
188185
- [**CS9104**](#reference-variable-restrictions): *A `using` statement resource of type cannot be used in async methods or async lambda expressions.*
189186
- [**CS9190**](#incorrect-syntax): *`readonly` modifier must be specified after `ref`.*
190187
- [**CS9199**](#reference-variable-restrictions): *A `ref readonly` parameter cannot have the Out attribute.*
@@ -285,8 +282,8 @@ To fix the error, remove the reference variable where it isn't allowed:
285282

286283
The `unscoped` qualifier on `ref` parameters isn't allowed in some locations:
287284

288-
- **CS9101**: *UnscopedRefAttribute can only be applied to struct instance methods and properties, and cannot be applied to constructors or or init-only members.*
289-
- **CS9102**: *UnscopedRefAttribute cannot be applied to an interface implementation.*
285+
- **CS9101**: *UnscopedRefAttribute can only be applied to struct instance or virtual interface methods and properties, and cannot be applied to constructors or or init-only members.*
286+
- **CS9102**: *UnscopedRefAttribute cannot be applied to an interface implementation because implemented member doesn't have this attribute..*
290287

291288
You must remove the `unscoped` modifier on the parameter declaration that caused the error.
292289

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: Errors and warnings associated with `ref struct` types
3+
description: Learn about errors and warnings related to `ref struct` types. The compiler enforces several restrictions on `ref struct` types to enforce ref safety rules
4+
f1_keywords:
5+
- "CS8343"
6+
- "CS8344"
7+
- "CS8345"
8+
- "CS9048"
9+
- "CS9050"
10+
- "CS9059"
11+
- "CS9241"
12+
- "CS9242"
13+
- "CS9243"
14+
- "CS9244"
15+
- "CS9245"
16+
- "CS9246"
17+
- "CS9247"
18+
helpviewer_keywords:
19+
- "CS8343"
20+
- "CS8344"
21+
- "CS8345"
22+
- "CS9048"
23+
- "CS9050"
24+
- "CS9059"
25+
- "CS9241"
26+
- "CS9242"
27+
- "CS9243"
28+
- "CS9244"
29+
- "CS9245"
30+
- "CS9246"
31+
- "CS9247"
32+
ms.date: 07/30/2024
33+
---
34+
# Errors and warnings associated with `ref struct` types
35+
36+
- [**CS8343**](#ref-struct-interface-implementations): *`ref structs` cannot implement interfaces*
37+
- [**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`.*
38+
- [**CS8345**](#ref-safety-violations): *Field or auto-implemented property cannot be of type unless it is an instance member of a `ref struct`.*
39+
- [**CS9048**](#ref-safety-violations): *The `scoped` modifier can be used for refs and `ref struct` values only.*
40+
- [**CS9050**](#ref-safety-violations): *A `ref` field cannot refer to a `ref struct`.*
41+
- [**CS9059**](#ref-safety-violations): *A ref field can only be declared in a ref struct.*
42+
- [**CS9241**](#ref-struct-interface-implementations): *'ref struct' is already specified.*
43+
- [**CS9242**](#ref-struct-interface-implementations): *The 'allows' constraint clause must be the last constraint specified.*
44+
- [**CS9243**](#ref-struct-interface-implementations): *Cannot allow ref structs for a type parameter known from other constraints to be a class.*
45+
- [**CS9244**](#ref-struct-interface-implementations): *The type may not be a `ref struct` or a type parameter allowing ref structs in order to use it as parameter in the generic type or method.*
46+
- [**CS9245**](#ref-struct-interface-implementations): *Type cannot implement interface member for `ref struct` type.*
47+
- [**CS9246**](#ref-struct-interface-implementations): *A non-virtual instance interface member cannot be accessed on a type parameter that allows ref struct.*
48+
- [**CS9247**](#ref-struct-interface-implementations): *foreach statement cannot operate on enumerators of type because it is a type parameter that allows ref struct and it is not known at compile time to implement `IDisposable`.*
49+
50+
## ref safety violations
51+
52+
- **CS8345**: *Field or auto-implemented property cannot be of type unless it is an instance member of a `ref struct`.*
53+
- **CS9048**: *The `scoped` modifier can be used for refs and `ref struct` values only.*
54+
- **CS9050**: *A `ref` field cannot refer to a `ref struct`.*
55+
- **CS9059**: *A `ref` field can only be declared in a `ref struct`.*
56+
57+
A [`ref struct`](../builtin-types/ref-struct.md) type can include `ref` fields. Other types aren't allowed `ref` fields. The compiler enforces restrictions on the declarations and use of `ref struct` types to enforce ref safety rules on instances of any `ref struct` type:
58+
59+
- Only `ref struct` types can contain auto-implemented `ref` properties.
60+
- Only `ref struct` types or `ref` variables can have the `scoped` modifier.
61+
- A `ref` field can be declared only in a `ref struct` type.
62+
- A `ref` field can't refer to a `ref struct` type/
63+
64+
Violating any of these rules produces one of the listed errors. If you intended to use that language feature, convert the type to a `ref struct`. Otherwise, remove the disallowed construct.
65+
66+
## ref struct interface implementations
67+
68+
- **CS8343**: *`ref structs` cannot implement interfaces*
69+
- **CS8344**: *`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`.*
70+
- **CS9241**: *'ref struct' is already specified.*
71+
- **CS9242**: *The 'allows' constraint clause must be the last constraint specified.*
72+
- **CS9243**: *Cannot allow ref structs for a type parameter known from other constraints to be a class.*
73+
- **CS9244**: *The type may not be a `ref struct` or a type parameter allowing ref structs in order to use it as parameter in the generic type or method.*
74+
- **CS9245**: *Type cannot implement interface member for `ref struct` type.*
75+
- **CS9246**: *A non-virtual instance interface member cannot be accessed on a type parameter that allows ref struct.*
76+
- **CS9247**: *foreach statement cannot operate on enumerators of type because it is a type parameter that allows ref struct and it is not known at compile time to implement `IDisposable`.*
77+
78+
Prior to C# 13, [`ref struct`](../builtin-types/ref-struct.md) types can't implement interfaces; the compiler generates *CS8343*. Beginning with C# 13, `ref struct` types can implement interfaces, subject to the following rules:
79+
80+
- A `ref struct` can't be converted to an instance of an interface it implements. This restriction includes the implicit conversion when you use a `ref struct` type as an argument when the parameter is an interface type. The conversion results in a boxing conversion, which violates ref safety.
81+
- A `ref struct` that implements an interface *must* implement all interface members. The `ref struct` must implement members where the interface includes a default implementation.
82+
83+
Beginning with C# 13, a `ref struct` can be used as a type argument for a generic type parameter, if and only if the generic type parameter has the [`allows ref struct`](../../programming-guide/generics/constraints-on-type-parameters.md#allows-ref-struct) anti-constraint. When you use the `allows ref struct` anti-constraint you must follow these rules:
84+
85+
- A `ref struct` is used as a type argument, the type parameter *must* have the `allows ref struct` anti-constraint.- The `allows ref struct` anti-constraint must be last in the `where` clause for that parameter
86+
- Uses of instances the type parameter must obey ref safety rules.

docs/csharp/language-reference/keywords/ref.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ You use the `ref` keyword in the following contexts:
1818
- 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).
1919
- In a `struct` declaration, to declare a `ref struct`. For more information, see the [`ref` structure types](../builtin-types/ref-struct.md) article.
2020
- 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.
21+
- 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.

docs/csharp/language-reference/toc.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ items:
437437
CS0171, CS0188, CS0843, CS8904, CS1738, CS8022, CS8023, CS8024, CS8025, CS8026, CS8058, CS8059, CS8107, CS8192, CS8302,
438438
CS8303, CS8304, CS8305, CS8306, CS8314, CS8320, CS8370, CS8371, CS8400, CS8401, CS8511, CS8627, CS8630, CS8652, CS8703,
439439
CS8704, CS8706, CS8773, CS8912, CS8919, CS8929, CS8936, CS8957, CS8967, CS9014, CS9015, CS9016, CS9017, CS9058, CS9064,
440-
CS9103, CS9171, CS9194, CS9202, CS9204
440+
CS9103, CS9171, CS9194, CS9202, CS9204, CS9240
441441
- name: Assembly references
442442
href: ./compiler-messages/assembly-references.md
443443
displayName: >
@@ -460,10 +460,15 @@ items:
460460
displayName: >
461461
ref safety,
462462
CS0192, CS0199, CS0206, CS0631, CS0767, CS1510, CS1605, CS1623, CS1649, CS1651, CS1655, CS1657, CS1741, CS1939, CS1988,
463-
CS7084, CS8166, CS8167, CS8168, CS8169. CS8325, CS8326, CS8327, CS8329, CS8330, CS8331, CS8332, CS8337, CS8338, CS8345,
464-
CS8351, CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, CS9087, CS9089, CS9091, CS9092,
465-
CS9093, CS9094, CS9095, CS9096, CS9097, CS9101, CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197,
466-
CS9198, CS9199, CS9200, CS9201
463+
CS7084, CS8166, CS8167, CS8168, CS8169. CS8325, CS8326, CS8327, CS8329, CS8330, CS8331, CS8332, CS8337, CS8338, CS8351,
464+
CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, CS9087, CS9089, CS9091, CS9092, CS9093,
465+
CS9094, CS9095, CS9096, CS9097, CS9101, CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197, CS9198,
466+
CS9199, CS9200, CS9201
467+
- name: "`ref struct` types"
468+
href: ./compiler-messages/ref-struct-errors.md
469+
displayName: >
470+
ref struct,
471+
CS8343, CS8344, CS8345, CS9048, CS9050, CS9059, CS9241, CS9242, CS9243, CS9244, CS9245, CS9246, CS9247
467472
- name: Iterator methods
468473
href: ./compiler-messages/iterator-yield.md
469474
displayName: >
@@ -2012,8 +2017,6 @@ items:
20122017
href: ./compiler-messages/CS8817.md
20132018
- name: CS9043
20142019
href: ./compiler-messages/cs9043.md
2015-
- name: CS9050
2016-
href: ./compiler-messages/cs9050.md
20172020
- name: Level 1 warning messages
20182021
items:
20192022
- name: CS0183

docs/csharp/misc/cs0401.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ms.assetid: 94eac5a8-7344-44d2-9d0c-a9954993603d
1010
---
1111
# Compiler Error CS0401
1212

13-
The new() constraint must be the last constraint specified
13+
The new() constraint must be the last restrictive constraint specified
1414

1515
When using multiple constraints, list all other constraints before the new() constraint.
1616

docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,6 @@ f1_keywords:
304304
- "CS8340"
305305
- "CS8341"
306306
- "CS8342"
307-
- "CS8343"
308-
- "CS8344"
309307
- "CS8346"
310308
- "CS8347"
311309
- "CS8348"
@@ -584,15 +582,13 @@ f1_keywords:
584582
- "CS9045"
585583
- "CS9046"
586584
- "CS9047"
587-
- "CS9048"
588585
- "CS9049"
589586
- "CS9051"
590587
- "CS9052"
591588
- "CS9053"
592589
- "CS9054"
593590
- "CS9056"
594591
- "CS9057"
595-
- "CS9059"
596592
- "CS9060"
597593
- "CS9061"
598594
- "CS9062"

0 commit comments

Comments
 (0)