Skip to content

Commit c303456

Browse files
authored
Finish remaining C# 9 diagnostics (#53888)
* Phase 1: move internal error This diagnostics incidates you found a bug in the compiler. Good job. * First pass This assumes a search won't uncover related errors for build configs. * Move command line diagnostics I don't want the index.md to be a catch-all, so create a speciic file for command line options and build config issues. This created more opportunities for consolidation. * consolidate existing diagnostics Several existing diagnostics were consolidated around the theme of invalid build configurations. * consolidate around themes. Consolidate around themes for this area of diagnostics. * Update invalid-build-command-line.md proofread * build warnings
1 parent 4bce4d8 commit c303456

22 files changed

Lines changed: 284 additions & 414 deletions

.openpublishing.redirection.csharp.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,34 @@
1212
"source_path_from_root": "/docs/csharp/fundamentals/types/namespaces.md",
1313
"redirect_url": "/dotnet/csharp/fundamentals/program-structure/namespaces"
1414
},
15+
{
16+
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0006.md",
17+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
18+
},
19+
{
20+
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0007.md",
21+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
22+
},
23+
{
24+
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0016.md",
25+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
26+
},
27+
{
28+
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1564.md",
29+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
30+
},
31+
{
32+
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1616.md",
33+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
34+
},
1535
{
1636
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1763.md",
1737
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors"
1838
},
39+
{
40+
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs2032.md",
41+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
42+
},
1943
{
2044
"source_path_from_root": "/docs/csharp/misc/cs0017.md",
2145
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/entry-point-errors"
@@ -340,10 +364,18 @@
340364
"source_path_from_root": "/docs/csharp/misc/cs1638.md",
341365
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/feature-version-errors"
342366
},
367+
{
368+
"source_path_from_root": "/docs/csharp/misc/cs1668.md",
369+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
370+
},
343371
{
344372
"source_path_from_root": "/docs/csharp/misc/cs1715.md",
345373
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/property-declaration-errors"
346374
},
375+
{
376+
"source_path_from_root": "/docs/csharp/misc/cs1719.md",
377+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
378+
},
347379
{
348380
"source_path_from_root": "/docs/csharp/misc/cs1720.md",
349381
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors"
@@ -356,10 +388,34 @@
356388
"source_path_from_root": "/docs/csharp/misc/cs1958.md",
357389
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/delegate-function-pointer-diagnostics"
358390
},
391+
{
392+
"source_path_from_root": "/docs/csharp/misc/cs2008.md",
393+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
394+
},
359395
{
360396
"source_path_from_root": "/docs/csharp/misc/cs2017.md",
361397
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/entry-point-errors"
362398
},
399+
{
400+
"source_path_from_root": "/docs/csharp/misc/cs2019.md",
401+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
402+
},
403+
{
404+
"source_path_from_root": "/docs/csharp/misc/cs2029.md",
405+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
406+
},
407+
{
408+
"source_path_from_root": "/docs/csharp/misc/cs2036.md",
409+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
410+
},
411+
{
412+
"source_path_from_root": "/docs/csharp/misc/cs3012.md",
413+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
414+
},
415+
{
416+
"source_path_from_root": "/docs/csharp/misc/cs3013.md",
417+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/invalid-build-command-line"
418+
},
363419
{
364420
"source_path_from_root": "/docs/csharp/misc/cs3024.md",
365421
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors"

docs/csharp/language-reference/compiler-messages/attribute-usage-errors.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ f1_keywords:
4444
- "CS8962"
4545
- "CS8963"
4646
- "CS8968"
47+
- "CS8783"
4748
- "CS8970"
4849
- "CS9331"
4950
helpviewer_keywords:
@@ -89,9 +90,10 @@ helpviewer_keywords:
8990
- "CS8962"
9091
- "CS8963"
9192
- "CS8968"
93+
- "CS8783"
9294
- "CS8970"
9395
- "CS9331"
94-
ms.date: 02/13/2026
96+
ms.date: 05/19/2026
9597
ai-usage: ai-assisted
9698
---
9799
# Resolve errors and warnings related to attribute declarations or attribute use in your code
@@ -143,6 +145,7 @@ That's by design. The text closely matches the text of the compiler error / warn
143145
- [**CS8962**](#callerargumentexpression-attribute-usage): *The CallerArgumentExpressionAttribute applied to parameter will have no effect. It is overridden by the CallerMemberNameAttribute.*
144146
- [**CS8963**](#callerargumentexpression-attribute-usage): *The CallerArgumentExpressionAttribute applied to parameter will have no effect. It is applied with an invalid parameter name.*
145147
- [**CS8968**](#attribute-arguments-and-parameters): *An attribute type argument cannot use type parameters*
148+
- [**CS8783**](#conditional-attribute-usage): *Local function 'method' must be 'static' in order to use the Conditional attribute*
146149
- [**CS8970**](#attribute-arguments-and-parameters): *Type cannot be used in this context because it cannot be represented in metadata.*
147150
- [**CS9331**](#predefined-attributes): *Attribute cannot be applied manually.*
148151

@@ -234,16 +237,16 @@ The following errors occur when you use specific predefined .NET attributes inco
234237

235238
To correct these errors, follow these rules. For more information, see [Indexers](../../programming-guide/indexers/index.md), [Structure types](../builtin-types/struct.md), <xref:System.Runtime.CompilerServices.TypeForwardedToAttribute>, and [Platform Invoke (P/Invoke)](../../../standard/native-interop/pinvoke.md).
236239

237-
- The <xref:System.Runtime.CompilerServices.IndexerNameAttribute> can only be applied to indexers that aren't explicit interface member declarations (**CS0415**). Remove the attribute from explicit interface indexers, because the interface already defines the indexer name.
240+
- You can apply <xref:System.Runtime.CompilerServices.IndexerNameAttribute> only to indexers that aren't explicit interface member declarations (**CS0415**). Remove the attribute from explicit interface indexers, because the interface already defines the indexer name.
238241
- You can't apply `IndexerName` to indexers marked with `override` because override indexers inherit their name from the base class (**CS0609**). Remove the `IndexerName` attribute from the override indexer.
239242
- Every instance field in a type marked with `StructLayout(LayoutKind.Explicit)` must have a <xref:System.Runtime.InteropServices.FieldOffsetAttribute> (**CS0625**). Explicit layout requires that you specify the byte offset for each instance field.
240-
- The <xref:System.Runtime.InteropServices.FieldOffsetAttribute> can only be placed on members of types that have <xref:System.Runtime.InteropServices.StructLayoutAttribute> set to `LayoutKind.Explicit` (**CS0636**). Add the `StructLayout` attribute to the containing type declaration.
243+
- You can place the <xref:System.Runtime.InteropServices.FieldOffsetAttribute> only on members of types that have <xref:System.Runtime.InteropServices.StructLayoutAttribute> set to `LayoutKind.Explicit` (**CS0636**). Add the `StructLayout` attribute to the containing type declaration.
241244
- The `FieldOffset` attribute isn't allowed on `static` or `const` fields because explicit layout applies only to instance fields (**CS0637**). Remove the `FieldOffset` attribute from the static or const field.
242245
- You can't apply <xref:System.Reflection.DefaultMemberAttribute> to a type that already contains an indexer because the compiler automatically defines the default member for types with indexers (**CS0646**). Remove the `DefaultMember` attribute.
243246
- All <xref:System.Runtime.CompilerServices.IndexerNameAttribute> attributes within a type must specify the same name (**CS0668**). Change the names to match, because the runtime uses a single name for all indexers on a type.
244-
- The type specified as an argument for <xref:System.Runtime.CompilerServices.TypeForwardedToAttribute> must be a non-generic, non-nested, non-pointer, non-array type (**CS0735**). Only top-level named types are valid forwarding targets.
247+
- You must specify a non-generic, non-nested, non-pointer, non-array type as an argument for <xref:System.Runtime.CompilerServices.TypeForwardedToAttribute> (**CS0735**). Only top-level named types are valid forwarding targets.
245248
- An assembly can have only one <xref:System.Runtime.CompilerServices.TypeForwardedToAttribute> for each external type (**CS0739**). Locate and remove the duplicate `TypeForwardedTo` declaration.
246-
- The <xref:System.Runtime.CompilerServices.RequiredAttributeAttribute> isn't permitted on types defined in C# (**CS1608**). This attribute is reserved for other languages that need to force compilers to require a particular feature.
249+
- You can't use the <xref:System.Runtime.CompilerServices.RequiredAttributeAttribute> on types defined in C# (**CS1608**). This attribute is reserved for other languages that need to force compilers to require a particular feature.
247250
- Some attributes are reserved for the compiler and can't be applied manually in source code (**CS9331**). Replace the attribute with the equivalent C# language syntax that causes the compiler to generate it.
248251

249252
## Conditional attribute usage
@@ -258,6 +261,7 @@ You see the following errors when you apply the <xref:System.Diagnostics.Conditi
258261
- **CS0685**: *Conditional member 'member' cannot have an out parameter.*
259262
- **CS1618**: *Cannot create delegate with method because it or a method it overrides has a Conditional attribute*
260263
- **CS1689**: *Attribute is only valid on methods or attribute classes*
264+
- **CS8783**: *Local function 'method' must be 'static' in order to use the Conditional attribute*
261265

262266
To correct these errors, follow these rules. For more information, see <xref:System.Diagnostics.ConditionalAttribute>, [Conditional methods](~/_csharpstandard/standard/attributes.md#23532-conditional-methods), and [Attributes](../../advanced-topics/reflection-and-attributes/index.md).
263267

@@ -269,6 +273,7 @@ To correct these errors, follow these rules. For more information, see <xref:Sys
269273
- Conditional methods can't have `out` parameters because the `out` variable value would be undefined when the compiler omits the method call (**CS0685**). Remove the `out` parameters from the method, or remove the `Conditional` attribute.
270274
- You can't create a delegate that references a conditional method because the method might not exist in builds where the condition symbol isn't defined (**CS1618**). Remove the `Conditional` attribute from the method, or don't use it as a delegate target.
271275
- The `Conditional` attribute is only valid on methods and attribute classes (**CS1689**). It isn't valid on other declaration types such as non-attribute classes, structs, or interfaces.
276+
- A local function must be declared `static` to use the `Conditional` attribute (**CS8783**). Non-static local functions capture state from the enclosing method, and the compiler can't safely omit a call that might involve captured variables. Add the `static` modifier to the local function, or remove the `[Conditional]` attribute.
272277

273278
## CallerArgumentExpression attribute usage
274279

docs/csharp/language-reference/compiler-messages/constructor-errors.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ f1_keywords:
1818
- "CS1018" # ERR_ThisOrBaseExpected
1919
- "CS8054" # ERR_EnumsCantContainDefaultConstructor
2020
- "CS8091" # ERR_ExternHasConstructorInitializer
21+
- "CS8760" # ERR_ExternEventInitializer
2122
- "CS8358" # ERR_AttributeCtorInParameter
2223
- "CS8813" # ERR_ModuleInitializerMethodMustBeOrdinary
2324
- "CS8814" # ERR_ModuleInitializerMethodMustBeAccessibleOutsideTopLevelType
@@ -78,6 +79,7 @@ helpviewer_keywords:
7879
- "CS8054"
7980
- "CS8091"
8081
- "CS8358"
82+
- "CS8760"
8183
- "CS8813"
8284
- "CS8814"
8385
- "CS8815"
@@ -120,7 +122,7 @@ helpviewer_keywords:
120122
- "CS9124"
121123
- "CS9136"
122124
- "CS9179"
123-
ms.date: 05/01/2026
125+
ms.date: 05/19/2026
124126
---
125127
# Resolve errors and warnings for constructor declarations and module initializers
126128

@@ -143,6 +145,7 @@ That's by design. The text closely matches the text of the compiler error / warn
143145
- [**CS1018**](#constructor-calls-with-base-and-this): *Keyword 'this' or 'base' expected.*
144146
- [**CS8054**](#constructor-declaration): *Enums cannot contain explicit parameterless constructors.*
145147
- [**CS8091**](#constructor-declaration): *cannot be extern and have a constructor initializer.*
148+
- [**CS8760**](#constructor-declaration): *'event': extern event cannot have initializer.*
146149
- [**CS8358**](#constructor-declaration): *Cannot use attribute constructor because it has 'in' or 'ref readonly' parameters.*
147150
- [**CS8813**](#module-initializer-declarations): *A module initializer must be an ordinary member method*
148151
- [**CS8814**](#module-initializer-declarations): *Module initializer method 'method' must be accessible at the module level*
@@ -212,6 +215,7 @@ To correct these errors, ensure your static constructor declaration follows thes
212215
- **CS8054**: *Enums cannot contain explicit parameterless constructors.*
213216
- **CS8358**: *Cannot use attribute constructor because it has 'in' or 'ref readonly' parameters.*
214217
- **CS8091**: *A constructor cannot be extern and have a constructor initializer.*
218+
- **CS8760**: *'event': extern event cannot have initializer.*
215219

216220
You can declare constructors only in `class` and `struct` types, including `record class` and `record struct` types. For more information, see [Instance constructors](../../programming-guide/classes-and-structs/instance-constructors.md).
217221

@@ -221,6 +225,7 @@ To fix these errors, try the following suggestions:
221225
- Remove instance constructors from static classes, because static classes can't be instantiated and therefore can't have instance constructors (**CS0710**). If you need initialization logic, use a static constructor instead.
222226
- Change `in` or `ref readonly` parameters to pass-by-value parameters in attribute constructors, because attribute constructors don't support `in` or `ref readonly` parameter modifiers (**CS8358**). The runtime instantiates attributes by using reflection, which doesn't support the `in` or `ref readonly` modifier.
223227
- Remove the `: base()` or `: this()` constructor initializer from an `extern` constructor, because extern constructors can't chain to other constructors (**CS8091**). The implementation of an extern constructor is provided externally, so constructor chaining isn't possible.
228+
- Remove the initializer from an `extern` event declaration (**CS8760**). Extern events have their add and remove accessors provided by external code, so a field initializer in the source has no effect and isn't permitted by the compiler.
224229

225230
The following warning can be generated for constructor declarations:
226231

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)