Skip to content

Commit fa2db7c

Browse files
authored
Add additional extension errors (#49290)
* Add additional extension errors Fixes #46453 After new operators were added, new diagnostics were necessary in the area of extensions. Also, pick up a few other stragglers added for other features. * Update docs/csharp/language-reference/toc.yml
1 parent 4423d2e commit fa2db7c

File tree

5 files changed

+97
-15
lines changed

5 files changed

+97
-15
lines changed

docs/csharp/language-reference/compiler-messages/extension-declarations.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Errors and warnings related to extension declarations"
33
description: "These errors and warnings indicate that you need to modify the declaration of an extension method using the `this` modifier on the first parameter, or an extension declaration"
4-
ms.date: 05/23/2025
4+
ms.date: 10/16/2025
55
f1_keywords:
66
- "CS1100"
77
- "CS1101"
@@ -32,6 +32,18 @@ f1_keywords:
3232
- "CS9303"
3333
- "CS9304"
3434
- "CS9305"
35+
- "CS9306"
36+
- "CS9309"
37+
- "CS9316"
38+
- "CS9317"
39+
- "CS9318"
40+
- "CS9319"
41+
- "CS9320"
42+
- "CS9321"
43+
- "CS9322"
44+
- "CS9323"
45+
- "CS9326"
46+
- "CS9329"
3547
helpviewer_keywords:
3648
- "CS1100"
3749
- "CS1101"
@@ -64,6 +76,18 @@ helpviewer_keywords:
6476
- "CS9303"
6577
- "CS9304"
6678
- "CS9305"
79+
- "CS9306"
80+
- "CS9309"
81+
- "CS9316"
82+
- "CS9317"
83+
- "CS9318"
84+
- "CS9319"
85+
- "CS9320"
86+
- "CS9321"
87+
- "CS9322"
88+
- "CS9323"
89+
- "CS9326"
90+
- "CS9329"
6791
---
6892
# Errors and warnings related to extension methods declared with `this` parameters or `extension` blocks
6993

@@ -98,6 +122,18 @@ helpviewer_keywords:
98122
- [**CS9303**](#errors-related-to-extension-block-declarations): *Cannot declare instance members in an extension block with an unnamed receiver parameter.*
99123
- [**CS9304**](#errors-related-to-extension-block-declarations): *Cannot declare init-only accessors in an extension block.*
100124
- [**CS9305**](#errors-related-to-extension-block-declarations): *Cannot use modifiers on the unnamed receiver parameter of extension block.*
125+
- [**CS9306**](#errors-related-to-extension-block-declarations): *Types and aliases cannot be named 'extension'.*
126+
- [**CS9309**](#errors-related-to-extension-block-declarations): *An extension member syntax is disallowed in nested position within an extension member syntax.*
127+
- [**CS9316**](#errors-related-to-extension-block-declarations): *Extension members are not allowed as an argument to '`nameof`'.*
128+
- [**CS9317**](#errors-related-to-extension-block-declarations): *The parameter of a unary operator must be the extended type.*
129+
- [**CS9318**](#errors-related-to-extension-block-declarations): *The parameter type for ++ or -- operator must be the extended type.*
130+
- [**CS9319**](#errors-related-to-extension-block-declarations): *One of the parameters of a binary operator must be the extended type.*
131+
- [**CS9320**](#errors-related-to-extension-block-declarations): *The first operand of an overloaded shift operator must have the same type as the extended type.*
132+
- [**CS9321**](#errors-related-to-extension-block-declarations): *An extension block extending a static class cannot contain user-defined operators.*
133+
- [**CS9322**](#errors-related-to-extension-block-declarations): *Cannot declare instance operator for a struct unless containing extension block receiver parameter is a '`ref`' parameter.*
134+
- [**CS9323**](#errors-related-to-extension-block-declarations): *Cannot declare instance extension operator for a type that is not known to be a struct and is not known to be a class.*
135+
- [**CS9326**](#errors-related-to-extension-block-declarations): *'`name`': extension member names cannot be the same as their extended type.*
136+
- [**CS9329**](#errors-related-to-extension-block-declarations): *This extension block collides with another extension block. They result in conflicting content-based type names in metadata.*
101137

102138
## Common errors on extension declarations
103139

@@ -143,6 +179,20 @@ These errors are specific to extension blocks, a C# 14 feature. Extension blocks
143179
- **CS9303**: *Cannot declare instance members in an extension block with an unnamed receiver parameter.*
144180
- **CS9304**: *Cannot declare init-only accessors in an extension block.*
145181
- **CS9305**: *Cannot use modifiers on the unnamed receiver parameter of extension block.*
182+
- **CS9306**: *Types and aliases cannot be named 'extension'.*
183+
- **CS9309**: *An extension member syntax is disallowed in nested position within an extension member syntax.*
184+
- **CS9316**: *Extension members are not allowed as an argument to '`nameof`'.*
185+
- **CS9317**: *The parameter of a unary operator must be the extended type.*
186+
- **CS9318**: *The parameter type for ++ or -- operator must be the extended type.*
187+
- **CS9319**: *One of the parameters of a binary operator must be the extended type.*
188+
- **CS9320**: *The first operand of an overloaded shift operator must have the same type as the extended type.*
189+
- **CS9321**: *An extension block extending a static class cannot contain user-defined operators.*
190+
- **CS9322**: *Cannot declare instance operator for a struct unless containing extension block receiver parameter is a '`ref`' parameter.*
191+
- **CS9323**: *Cannot declare instance extension operator for a type that is not known to be a struct and is not known to be a class.*
192+
- **CS9326**: *'`name`': extension member names cannot be the same as their extended type.*
193+
- **CS9329**: *This extension block collides with another extension block. They result in conflicting content-based type names in metadata.*
194+
195+
The contextual keyword [`extension`](../keywords/extension.md) declares an extension block. It can't be used for a type.
146196

147197
Extension declarations must follow these rules:
148198

@@ -155,6 +205,23 @@ Extension members declared in an extension block must follow these rules, in add
155205
- The extension must provide a parameter name for the receiver in order to contain members that extend an instance.
156206
- The receiver parameter name must be unique in that extension block.
157207
- All extension members must use all type parameters declared on the extension. They can add more type parameters.
208+
- Extension blocks can't be nested within another extension block.
209+
210+
**CS9316** is emitted when you attempt to use an extension member as an argument to the `nameof` operator. Extension members aren't allowed in this context.
211+
212+
**CS9317**, **CS9318**, **CS9319**, **CS9320**, **CS9321**, **CS9322**, and **CS9323** are operator-related errors in extension blocks:
213+
214+
- **CS9317**: Unary operators must have the extended type as their parameter.
215+
- **CS9318**: Increment (`++`) and decrement (`--`) operators must have the extended type as their parameter.
216+
- **CS9319**: Binary operators must have at least one parameter that is the extended type.
217+
- **CS9320**: Shift operators must have the extended type as their first operand.
218+
- **CS9321**: You can't declare user-defined operators in extension blocks that extend static classes.
219+
- **CS9322**: When extending a struct with instance operators, the receiver parameter must use the `ref` modifier.
220+
- **CS9323**: You can't declare instance operators for types that aren't constrained to be either a struct or a class.
221+
222+
**CS9326** is emitted when an extension member has the same name as the extended type. Choose a different name for the member.
223+
224+
**CS9329** occurs when two extension blocks result in conflicting content-based type names in the compiled metadata. This typically happens when multiple extension blocks with the same receiver type and similar characteristics are declared. Consolidate the extension blocks or differentiate them in a way that produces unique metadata names.
158225

159226
## Errors related to `this` parameter extension methods
160227

docs/csharp/language-reference/compiler-messages/pattern-matching-warnings.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ f1_keywords:
55
- "CS8509" # WRN_SwitchNotAllPossibleValues: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '...' is not covered.
66
- "CS9134"
77
- "CS9135"
8+
- "CS9335"
9+
- "CS9337"
810
helpviewer_keywords:
911
- "CS8509"
1012
- "CS9134"
11-
- "CS9135"
12-
ms.date: 11/02/2022
13+
- "CS9335"
14+
- "CS9337"
15+
ms.date: 10/16/2025
1316
---
1417
# Pattern matching errors and warnings
1518

1619
The compiler generates the following errors for invalid pattern match expressions:
1720

1821
- **CS9134**: *A switch expression arm does not begin with a 'case' keyword.*
1922
- **CS9135**: *A constant value of type is expected*
23+
- **CS9335**: *The pattern is redundant.*
24+
- **CS9337**: *The pattern is too complex to analyze for redundancy.*
2025

2126
The compiler generates the following warnings for incomplete pattern matching expressions:
2227

@@ -45,4 +50,12 @@ To address this warning, add switch arms that cover all possible input values. F
4550

4651
The `_` pattern matches all remaining values. One scenario for the `_` pattern is matching invalid values, as shown in the preceding example.
4752

53+
The compiler generates CS9335 when you wrote a pattern where an `or` is redundant:
54+
55+
:::code language="csharp" source="./snippets/pattern-matching-warnings/Switch.cs" id="RedundantPattern":::
56+
57+
This warning indicates you likely meant `is not (null or 42)` or `is not (int or string)` instead.
58+
59+
You can construct patterns that are too complex to analyze for redundancy. If you do that, the compiler warns you with CS9337.
60+
4861
For more information, see [Switch](../statements/selection-statements.md#the-switch-statement).

docs/csharp/language-reference/compiler-messages/snippets/pattern-matching-warnings/Switch.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ void Method(EnumValues enumValues)
2121
};
2222
}
2323
// </SwitchNotAllPossibleValues>
24+
25+
public static void RedundantPattern()
26+
{
27+
object? o = null;
28+
// <RedundantPattern>
29+
_ = o is not null or 42; // warning: pattern "42" is redundant
30+
_ = o is not int or string; // warning: pattern "string" is redundant
31+
// </RedundantPattern>
32+
}
2433
}
2534
}
2635

docs/csharp/language-reference/toc.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ items:
520520
displayName: >
521521
CS1100, CS1101, CS1102, CS1103, CS1105, CS1106, CS1109, CS1110, CS1113, CS1112, CS1100, CS1101, CS1102, CS1103,
522522
CS1105, CS1106, CS1109, CS1110, CS1112, CS1113, CS1754, CS9281, CS9282, CS9283, CS9284, CS9285, CS9287, CS9288,
523-
CS9289, CS9290, CS9292, CS9293, CS9295, CS9300, CS9301, CS9302, CS9303, CS9304, CS9305
523+
CS9289, CS9290, CS9292, CS9293, CS9295, CS9300, CS9301, CS9302, CS9303, CS9304, CS9305, CS9306, CS9309, CS9316,
524+
CS9317, CS9318, CS9319, CS9320, CS9321, CS9322, CS9323, CS9326, CS9329
524525
- name: Partial declarations
525526
href: ./compiler-messages/partial-declarations.md
526527
displayName: >
@@ -543,7 +544,7 @@ items:
543544
CS8768, CS8670, CS8714, CS8767, CS8769, CS8770, CS8774, CS8776, CS8775, CS8777, CS8819, CS8824, CS8825, CS8847
544545
- name: Pattern matching warnings
545546
href: ./compiler-messages/pattern-matching-warnings.md
546-
displayName: CS8509, CS9134, CS9135
547+
displayName: CS8509, CS9134, CS9135, CS9335, CS9337
547548
- name: String literal declarations
548549
href: ./compiler-messages/string-literal.md
549550
displayName: >
@@ -574,7 +575,7 @@ items:
574575
displayName: >
575576
CS0765, CS0831, CS0832, CS0834, CS0835, CS0838, CS0845, CS0853, CS0854, CS0855, CS1944, CS1945, CS1946, CS1951, CS1952,
576577
CS1963, CS2037, CS7053, CS8072, CS8074, CS8075, CS8110, CS8122, CS8143, CS8144, CS8153, CS8155, CS8198, CS8207, CS8382,
577-
CS8514, CS8640, CS8642, CS8790, CS8791, CS8792, CS8810, CS8849, CS8927, CS8952, CS9170, CS9175
578+
CS8514, CS8640, CS8642, CS8790, CS8791, CS8792, CS8810, CS8849, CS8927, CS8952, CS9170, CS9175, CS9307
578579
- name: Using directive and aliases
579580
href: ./compiler-messages/using-directive-errors.md
580581
displayName: >

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -593,22 +593,14 @@ f1_keywords:
593593
- "CS9235"
594594
- "CS9236"
595595
# C# 14 errors begin here
596-
## using `extension` as type name. (valid identifiers).
597-
- "CS9306"
598-
## More extension errors
599-
- "CS9316"
600-
- "CS9317"
601-
- "CS9318"
602-
- "CS9319"
603-
- "CS9326"
604596
- "CS9327"
605597
- "CS9328"
606-
- "CS9329"
607598
- "CS9330"
608599
- "CS9331"
609600
- "CS9332"
610601
- "CS9333"
611602
- "CS9334"
603+
- "CS9338"
612604
helpviewer_keywords:
613605
- "errors [C#], additional information"
614606
---

0 commit comments

Comments
 (0)