-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add, update compiler error and warning messages for
ref struct
inte…
…rfaces (#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
- Loading branch information
1 parent
598f078
commit a4fa2cd
Showing
9 changed files
with
109 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 0 additions & 45 deletions
45
docs/csharp/language-reference/compiler-messages/cs9050.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
docs/csharp/language-reference/compiler-messages/ref-struct-errors.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
--- | ||
title: Errors and warnings associated with `ref struct` types | ||
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 | ||
f1_keywords: | ||
- "CS8343" | ||
- "CS8344" | ||
- "CS8345" | ||
- "CS9048" | ||
- "CS9050" | ||
- "CS9059" | ||
- "CS9241" | ||
- "CS9242" | ||
- "CS9243" | ||
- "CS9244" | ||
- "CS9245" | ||
- "CS9246" | ||
- "CS9247" | ||
helpviewer_keywords: | ||
- "CS8343" | ||
- "CS8344" | ||
- "CS8345" | ||
- "CS9048" | ||
- "CS9050" | ||
- "CS9059" | ||
- "CS9241" | ||
- "CS9242" | ||
- "CS9243" | ||
- "CS9244" | ||
- "CS9245" | ||
- "CS9246" | ||
- "CS9247" | ||
ms.date: 07/30/2024 | ||
--- | ||
# Errors and warnings associated with `ref struct` types | ||
|
||
- [**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**](#ref-safety-violations): *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.* | ||
- [**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.* | ||
- [**CS9245**](#ref-struct-interface-implementations): *Type cannot implement interface member for `ref struct` type.* | ||
- [**CS9246**](#ref-struct-interface-implementations): *A non-virtual instance interface member cannot be accessed on a type parameter that allows ref struct.* | ||
- [**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`.* | ||
|
||
## ref safety violations | ||
|
||
- **CS8345**: *Field or auto-implemented property cannot be of type unless it is an instance member of a `ref struct`.* | ||
- **CS9048**: *The `scoped` modifier can be used for refs and `ref struct` values only.* | ||
- **CS9050**: *A `ref` field cannot refer to a `ref struct`.* | ||
- **CS9059**: *A `ref` field can only be declared in a `ref struct`.* | ||
|
||
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: | ||
|
||
- Only `ref struct` types can contain auto-implemented `ref` properties. | ||
- Only `ref struct` types or `ref` variables can have the `scoped` modifier. | ||
- A `ref` field can be declared only in a `ref struct` type. | ||
- A `ref` field can't refer to a `ref struct` type/ | ||
|
||
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. | ||
|
||
## ref struct interface implementations | ||
|
||
- **CS8343**: *`ref structs` cannot implement interfaces* | ||
- **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`.* | ||
- **CS9241**: *'ref struct' is already specified.* | ||
- **CS9242**: *The 'allows' constraint clause must be the last constraint specified.* | ||
- **CS9243**: *Cannot allow ref structs for a type parameter known from other constraints to be a class.* | ||
- **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.* | ||
- **CS9245**: *Type cannot implement interface member for `ref struct` type.* | ||
- **CS9246**: *A non-virtual instance interface member cannot be accessed on a type parameter that allows ref struct.* | ||
- **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`.* | ||
|
||
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: | ||
|
||
- 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. | ||
- 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. | ||
|
||
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: | ||
|
||
- 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 | ||
- Uses of instances the type parameter must obey ref safety rules. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters