This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Proposal: Repeated Attributes in Partial Members #3658
Labels
Needs Implementation
The specification for this issue has been approved, it needs an implementation
Proposal champion
Milestone
Repeated Attributes in Partial Members
Summary
Allow each declaration of a partial member to independently apply an attribute not marked with
[AttributeUsage(AllowMultiple = true)]
, as long as the attribute arguments are identical in all applications.Motivation
When considering what attributes are present on a 'partial' method, the language unions together all the attributes in all corresponding positions on both declarations. For example, the method
M
below has attributesA
andB
.This means that attributes which are not marked
[AttributeUsage(AllowMultiple = true)]
cannot be present across both parts:This presents a usability/readability issue, because some attributes are designed to inform the user and/or maintainer of the method of what pre/postconditions or invariants the method requires. For example:
A partial member typically facilitates the relationship between a code generator and an end user--each party provides one of the declarations of the partial member in order for a code generator to provide functionality to the user, or for the user to access an extension point in generated code. In the situation where only one declaration is allowed to have these single-application attributes, the generator and the user can't effectively communicate their requirements to each other. If a generator produces a defining declaration with a
NotNullWhen
attribute, for instance, the user cannot write an implementing declaration with that same attribute, even though the postcondition is applicable to the implementation, and checked by the compiler. This creates confusion for users when tracking down the root causes of warnings or when trying to understand the behaviors of a method.Solution
Allow a non-AllowMultiple attribute to be used once on each symbol (member, return, parameter, etc.) in each partial declaration, as long as the attribute arguments are identical. Since attribute arguments are all constants, the compiler can verify this. When attributes are unioned across declarations, each non-AllowMultiple attribute will be de-duplicated and only one instance of the attribute will be emitted.
Open questions
Design meetings
6th July 2020
The proposal is accepted.
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-09-26.md#ungrouped
The text was updated successfully, but these errors were encountered: