Skip to content

Commit df490d0

Browse files
BillWagnerCopilotmeaghanlewis
authored
Add C# errors introduced in C# 12 to appropriate files (#49377)
* checkpoint - scratch pad for errors Put the errors and the messages in one file for triage and moving into correct themes. * Move errors to correct theme. * Final edits * Update docs/csharp/language-reference/compiler-messages/lambda-expression-errors.md Co-authored-by: Copilot <[email protected]> * fix build warnings * Apply suggestions from code review Co-authored-by: Meaghan Osagie (Lewis) <[email protected]> * Apply suggestions from code review Co-authored-by: Meaghan Osagie (Lewis) <[email protected]> * Apply suggestions from code review Co-authored-by: Meaghan Osagie (Lewis) <[email protected]> --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Meaghan Osagie (Lewis) <[email protected]>
1 parent bd9f55a commit df490d0

File tree

7 files changed

+105
-46
lines changed

7 files changed

+105
-46
lines changed

docs/csharp/language-reference/compiler-messages/array-declaration-errors.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ f1_keywords:
3737
- "CS9208"
3838
- "CS9209"
3939
- "CS9210"
40+
- "CS9212"
41+
- "CS9213"
42+
- "CS9214"
43+
- "CS9215"
44+
- "CS9222"
4045
helpviewer_keywords:
4146
- "CS0022"
4247
- "CS0178"
@@ -73,6 +78,11 @@ helpviewer_keywords:
7378
- "CS9208"
7479
- "CS9209"
7580
- "CS9210"
81+
- "CS9212"
82+
- "CS9213"
83+
- "CS9214"
84+
- "CS9215"
85+
- "CS9222"
7686
ms.date: 11/02/2023
7787
---
7888
# Resolve errors and warnings in array and collection declarations and initialization expressions
@@ -110,6 +120,11 @@ That's by design. The text closely matches the text of the compiler error / warn
110120
- [**CS9188**](#invalid-collection-builder): *Type has a CollectionBuilderAttribute but no element type.*
111121
- [**CS9203**](#invalid-collection-initializer): *A collection expression of this type cannot be used in this context because it may be exposed outside of the current scope.*
112122
- [**CS9210**](#invalid-collection-initializer): *This version of <xref:System.Collections.Immutable.ImmutableArray%601?displayProperty=nameWithType>cannot be used with collection expressions.*
123+
- [**CS9212**](#invalid-collection-initializer): *Spread operator '`..`' cannot operate on variables of type 'type' because 'type' does not contain a public instance or extension definition for 'member'.*
124+
- [**CS9213**](#invalid-collection-initializer): *Collection expression target 'type' has no element type.*
125+
- [**CS9214**](#invalid-collection-initializer): *Collection expression type must have an applicable constructor that can be called with no arguments.*
126+
- [**CS9215**](#invalid-collection-initializer): *Collection expression type 'type' must have an instance or extension method 'Add' that can be called with a single argument.*
127+
- [**CS9222**](#invalid-collection-initializer): *Collection initializer results in an infinite chain of instantiations of collection 'type'.*
113128

114129
In addition, the following warnings are covered in this article:
115130

@@ -141,6 +156,11 @@ The following errors indicate that the code generated by the compiler for a coll
141156
- **CS9176**: *There is no target type for the collection literal.*
142157
- **CS9203**: *A collection expression of this type cannot be used in this context because it may be exposed outside of the current scope.*
143158
- **CS9210**: *This version of <xref:System.Collections.Immutable.ImmutableArray%601?displayProperty=nameWithType>cannot be used with collection expressions.*
159+
- **CS9212**: *Spread operator '`..`' cannot operate on variables of type 'type' because 'type' does not contain a public instance or extension definition for 'member'.*
160+
- **CS9213**: *Collection expression target 'type' has no element type.*
161+
- **CS9214**: *Collection expression type must have an applicable constructor that can be called with no arguments.*
162+
- **CS9215**: *Collection expression type 'type' must have an instance or extension method 'Add' that can be called with a single argument.*
163+
- **CS9222**: *Collection initializer results in an infinite chain of instantiations of collection 'type'.*
144164

145165
The compiler might also generate the following warning:
146166

@@ -159,6 +179,11 @@ The errors all indicate that the code generated by the compiler for a collection
159179
- Collection expressions can initialize explicitly typed variables of a collection type. If the variable isn't a collection or array type, or is implicitly typed (using `var`), a collection initializer can't be used.
160180
- A `ref struct` type, like <xref:System.Span%601?displayProperty=nameWithType> can't be initialized with a collection expression that may violate ref safety.
161181
- A collection expression can't correctly initialize an <xref:System.Collections.Immutable.ImmutableArray%601?displayProperty=nameWithType> using the current version. Use a different version of the runtime, or change the initialization expression.
182+
- The spread operator (`..`) in **CS9212** requires the type to implement a suitable method (like `GetEnumerator`) to enumerate its elements. Ensure your type implements the required enumeration pattern or provides an extension method.
183+
- **CS9213** occurs when the compiler can't determine what element type to use for the collection expression. This typically happens with custom collection types. Make sure your collection type properly exposes its element type through its type definition or implements appropriate collection interfaces.
184+
- **CS9214** is generated when a collection expression tries to initialize a type that doesn't have a parameterless constructor. Collection expressions require a constructor that can be called with no arguments to create the instance before adding elements.
185+
- **CS9215** happens when the collection type doesn't provide an `Add` method that accepts a single parameter of the element type. The `Add` method must be accessible (typically public) and accept exactly one argument that matches the collection's element type.
186+
- **CS9222** indicates a circular dependency in collection initialization. This occurs when initializing a collection triggers the creation of another instance of the same collection type, which in turn requires initializing another instance, creating an infinite loop. Review your collection type's constructor and initialization logic to break the circular dependency.
162187

163188
The warnings indicates that the collection expression, including any [spread elements](../operators/collection-expressions.md#spread-element) might allocate memory. Creating different storage and converting might be more efficient.
164189

docs/csharp/language-reference/compiler-messages/inline-array-errors.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Resolve errors related to inline arrays
33
description: These compiler errors and warnings are generated when you create an inline array struct that is invalid. This article helps you diagnose and fix those issues.
44
f1_keywords:
5+
- "CS9125"
56
- "CS9164"
67
- "CS9165"
78
- "CS9166"
@@ -18,6 +19,7 @@ f1_keywords:
1819
- "CS9189"
1920
- "CS9259"
2021
helpviewer_keywords:
22+
- "CS9125"
2123
- "CS9164"
2224
- "CS9165"
2325
- "CS9166"
@@ -42,6 +44,7 @@ This article covers the following compiler errors and warnings:
4244
<!-- The text in this list generates issues for Acrolinx, because they don't use contractions.
4345
That's by design. The text closely matches the text of the compiler error / warning for SEO purposes.
4446
-->
47+
- [**CS9125**](#inline-array-declaration): *Attribute parameter 'SizeConst' must be specified.*
4548
- [**CS9164**](#conversions-to-span): *Cannot convert expression to `Span<T>` because it is not an assignable variable*
4649
- [**CS9165**](#conversions-to-span): *Cannot convert expression to `ReadOnlySpan<T>` because it may not be passed or returned by reference*
4750
- [**CS9166**](#element-access): *Index is outside the bounds of the inline array*
@@ -62,6 +65,7 @@ That's by design. The text closely matches the text of the compiler error / warn
6265

6366
You declare inline arrays as a `struct` type with a single field, and an attribute that specifies the length of the array. The compiler generates the following errors for invalid inline array declarations:
6467

68+
- **CS9125**: *Attribute parameter 'SizeConst' must be specified.*
6569
- **CS9167**: *Inline array length must be greater than 0.*
6670
- **CS9168**: *Inline array struct must not have explicit layout.*
6771
- **CS9169**: *Inline array struct must declare one and only one instance field which must not be a ref field.*

docs/csharp/language-reference/compiler-messages/lambda-expression-errors.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ f1_keywords:
1818
- "CS9098" # ERR_ImplicitlyTypedDefaultParameter: Implicitly typed lambda parameter '{0}' cannot have a default value.
1919
- "CS9099" # WRN_OptionalParamValueMismatch: The default parameter value does not match in the target delegate type.
2020
- "CS9100" # WRN_ParamsArrayInLambdaOnly: Parameter has params modifier in lambda but not in target delegate type.
21+
- "CS9236" # INF_TooManyBoundLambdas: Compiling requires binding the lambda expression at least {0} times.
2122
helpviewer_keywords:
2223
- "CS0748"
2324
- "CS0835"
@@ -35,6 +36,7 @@ helpviewer_keywords:
3536
- "CS9098"
3637
- "CS9099"
3738
- "CS9100"
39+
- "CS9236"
3840
ms.date: 05/04/2023
3941
---
4042
# Errors and warnings when using lambda expressions and anonymous functions
@@ -64,6 +66,10 @@ In addition, there are several *warnings* related to declaring and using lambda
6466
- [**CS9099**](#lambda-expression-delegate-type): *The default parameter value does not match in the target delegate type.*
6567
- [**CS9100**](#lambda-expression-delegate-type): *Parameter has params modifier in lambda but not in target delegate type.*
6668

69+
The compiler also produces the following *informational* message:
70+
71+
- [**CS9236**](#syntax-limitations-in-lambda-expressions): *Compiling requires binding the lambda expression at least count times. Consider declaring the lambda expression with explicit parameter types, or if the containing method call is generic, consider using explicit type arguments.*
72+
6773
## Syntax limitations in lambda expressions
6874

6975
Some C# syntax is prohibited in lambda expressions and anonymous methods. Using invalid constructs in a lambda expression causes the following errors:
@@ -90,6 +96,12 @@ In addition, interpolated string handler types are ignored when applied to a lam
9096

9197
- **CS8971**: *InterpolatedStringHandlerArgument has no effect when applied to lambda parameters and will be ignored at the call site.*
9298

99+
Certain expressions cause the compiler to emit the following informational warning:
100+
101+
- **CS9236**: *Compiling requires binding the lambda expression at least count times. Consider declaring the lambda expression with explicit parameter types, or if the containing method call is generic, consider using explicit type arguments.*
102+
103+
The complexity of the lambda expressions and how they invoke other lambda expressions is negatively impacting compiler performance. The reason is that the compiler must infer parameter and argument types through the lambda expressions and the potential types takes time.
104+
93105
## Lambda expression parameters and returns
94106

95107
These errors indicate a problem with a parameter declaration:

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ f1_keywords:
6666
- "CS9199"
6767
- "CS9200"
6868
- "CS9201"
69+
- "CS9205"
6970
- "CS9265"
7071
helpviewer_keywords:
7172
- "CS0192"
@@ -132,6 +133,7 @@ helpviewer_keywords:
132133
- "CS9199"
133134
- "CS9200"
134135
- "CS9201"
136+
- "CS9205"
135137
- "CS9265"
136138
ms.date: 11/06/2024
137139
---
@@ -209,6 +211,7 @@ The following warnings are generated when reference variables are used incorrect
209211
- [**CS9198**](#reference-variable-restrictions): *Reference kind modifier of parameter doesn't match the corresponding parameter in target.*
210212
- [**CS9200**](#reference-variable-restrictions): *A default value is specified for `ref readonly` parameter, but `ref readonly` should be used only for references. Consider declaring the parameter as `in`.*
211213
- [**CS9201**](#reference-variable-restrictions): *Ref field should be ref-assigned before use.*
214+
- [**CS9205**](#incorrect-syntax): *Expected interpolated string.*
212215
- [**CS9265**](#reference-variable-restrictions): *Field is never ref-assigned to, and will always have its default value (null reference)*
213216

214217
These errors and warnings follow these themes:
@@ -228,12 +231,14 @@ These errors indicate that you're using incorrect syntax regarding reference var
228231
- **CS8373**: *The left-hand side of a `ref` assignment must be a ref variable.*
229232
- **CS8388**: *An `out` variable cannot be declared as a ref local.*
230233
- **CS9190**: *`readonly` modifier must be specified after `ref`.*
234+
- **CS9205**: *Expected interpolated string.*
231235

232236
You can correct the error with one of these changes:
233237

234238
- The left operand of an `= ref` operator must be a reference variable. For more information on the correct syntax, see [reference variables](../statements/declarations.md#reference-variables).
235239
- The parameter modifier `ref readonly` must be in that order. `readonly ref` is not a legal parameter modifier. Switch the order of the words.
236240
- A local variable can't be declared as `out`. To declare a local reference variable, use `ref`.
241+
- An `out` argument can't be an interpolated string.
237242

238243
## Reference variable restrictions
239244

0 commit comments

Comments
 (0)