diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 9016311e2adf4..3ca5bb91c707c 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -32,6 +32,10 @@ "source_path_from_root": "/docs/csharp/misc/cs0123.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/delegate-function-pointer-diagnostics" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0144.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/new-object-creation-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0148.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/delegate-function-pointer-diagnostics" @@ -152,6 +156,10 @@ "source_path_from_root": "/docs/csharp/misc/cs0704.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0712.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/new-object-creation-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0718.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors" @@ -292,6 +300,10 @@ "source_path_from_root": "/docs/csharp/misc/cs1022.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs1022" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1526.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/new-object-creation-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1555.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/entry-point-errors" diff --git a/docs/csharp/language-reference/compiler-messages/new-object-creation-errors.md b/docs/csharp/language-reference/compiler-messages/new-object-creation-errors.md new file mode 100644 index 0000000000000..9997309f728de --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/new-object-creation-errors.md @@ -0,0 +1,81 @@ +--- +title: "Resolve errors and warnings related to `new` expressions and object creation expressions" +description: "This article helps you diagnose and correct compiler errors and warnings related to `new` expressions and object creation expressions" +f1_keywords: + - "CS0144" + - "CS0712" + - "CS1526" + - "CS8181" + - "CS8386" + - "CS8752" + - "CS8753" + - "CS8754" +helpviewer_keywords: + - "CS0144" + - "CS0712" + - "CS1526" + - "CS8181" + - "CS8386" + - "CS8752" + - "CS8753" + - "CS8754" +ms.date: 05/15/2026 +ai-usage: ai-assisted +--- + +# Resolve errors and warnings for `new` expressions and object creation + +This article covers the following compiler errors and warnings: + + + +- [**CS0144**](#types-that-cant-be-instantiated): *Cannot create an instance of the abstract type or interface 'type'* +- [**CS0712**](#types-that-cant-be-instantiated): *Cannot create an instance of the static class 'type'* +- [**CS1526**](#new-expression-syntax-errors): *A new expression requires an argument list or (), [], or {} after type* +- [**CS8181**](#new-expression-syntax-errors): *'new' cannot be used with tuple type. Use a tuple literal expression instead.* +- [**CS8386**](#new-expression-syntax-errors): *Invalid object creation* +- [**CS8752**](#target-typed-new-expressions): *The type 'type' may not be used as the target type of new()* +- [**CS8753**](#target-typed-new-expressions): *Use of new() is not valid in this context* +- [**CS8754**](#target-typed-new-expressions): *There is no target type for 'expression'* + +## Types that can't be instantiated + +- **CS0144**: *Cannot create an instance of the abstract type or interface 'type'* +- **CS0712**: *Cannot create an instance of the static class 'type'* + +The `new` operator can only create instances of concrete, non-static types. The language prohibits instantiating abstract classes, interfaces, and static classes because these types are incomplete or aren't designed to have instances. + +- Create a concrete class that derives from the abstract class, or create a class that implements the interface, then instantiate that concrete type (**CS0144**). You can't use `new` directly on an `abstract` class or an `interface` because they don't provide complete implementations. If you own the type, you can also remove the `abstract` modifier to make the class directly instantiable. +- Remove the `new` expression and access the static class members directly through the class name (**CS0712**). Static classes exist solely to group static members and can't be instantiated. If you need an instance, remove the `static` modifier from the class declaration. + +For more information, see [abstract](../keywords/abstract.md), [interface](../keywords/interface.md), and [Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md). + +## `new` expression syntax errors + +- **CS1526**: *A new expression requires an argument list or (), [], or {} after type* +- **CS8181**: *'new' cannot be used with tuple type. Use a tuple literal expression instead.* +- **CS8386**: *Invalid object creation* + +These errors occur when the syntax of a `new` expression is malformed or when you use `new` with a type that requires a different creation syntax. + +- Add an argument list `()`, array dimensions `[]`, or an initializer `{}` after the type name in a `new` expression (**CS1526**). The `new` operator requires one of these to indicate how the object is constructed. For example, write `new MyClass()` instead of `new MyClass`. +- Replace `new (int, string)(...)` with a tuple literal expression like `(1, "hello")` (**CS8181**). Tuple types use a dedicated literal syntax rather than the `new` operator. To create a tuple, use parenthesized values directly: `(int X, string Y) point = (1, "hello");`. +- Ensure the `new` expression targets a valid constructible type (**CS8386**). This error occurs when the compiler can't determine a valid object creation from the syntax. Verify you're using a type name that supports construction, and that the expression is syntactically complete. + +For more information, see [new operator](../operators/new-operator.md) and [Tuple types](../builtin-types/value-tuples.md). + +## Target-typed `new` expressions + +- **CS8752**: *The type 'type' may not be used as the target type of new()* +- **CS8753**: *Use of new() is not valid in this context* +- **CS8754**: *There is no target type for 'expression'* + +Target-typed `new` expressions (introduced in C# 9) let you omit the type name when the compiler can infer it from context, as in `MyClass x = new();`. These errors occur when the compiler can't determine a valid target type or when the inferred type isn't constructible. + +- Use an explicit type name instead of target-typed `new()` when the target type is an interface, abstract class, static class, or other non-constructible type (**CS8752**). Target-typed `new()` infers the type from the left-hand side, but the inferred type must be a concrete, instantiable type. Write the full `new ConcreteType()` instead. +- Move the `new()` expression to a context where a target type is available (**CS8753**). Target-typed `new` is valid only in contexts where the compiler can determine a type, such as variable declarations with an explicit type, assignment expressions, return statements with a known return type, or argument positions with a known parameter type. You can't use `new()` in contexts like `var x = new();` where no target type exists. +- Provide an explicit type for the `new` expression when no target type can be inferred (**CS8754**). This error occurs when you use `new()` in a position where the compiler has no way to determine what type to construct. Replace `new()` with `new ExplicitType()`, or declare the variable with an explicit type rather than `var`. + +For more information, see [Target-typed new expressions](../operators/new-operator.md#target-typed-new). diff --git a/docs/csharp/language-reference/compiler-messages/source-generator-errors.md b/docs/csharp/language-reference/compiler-messages/source-generator-errors.md index 5d82a64d12907..861f7d1bf43c5 100644 --- a/docs/csharp/language-reference/compiler-messages/source-generator-errors.md +++ b/docs/csharp/language-reference/compiler-messages/source-generator-errors.md @@ -2,6 +2,16 @@ title: Errors and warnings associated with source generators and interceptors description: You might see these errors and warnings when code is compiled with source generators and interceptors. They indicate some condition where the compiler can't run the source generator, or the generate code isn't compilable. f1_keywords: + - "CS8032" + - "CS8033" + - "CS8034" + - "CS8040" + - "CS8700" + - "CS8784" + - "CS8785" + - "CS8850" + - "CS9057" + - "CS9067" - "CS9137" - "CS9138" - "CS9139" @@ -37,9 +47,17 @@ f1_keywords: - "CS9234" - "CS9235" - "CS9270" +helpviewer_keywords: + - "CS8032" + - "CS8033" + - "CS8034" + - "CS8040" + - "CS8700" + - "CS8784" + - "CS8785" + - "CS8850" - "CS9057" - "CS9067" -helpviewer_keywords: - "CS9137" - "CS9138" - "CS9139" @@ -75,52 +93,56 @@ helpviewer_keywords: - "CS9234" - "CS9235" - "CS9270" - - "CS9057" - - "CS9067" ms.date: 01/27/2026 --- # Errors and warnings associated with source generators and interceptors -The following errors are generated when source generators or interceptors are loaded during a compilation: +The following errors occur when the compiler loads source generators or interceptors during compilation: -- [**CS9137**](#interceptors-are-experimental): *The 'interceptors' experimental feature is not enabled. Add `InterceptorsPreview` to your project.* -- [**CS9138**](#incorrect-interceptor-declaration): *Method cannot be used as an interceptor because it or its containing type has type parameters.* +- [**CS9137**](#interceptors-are-experimental): *The 'interceptors' feature is not enabled in this namespace. Add `InterceptorsPreview` to your project.* +- [**CS9138**](#incorrect-interceptor-declaration): *Method 'method' cannot be used as an interceptor because its containing type has type parameters.* - [**CS9139**](#incorrect-mapping): *Cannot intercept: compilation does not contain a file with path.* -- [**CS9140**](#incorrect-mapping): *Cannot intercept: compilation does not contain a file with path. Did you mean to use a different path?* -- [**CS9141**](#incorrect-mapping): *The provided line and character number does not refer to an interceptable method name, but rather to a token.* +- [**CS9140**](#incorrect-mapping): *Cannot intercept: compilation does not contain a file with path 'path'. Did you mean to use path 'candidatePath'?* +- [**CS9141**](#incorrect-mapping): *The provided line and character number does not refer to an interceptable method name, but rather to token 'token'.* - [**CS9142**](#incorrect-mapping): *The given file has `n` lines, which is fewer than the provided line number `m`.* - [**CS9143**](#incorrect-mapping): *The given line is `c` characters long, which is fewer than the provided character number `n`.* - [**CS9144**](#signature-mismatch): *Cannot intercept method `M` with interceptor `V` because the signatures do not match.* - [**CS9145**](#incorrect-mapping): *Cannot intercept: Path is unmapped. Expected mapped path.* - [**CS9146**](#incorrect-interceptor-declaration): *An interceptor method must be an ordinary member method.* -- [**CS9147**](#incorrect-mapping): *The provided line and character number does not refer to the start of a token. Did you mean to use line `n` and character `c`?* +- [**CS9147**](#incorrect-mapping): *The provided line and character number does not refer to the start of token 'token'. Did you mean to use line 'n' and character 'c'?* - [**CS9148**](#signature-mismatch): *Interceptor must have a `this` parameter matching parameter.* - [**CS9149**](#signature-mismatch): *Interceptor must not have a `this` parameter because method does not have a `this` parameter.* - [**CS9150**](#incorrect-mapping): *Interceptor cannot have a `null` file path.* - [**CS9151**](#incorrect-interceptor-declaration): *Possible method name `M` cannot be intercepted because it is not being invoked.* -- [**CS9152**](#incorrect-interceptor-declaration): *Cannot intercept a call in file with this path because multiple files in the compilation have this path.* +- [**CS9152**](#incorrect-interceptor-declaration): *Cannot intercept a call in file with path 'path' because multiple files in the compilation have this path.* - [**CS9153**](#incorrect-interceptor-declaration): *The indicated call is intercepted multiple times.* - [**CS9155**](#signature-mismatch): *Cannot intercept call with `M` because it is not accessible within `V`.* - [**CS9156**](#signature-mismatch): *Cannot intercept call to `M` with `V` because of a difference in 'scoped' modifiers or `[UnscopedRef]` attributes.* - [**CS9157**](#incorrect-mapping): *Line and character numbers provided to `InterceptsLocationAttribute` must be positive.* - [**CS9160**](#incorrect-interceptor-declaration): *A nameof operator cannot be intercepted.* - [**CS9161**](#incorrect-interceptor-declaration): *An interceptor cannot be marked with `UnmanagedCallersOnlyAttribute`.* -- [**CS9177**](#signature-mismatch): *Interceptor must be non-generic or have matching arity.* -- [**CS9178**](#signature-mismatch): *Method must be non-generic to match* +- [**CS9177**](#signature-mismatch): *Method 'method' must be non-generic or have arity 'arity' to match 'target'.* +- [**CS9178**](#signature-mismatch): *Method 'method' must be non-generic to match 'target'.* - [**CS9206**](#incorrect-interceptor-declaration): *An interceptor cannot be declared in the global namespace.* -- [**CS9207**](#incorrect-interceptor-declaration): *Cannot intercept because method is not an invocation of an ordinary member method.* +- [**CS9207**](#incorrect-interceptor-declaration): *Cannot intercept 'method' because it is not an invocation of an ordinary member method.* - [**CS9231**](#incorrect-interceptor-declaration): *The data argument to InterceptsLocationAttribute is not in the correct format.* - [**CS9232**](#incorrect-interceptor-declaration): *Version 'version' of the interceptors format is not supported. The latest supported version is '1'.* - [**CS9233**](#incorrect-interceptor-declaration): *Cannot intercept a call in file 'file' because it is duplicated elsewhere in the compilation.* - [**CS9234**](#incorrect-interceptor-declaration): *Cannot intercept a call in file 'file' because a matching file was not found in the compilation.* - [**CS9235**](#incorrect-interceptor-declaration): *The data argument to InterceptsLocationAttribute refers to an invalid position in file 'file'.* -The following warnings are generated when source generators or interceptors are loaded during a compilation: +The following warnings occur when the compiler loads source generators or interceptors during compilation: -- [**CS8784**](#incorrect-interceptor-declaration): *Generator '`YourSourceGeneratorName`' failed to initialize. It will not contribute to the output and compilation errors may occur as a result.* -- [**CS8785**](#incorrect-interceptor-declaration): *Generator '`YourSourceGeneratorName`' failed to generate source. It will not contribute to the output and compilation errors may occur as a result.* -- [**CS9057**](#analyzer-compatibility): *Analyzer assembly cannot be used because it references a newer version of the compiler than the currently running version.* -- [**CS9067**](#analyzer-compatibility): *Analyzer reference specified multiple times.* +- [**CS8032**](#analyzer-and-source-generator-compatibility): *An instance of analyzer 'analyzer' cannot be created from 'assembly' : 'reason'.* +- [**CS8033**](#analyzer-and-source-generator-compatibility): *The assembly 'assembly' does not contain any analyzers.* +- [**CS8034**](#analyzer-and-source-generator-compatibility): *Unable to load Analyzer assembly 'assembly' : 'reason'* +- [**CS8040**](#analyzer-and-source-generator-compatibility): *Skipping some types in analyzer assembly 'assembly' due to a ReflectionTypeLoadException : 'exception'.* +- [**CS8700**](#analyzer-and-source-generator-compatibility): *Multiple analyzer config files cannot be in the same directory ('directory').* +- [**CS8784**](#source-generator-failures): *Generator 'generator' failed to initialize. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'exception' with message 'message'.* +- [**CS8785**](#source-generator-failures): *Generator 'generator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'exception' with message 'message'.* +- [**CS8850**](#analyzer-and-source-generator-compatibility): *The assembly 'assembly' containing type 'type' references .NET Framework, which is not supported.* +- [**CS9057**](#analyzer-and-source-generator-compatibility): *Analyzer assembly 'assembly' cannot be used because it references version 'version' of the compiler, which is newer than the currently running version 'currentVersion'.* +- [**CS9067**](#analyzer-and-source-generator-compatibility): *Analyzer reference 'reference' specified multiple times* - [**CS9154**](#signature-mismatch): *Intercepting a call to `M` with interceptor `V`, but the signatures do not match.* - [**CS9158**](#signature-mismatch): *Nullability of reference types in return type doesn't match interceptable method.* - [**CS9159**](#signature-mismatch): *Nullability of reference types in type of parameter doesn't match interceptable method.* @@ -128,11 +150,49 @@ The following warnings are generated when source generators or interceptors are These errors and warnings follow these themes: +## Source generator failures + +- **CS8784**: *Generator 'generator' failed to initialize. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'exception' with message 'message'.* +- **CS8785**: *Generator 'generator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'exception' with message 'message'.* + +These warnings indicate that a source generator threw an unhandled exception during compilation. Source generators run as part of the build pipeline, and a failure prevents the generator from contributing its output to the compilation. For more information about implementing source generators, see [Source Generators](../../roslyn-sdk/index.md#source-generators). + +To correct these warnings, follow this guidance: + +- Examine the full exception details in the build output to identify the root cause (**CS8784**, **CS8785**). The warning message includes the exception type, message, and stack trace, which can help you or the generator author diagnose the failure. +- Ensure all required dependencies for the source generator are available in the project (**CS8784**). Initialization failures often occur when the generator can't locate a required assembly or resource during startup. Verify that the generator's NuGet package is correctly installed and all transitive dependencies are resolved. +- Report the failure to the source generator's maintainer if the exception originates in third-party generator code (**CS8784**, **CS8785**). Include the full exception details from the build output in your bug report. +- If you authored the source generator, add exception handling within your `Initialize` or `Execute` methods to catch and report errors gracefully (**CS8784**, **CS8785**). + +## Analyzer and source generator compatibility + +- **CS8032**: *An instance of analyzer 'analyzer' cannot be created from 'assembly' : 'reason'.* +- **CS8033**: *The assembly 'assembly' does not contain any analyzers.* +- **CS8034**: *Unable to load Analyzer assembly 'assembly' : 'reason'* +- **CS8040**: *Skipping some types in analyzer assembly 'assembly' due to a ReflectionTypeLoadException : 'exception'.* +- **CS8700**: *Multiple analyzer config files cannot be in the same directory ('directory').* +- **CS8850**: *The assembly 'assembly' containing type 'type' references .NET Framework, which is not supported.* +- **CS9057**: *Analyzer assembly 'assembly' cannot be used because it references version 'version' of the compiler, which is newer than the currently running version 'currentVersion'.* +- **CS9067**: *Analyzer reference 'reference' specified multiple times* + +These warnings and errors indicate problems with loading, instantiating, or configuring analyzer and source generator assemblies. Because source generators load as analyzer assemblies, these diagnostics apply to both analyzers and source generators. + +To correct these problems, follow this guidance: + +- Upgrade your .NET SDK or compiler version to match the analyzer's requirements, or use an older version of the analyzer package that is compatible with your current compiler (**CS9057**). This error occurs when an analyzer assembly targets a newer Roslyn API version than the running compiler provides. +- Remove duplicate analyzer references from your project (**CS9067**). Check your project file for repeated `` or `` elements that include the same analyzer assembly through different paths. Transitive package dependencies can also cause duplicates. +- Verify the analyzer assembly is a valid .NET assembly and isn't corrupted (**CS8034**). If the assembly can't be loaded at all, ensure the file exists at the referenced path and was built for a compatible target framework. Rebuild or reinstall the NuGet package to get a fresh copy. +- Ensure the analyzer type has a public parameterless constructor (**CS8032**). The compiler instantiates analyzers by calling their default constructor. If the constructor throws an exception or requires parameters, the analyzer can't be created. +- Verify the referenced assembly actually exports analyzer or source generator types (**CS8033**). An assembly referenced as an analyzer must contain types that implement `DiagnosticAnalyzer` or `ISourceGenerator`/`IIncrementalGenerator`. If the assembly is a runtime dependency rather than an analyzer, reference it as a normal project or package reference instead. +- Resolve type loading problems in the analyzer assembly by ensuring all its dependencies are available (**CS8040**). A `ReflectionTypeLoadException` typically means the assembly references types from other assemblies that can't be found. Make sure all required dependencies are included in the analyzer's NuGet package. +- Ensure analyzer config files (`.editorconfig` or `.globalconfig`) are unique per directory (**CS8700**). The compiler doesn't allow multiple analyzer configuration files in the same directory because the precedence rules would be ambiguous. Merge the files or remove the duplicate. +- Update the analyzer or source generator to target .NET Standard or modern .NET instead of .NET Framework (**CS8850**). Assemblies that reference .NET Framework aren't supported in the modern .NET compiler toolchain. If you maintain the analyzer, retarget it to `netstandard2.0`. If it's a third-party package, upgrade to a version that supports modern .NET. + ## Interceptors are experimental -- **CS9137**: *The 'interceptors' experimental feature is not enabled. Add `InterceptorsPreview` to your project.* +- **CS9137**: *The 'interceptors' feature is not enabled in this namespace. Add `InterceptorsPreview` to your project.* -To use interceptors, add the `InterceptorsPreview` element to your project file within a `` section (**CS9137**), because interceptors are an experimental feature that isn't enabled by default. This explicit opt-in is required because the interceptors feature is subject to breaking changes or removal in future releases, and the compiler needs confirmation that you understand the risks before allowing its use. For more information about interceptors and their capabilities, see [Interceptors](../../whats-new/csharp-12.md#interceptors) in the C# 12 features documentation. +To use interceptors, add the `InterceptorsPreview` element to your project file within a `` section (**CS9137**). Interceptors are an experimental feature and aren't enabled by default. This explicit opt-in is required because the interceptors feature is subject to breaking changes or removal in future releases. The compiler needs confirmation that you understand the risks before allowing its use. For more information about interceptors and their capabilities, see [Interceptors](../../whats-new/csharp-12.md#interceptors) in the C# 12 features documentation. ## Signature mismatch @@ -143,8 +203,8 @@ The following errors and warnings indicate a mismatch between the interceptor me - **CS9149**: *Interceptor must not have a `this` parameter because method does not have a `this` parameter.* - **CS9155**: *Cannot intercept call with `M` because it is not accessible within `V`.* - **CS9156**: *Cannot intercept call to `M` with `V` because of a difference in 'scoped' modifiers or `[UnscopedRef]` attributes.* -- **CS9177**]: *Interceptor must be non-generic or have matching arity.* -- **CS9178**: *Method must be non-generic to match* +- **CS9177**: *Method 'method' must be non-generic or have arity 'arity' to match 'target'.* +- **CS9178**: *Method 'method' must be non-generic to match 'target'.* In addition, the following warnings indicate a mismatch in the signatures of the interceptor and the interceptable method: @@ -158,7 +218,7 @@ To correct these issues, ensure your interceptor method matches the interceptabl - Ensure the interceptor method signature exactly matches the interceptable method (**CS9144**, **CS9154**). The parameter types, modifiers, order, and return type must be identical. Review both method declarations and align their signatures. - Add a `this` parameter to your interceptor when the interceptable method is an instance method (**CS9148**), or remove the `this` parameter when the interceptable method is static (**CS9149**). Instance interceptors require a `this` parameter of the declaring type, while static interceptors must not have one. - Declare your interceptor in a location where the interceptable method is accessible (**CS9155**). If the interceptable method is `internal`, the interceptor must be in the same assembly. If it's `private`, the interceptor must be in the same type or a nested type. -- Match the `scoped` modifiers and `[UnscopedRef]` attributes on corresponding `ref` parameters (**CS9156**). Each `ref` parameter in the interceptor must have the same lifetime annotations as the corresponding parameter in the interceptable method to ensure memory safety. +- Match the [`scoped`](../statements/declarations.md#scoped-ref) modifiers and [`[UnscopedRef]`](../attributes/general.md#unscopedref-attribute) attributes on corresponding `ref` parameters (**CS9156**). Each `ref` parameter in the interceptor must have the same lifetime annotations as the corresponding parameter in the interceptable method to ensure memory safety. - Ensure both methods have matching generic arity (**CS9177**, **CS9178**). If the interceptable method is non-generic, the interceptor must also be non-generic. If the interceptable method has type parameters, the interceptor must have the same number of type parameters with compatible constraints. - Match the nullability annotations in the return type (**CS9158**) and parameter types (**CS9159**). Enable nullable reference types in your project and ensure the interceptor's nullability annotations match the interceptable method exactly to maintain type safety. - Use the updated `InterceptableLocation`-based generation for `InterceptsLocationAttribute` instead of the deprecated `(string, int, int)` constructor (**CS9270**). The newer format provides better tooling support and compile-time validation. See the [GitHub issue](https://github.com/dotnet/roslyn/issues/72133) for migration guidance. @@ -168,12 +228,12 @@ To correct these issues, ensure your interceptor method matches the interceptabl Interceptors require a source mapping that maps the interceptable method and the interceptor method. The following errors indicate an issue with the mapping: - **CS9139**: *Cannot intercept: compilation does not contain a file with path.* -- **CS9140**: *Cannot intercept: compilation does not contain a file with path. Did you mean to use a different path?* -- **CS9141**: *The provided line and character number does not refer to an interceptable method name, but rather to a token.* +- **CS9140**: *Cannot intercept: compilation does not contain a file with path 'path'. Did you mean to use path 'candidatePath'?* +- **CS9141**: *The provided line and character number does not refer to an interceptable method name, but rather to token 'token'.* - **CS9142**: *The given file has `n` lines, which is fewer than the provided line number `m`.* - **CS9143**: *The given line is `c` characters long, which is fewer than the provided character number `n`.* - **CS9145**: *Cannot intercept: Path is unmapped. Expected mapped path.* -- **CS9147**: *The provided line and character number does not refer to the start of a token. Did you mean to use line `n` and character `c`?* +- **CS9147**: *The provided line and character number does not refer to the start of token 'token'. Did you mean to use line 'n' and character 'c'?* - **CS9150**: *Interceptor cannot have a `null` file path.* - **CS9157**: *Line and character numbers provided to `InterceptsLocationAttribute` must be positive.* @@ -188,17 +248,17 @@ To correct mapping errors, ensure your `InterceptsLocationAttribute` contains va ## Incorrect interceptor declaration -The following errors indicate issues with interceptor declarations, including problems with the `InterceptsLocationAttribute` format or violations of interceptor rules: +The following errors indicate problems with interceptor declarations, including issues with the `InterceptsLocationAttribute` format or violations of interceptor rules: -- **CS9138**: *Method cannot be used as an interceptor because it or its containing type has type parameters.* +- **CS9138**: *Method 'method' cannot be used as an interceptor because its containing type has type parameters.* - **CS9146**: *An interceptor method must be an ordinary member method.* - **CS9151**: *Possible method name `M` cannot be intercepted because it is not being invoked.* -- **CS9152**: *Cannot intercept a call in file with this path because multiple files in the compilation have this path.* +- **CS9152**: *Cannot intercept a call in file with path 'path' because multiple files in the compilation have this path.* - **CS9153**: *The indicated call is intercepted multiple times.* - **CS9160**: *A nameof operator cannot be intercepted.* - **CS9161**: *An interceptor cannot be marked with `UnmanagedCallersOnlyAttribute`.* - **CS9206**: *An interceptor cannot be declared in the global namespace.* -- **CS9207**: *Cannot intercept because method is not an invocation of an ordinary member method.* +- **CS9207**: *Cannot intercept 'method' because it is not an invocation of an ordinary member method.* - **CS9231**: *The data argument to InterceptsLocationAttribute is not in the correct format.* - **CS9232**: *Version 'version' of the interceptors format is not supported. The latest supported version is '1'.* - **CS9233**: *Cannot intercept a call in file 'file' because it is duplicated elsewhere in the compilation.* @@ -210,24 +270,12 @@ To correct interceptor declaration errors, follow these rules for valid intercep - Format the `InterceptsLocationAttribute` data argument correctly (**CS9231**). The attribute requires specifically structured data that encodes file path and position information. Ensure your source generator produces data in the expected format matching the current interceptors specification. - Use version '1' in your `InterceptsLocationAttribute` (**CS9232**), because it's the latest supported version. Update your source generator to output version 1 format attributes rather than unsupported version numbers. - Ensure unique file paths in your compilation (**CS9233**, **CS9234**). When the compilation contains duplicate file paths, rename or reorganize files to make each path unique. Verify that the file path in the attribute matches a file actually included in the compilation. -- Validate position data points to valid code locations (**CS9235**). The line and character numbers must reference a valid interception point within the specified file. Regenerate the attribute if the source file has changed or if the position falls outside the file's bounds. -- Declare non-generic interceptor methods in non-generic types (**CS9138**). Interceptors can't have type parameters on the method itself or on their containing type. If you need to intercept a generic method, create a non-generic interceptor that works with the specific constructed type. +- Validate position data points to valid code locations (**CS9235**). The line and character numbers must reference a valid interception point within the specified file. Regenerate the attribute if the source file changed or if the position falls outside the file's bounds. +- Declare non-generic interceptor methods in non-generic types (**CS9138**). Interceptors can't be declared in types that have type parameters. If you need to intercept a generic method, create a non-generic interceptor in a non-generic containing type that works with the specific constructed type. - Make interceptors ordinary member methods (**CS9146**). Interceptors can't be operators, constructors, finalizers, properties, or indexers. Declare your interceptor as a regular static or instance method. - Intercept actual method invocations, not expressions (**CS9151**, **CS9207**). You can only intercept calls to ordinary member methods that are being invoked. You can't intercept method groups, delegates, or methods referenced without being called. Ensure the interceptable location identifies an actual method call. - Remove duplicate interception attempts (**CS9153**). Each method call can only be intercepted once. If multiple `InterceptsLocationAttribute` instances target the same call, remove all but one to resolve the ambiguity. - Don't intercept `nameof` operators (**CS9160**). The `nameof` operator doesn't invoke methods at runtime, so it can't be intercepted. Only intercept actual method calls that execute at runtime. - Remove `UnmanagedCallersOnlyAttribute` from interceptors (**CS9161**). Interceptors must be callable from managed code and can't be marked with `UnmanagedCallersOnlyAttribute`. Remove the attribute from your interceptor method declaration. - Declare interceptors within a namespace (**CS9206**). Interceptors can't be declared in the global namespace and must be contained within at least one namespace declaration. Wrap your interceptor class in a namespace. -- Resolve duplicate file paths at the compilation level (**CS9152**). When multiple files share the same path in the compilation, the compiler can't determine which file to intercept in. Ensure build configuration produces unique file paths or use a different organization strategy for your source files. - -## Analyzer compatibility - -The following warnings indicate issues with analyzer or source generator assemblies: - -- **CS9057**: *Analyzer assembly cannot be used because it references a newer version of the compiler than the currently running version.* -- **CS9067**: *Analyzer reference specified multiple times.* - -These warnings occur when there are compatibility issues with analyzer assemblies: - -- **CS9057** is generated when an analyzer assembly references a version of the Roslyn compiler that is newer than the one currently running. This prevents the analyzer from loading because it might depend on APIs or behaviors not available in the current compiler version. To resolve this, either upgrade your compiler/SDK to match the analyzer's requirements or use a version of the analyzer compatible with your current compiler version. -- **CS9067** warns when the same analyzer assembly is referenced multiple times in your project. This typically happens when an analyzer is included through multiple paths or package references. While not an error, duplicate references can impact build performance and might cause unexpected behavior. Remove the duplicate references to resolve this warning. +- Resolve duplicate file paths at the compilation level (**CS9152**). When multiple files share the same path in the compilation, the compiler can't determine which file to intercept. Ensure build configuration produces unique file paths or use a different organization strategy for your source files. diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index c37d4a6caa482..ece724654afb2 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -555,6 +555,11 @@ items: CS0706, CS0717, CS0718, CS1720, CS1763, CS1948, CS1960, CS1961, CS3024, CS7002, CS8322, CS8375, CS8377, CS8379, CS8380, CS8387, CS8389, CS8427, CS8665, CS8666, CS8822, CS8823, CS8893, CS8894, CS8895, CS8896, CS9011, CS9012, CS9338 + - name: "`new` expressions and object creation" + href: ./compiler-messages/new-object-creation-errors.md + displayName: > + new, object creation, target-typed new, + CS0144, CS0712, CS1526, CS8181, CS8386, CS8752, CS8753, CS8754 - name: Asynchronous methods href: ./compiler-messages/async-await-errors.md displayName: > @@ -705,10 +710,11 @@ items: - name: Source generators href: ./compiler-messages/source-generator-errors.md displayName: > - CS9057, CS9067, CS9137, CS9138, CS9139, CS9140, CS9141, CS9142, CS9143, CS9144, - CS9145, CS9146, CS9147, CS9148, CS9149, CS9150, CS9151, CS9152, CS9153, CS9154, - CS9155, CS9156, CS9157, CS9158, CS9159, CS9160, CS9161, CS9177, CS9178, CS9206, - CS9207, CS9231, CS9232, CS9233, CS9234, CS9235, CS9270 + CS8032, CS8033, CS8034, CS8040, CS8700, CS8784, CS8785, CS8850, CS9057, CS9067, + CS9137, CS9138, CS9139, CS9140, CS9141, CS9142, CS9143, CS9144, CS9145, CS9146, + CS9147, CS9148, CS9149, CS9150, CS9151, CS9152, CS9153, CS9154, CS9155, CS9156, + CS9157, CS9158, CS9159, CS9160, CS9161, CS9177, CS9178, CS9206, CS9207, CS9231, + CS9232, CS9233, CS9234, CS9235, CS9270 - name: File local types href: ./compiler-messages/file-local-types.md displayName: > @@ -899,8 +905,6 @@ items: href: ../misc/cs0140.md - name: CS0143 href: ../misc/cs0143.md - - name: CS0144 - href: ../misc/cs0144.md - name: CS0145 href: ../misc/cs0145.md - name: CS0146 @@ -1173,8 +1177,6 @@ items: href: ../misc/cs0709.md - name: CS0711 href: ../misc/cs0711.md - - name: CS0712 - href: ../misc/cs0712.md - name: CS0713 href: ../misc/cs0713.md - name: CS0714 @@ -1331,8 +1333,6 @@ items: href: ../misc/cs1524.md - name: CS1525 href: ../misc/cs1525.md - - name: CS1526 - href: ../misc/cs1526.md - name: CS1527 href: ../misc/cs1527.md - name: CS1528 diff --git a/docs/csharp/misc/cs0144.md b/docs/csharp/misc/cs0144.md deleted file mode 100644 index 14fa1da972b36..0000000000000 --- a/docs/csharp/misc/cs0144.md +++ /dev/null @@ -1,39 +0,0 @@ ---- -description: "Compiler Error CS0144" -title: "Compiler Error CS0144" -ms.date: 07/20/2015 -f1_keywords: - - "CS0144" -helpviewer_keywords: - - "CS0144" -ms.assetid: 3904cab1-05bd-44ec-81d0-e36c5656f742 ---- -# Compiler Error CS0144 - -Cannot create an instance of the abstract class or interface 'interface' - - You cannot create an instance of an [abstract](../language-reference/keywords/abstract.md) class or an [interface](../language-reference/keywords/interface.md). For more information, see [Interfaces](../fundamentals/types/interfaces.md). - - The following sample generates CS0144: - -```csharp -// CS0144.cs -interface MyInterface -{ -} -public class MyClass -{ - public static void Main() - { - MyInterface myInterface = new MyInterface (); // CS0144 - } -} -``` - -## How to fix violations - -You can solve this problem by implementing one of the two following solutions: - -1. Change the type declaration so that it's not abstract: Either remove the abstract keyword from the class declaration, or change the type from an interface to a class. - -2. Create a type that's derived from the abstract class or that implements the interface. diff --git a/docs/csharp/misc/cs0712.md b/docs/csharp/misc/cs0712.md deleted file mode 100644 index 9fefb8f7c7781..0000000000000 --- a/docs/csharp/misc/cs0712.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Compiler Error CS0712" -title: "Compiler Error CS0712" -ms.date: 07/20/2015 -f1_keywords: - - "CS0712" -helpviewer_keywords: - - "CS0712" -ms.assetid: cde64c0c-3953-4563-8c24-8b646230bbb8 ---- -# Compiler Error CS0712 - -Cannot create an instance of the static class 'static class' - - It is not possible to create instances of static classes. Static classes are designed to contain static fields and methods, but may not be instantiated. - -## Example - - The following sample generates CS0712: - -```csharp -// CS0712.cs -public static class SC -{ -} - -public class CMain -{ - public static void Main() - { - SC sc = new SC(); // CS0712 - } -} -``` diff --git a/docs/csharp/misc/cs1526.md b/docs/csharp/misc/cs1526.md deleted file mode 100644 index f261a251b0924..0000000000000 --- a/docs/csharp/misc/cs1526.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS1526" -title: "Compiler Error CS1526" -ms.date: 07/20/2015 -f1_keywords: - - "CS1526" -helpviewer_keywords: - - "CS1526" -ms.assetid: 92feeb9f-e577-4c08-b12b-c19822857200 ---- -# Compiler Error CS1526 - -A new expression requires (), [], or {} after type - - The [new](../language-reference/operators/new-operator.md) operator, used to dynamically allocate memory for an object, was not specified correctly. - -## Example - - The following sample shows how to use `new` to allocate space for an array and an object. - -```csharp -// CS1526.cs -public class y -{ - public static int globalCounter = 0; - public int instanceCounter = 0; -} - -public class z -{ - public static void Main() - { - y yInstance = new y; // CS1526 - y[] yArray = new y[10]; // Array of Ys - - for (int i = 0; i < yArray.Length; i++) - yArray[i] = new y(); // an object of type y - } -} -``` diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index 07cc0c45bb3e4..dbf455651acd1 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -141,12 +141,8 @@ f1_keywords: - "CS8027" - "CS8028" - "CS8029" - - "CS8032" - - "CS8033" - - "CS8034" - "CS8035" # build only diagnostic - "CS8036" - - "CS8040" - "CS8055" - "CS8057" - "CS8058" @@ -190,7 +186,6 @@ f1_keywords: - "CS8138" - "CS8179" - "CS8180" - - "CS8181" - "CS8182" - "CS8183" - "CS8184" @@ -243,7 +238,6 @@ f1_keywords: - "CS8383" - "CS8384" - "CS8385" - - "CS8386" # C# 8.0 diagnostics - "CS8412" - "CS8413" @@ -266,7 +260,6 @@ f1_keywords: - "CS8656" - "CS8662" - "CS8669" - - "CS8700" - "CS8701" - "CS8702" - "CS8707" @@ -276,16 +269,11 @@ f1_keywords: - "CS8750" # C# 9 diagnostics start here - "CS8751" # misc - - "CS8752" # target type new - - "CS8753" - - "CS8754" - "CS8760" # misc - "CS8761" - "CS8771" - "CS8772" - "CS8783" # local function - - "CS8784" # source generator - - "CS8785" - "CS8830" # feature / version - "CS8831" - "CS8888" # feature / version