Inconsistent behavior of attribute target specifiers on events
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
**Version used:** https://github.com/dotnet/roslyn/commit/80ccf3fe1914f0df2b3745e29fadfa7f87dd6431
```cs
using System;
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class A : Attribute { public A(int i) { } }
class C
{
[A(1)] [method: A(2)] [param: A(3)] [return: A(4)] [field: A(5)] event Action E;
}
```
- Warning is reported that `[param:]` is an invalid attribute location. However, the attribute is actually emitted to metadata for the param. This seems like a compiler bug. Per the current [spec](https://github.com/dotnet/csharpstandard/blob/bebb8358d440a0e4ae7ec3546fd1740ce287de30/standard/attributes.md#223-attribute-specification), the warning is correct, the emit is wrong. Ideally, the spec would be changed to allow this. Alternatively, the compiler could be fixed to not emit the attribute - that's a breaking change, though.
- It feels like `[param:]` and `[return:]` should be allowed for field-like and `extern` events. Those don't have user-defined accessors so there is no other way to specify such attributes. This would need a spec change.
- The spec doesn't seem to allow `[method:]` on `extern` events either (it's allowed only on field-like events), but that's currently allowed by the compiler. Ideally, the spec would be changed to allow this.
See also https://github.com/dotnet/roslyn/pull/77182#discussion_r1954227418.
To get started on implementing this, consider reverting this commit: https://github.com/dotnet/roslyn/pull/77182/commits/d38b97cac91f9ea01edc857dec54cafb40b2e974
Contributor guide
Research direction
Start with the supplied C# repro and inspect the behavior introduced by commit d38b97cac91f9ea01edc857dec54cafb40b2e974. Compare the warning and emitted metadata for each event attribute target with the linked specification and PR discussion; done means the compiler behavior and specification agree for field-like and extern events.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100