Skip to content

Inconsistent behavior of attribute target specifiers on events #77254

@jjonescz

Description

@jjonescz

Version used: 80ccf3f

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, 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 #77182 (comment).

To get started on implementing this, consider reverting this commit: d38b97c

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions