dotnet / dotnet/roslyn

Improve CS0655 diagnostic for nullable attribute property types

Open
#84,119 0 comments 0 reactions 0 assignees View on GitHub
Area-Compilers Concept-Diagnostic Clarity
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

## Summary

CS0655 can be misleading when a named attribute argument targets a property whose type is not a valid attribute parameter type.

In this example, the actual issue is that the attribute property has type `Developers?`, which is not a valid attribute parameter type. However, the diagnostic message appears to suggest that the named argument itself (`Developers`) is invalid, rather than indicating that the property's type is the root of the problem.

## Background and Motivation
This became confusing after refactoring the property from a derived attribute into a shared base attribute class.
It appeared that moving the property caused the issue, while the actual cause was that the property's type had become nullable.

The current wording requires knowledge of the attribute parameter type restrictions to understand that the failure is caused by Developers?, not by the named argument itself.

Image

Image

Steps to recreate:

```
public enum Developers
{
Andi
}

public abstract class ToDoAttributeBase : Attribute
{
public Developers? Developer { get; set; }
}

public class ToDoRenameAttribute(string something)
: ToDoAttributeBase
{ }

[ToDoRename("SomeForm", Developer = Developers.Peter)]
public partial class LookupEditToVForm
{ }
```

## Proposed Feature

Instead of the actual result, which is this error message: CS0655: 'Developer' is not a valid named attribute argument because it is not a valid attribute parameter type, the diagnostic should make it more obvious that:

1. Developer is the name of the attribute property being assigned.
2. The actual problem is the property's type (Developers?), not the property itself.
3. Nullable value types are not valid attribute parameter types, even when exposed through a public attribute property.

## Alternative Designs
The alternative would be to make the diagnostic intuitively easier by displaying something along the lines of:
"Attribute property 'Developer' has type 'Developers?', but nullable value types cannot be used as attribute parameter types."
or
"Named attribute argument 'Developer' is invalid because its type 'Developers?' is not a valid attribute parameter type."

The compiler's behavior is correct, and this request is solely about improving the diagnostic message to make the underlying cause easier to identify.

Contributor guide

Open the contributing guide

Research direction

Start by building the C# reproduction in the issue and observing CS0655 for the nullable Developers? property. Locate the compiler diagnostic definition and its tests by searching for CS0655, then update the wording so it identifies Developer and Developers? as the cause. Done means the diagnostic clearly explains that nullable value types are invalid attribute parameter types and existing tests still pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.