diff --git a/standard/attributes.md b/standard/attributes.md index 21983344c..2550f8bd2 100644 --- a/standard/attributes.md +++ b/standard/attributes.md @@ -504,6 +504,7 @@ A number of attributes affect the language in some way. These attributes include - `System.Runtime.CompilerServices.EnumeratorCancellationAttribute` ([§23.5.8](attributes.md#2358-the-enumeratorcancellation-attribute)), which is used to specify parameter for the cancellation token in an asynchronous iterator. - `System.Runtime.CompilerServices.ModuleInitializerAttribute` ([§23.5.9](attributes.md#2359-the-moduleinitializer-attribute)), which is used to mark a method as a module initializer. - `System.Runtime.CompilerServices.InterpolatedStringHandlerAttribute` and `System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute`, which are used to declare a custom interpolated string expression handler ([§23.5.9.1](attributes.md#23591-custom-interpolated-string-expression-handlers)) and to call one of its constructors, respectively. +- `System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute` (§SetsRequiredMembers) and `System.Runtime.CompilerServices.RequiredMemberAttribute` (§RequiredMember), which are used in required-member contexts ([§15.7.1](classes.md#1571-general)). The Nullable static analysis attributes ([§23.5.7](attributes.md#2357-code-analysis-attributes)) can improve the correctness of warnings generated for nullabilities and null states ([§8.9.5](types.md#895-nullabilities-and-null-states)). @@ -733,6 +734,8 @@ The attribute `ObsoleteAttribute` is used to mark types and members of types tha If a program uses a type or member that is decorated with `ObsoleteAttribute`, a compiler shall issue a warning or an error. Specifically, a compiler shall issue a warning if no error parameter is provided, or if the error parameter is provided and has the value `false`. A compiler shall issue an error if the error parameter is specified and has the value `true`. +A compiler may issue a warning when a required member ([§15.7.1](classes.md#1571-general)) is decorated with `ObsoleteAttribute`, unless its containing type is also decorated with `ObsoleteAttribute` or every instance constructor of that type is decorated with either `ObsoleteAttribute` or `SetsRequiredMembersAttribute` (§SetsRequiredMembers). + > *Example*: In the following code > > @@ -1634,6 +1637,65 @@ Constructor overload resolution uses the parameter types of the containing membe > *Note*: In particular, a forwarded lambda is not independently bound to a different delegate type for the constructor. Handler constructor parameter types do not contribute to generic inference for the containing invocation. Nullable analysis of constructor arguments does not flow changes back to the original receiver or argument slots. *end note* +### §Required-Member-Attributes Required member attributes + +#### §SetsRequiredMembers The SetsRequiredMembers attribute + +This attribute indicates that the constructor it decorates sets all required members for the current type, so callers do not need to set any required members themselves. However, the compiler doesn't verify that the constructor actually initializes all required members. + +> *Example*: +> +> +> ```csharp +> public class Person +> { +> public Person() { } +> +> [SetsRequiredMembers] +> public Person(string firstName, string lastName) => +> (FirstName, LastName) = (firstName, lastName); +> +> public required string FirstName { get; init; } +> public required string LastName { get; init; } +> +> public int? Age { get; set; } +> } +> +> public class Student : Person +> { +> public Student() : base() +> { +> } +> +> [SetsRequiredMembers] +> public Student(string firstName, string lastName) : +> base(firstName, lastName) +> { +> } +> +> public double GPA { get; set; } +> } +> +> public class Test +> { +> public static void M() +> { +> var p1 = new Student(); // error: doesn't set required members +> var p2 = new Student("Jane", "Williams"); // OK +> } +> } +> ``` +> +> *end example* + + + +> *Note*: As the derived-type constructor `Student(string, string)` chains to the base-type constructor `Person(string, string)`, which has this attribute, the derived-type constructor must also have that attribute ([§15.11.1](classes.md#15111-general)). *end note* + +#### §RequiredMember The RequiredMember attribute + +This attribute indicates that the current type has one or more required members ([§15.7.1](classes.md#1571-general)), or that a specific member of that type is required. However, it is an error for this attribute to be used explicitly. Instead, the presence of the modifier `required` results in the type or member being treated as if it were decorated with this attribute. + ## 23.6 Attributes for interoperation For interoperation with other languages, an indexer may be implemented using indexed properties. If the indexer is not decorated with `IndexerNameAttribute`, then the name `Item` is used by default. `IndexerNameAttribute` enables a developer to override this default and specify a different name. diff --git a/standard/classes.md b/standard/classes.md index 7f702e22f..dd7e998f5 100644 --- a/standard/classes.md +++ b/standard/classes.md @@ -29,6 +29,8 @@ There are two kinds of class: ***non-record class***, as declared by *non_record A *non_record_class_declaration* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *class_modifier*s ([§15.2.2](classes.md#1522-class-modifiers)), followed by an optional `partial` modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `class` and an *identifier* that names the class, followed by an optional *type_parameter_list* ([§15.2.3](classes.md#1523-type-parameters)), followed by an optional *class_base* specification ([§15.2.4](classes.md#1524-class-base-specification)), followed by an optional set of *type_parameter_constraints_clause*s ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by a *class_body* ([§15.2.6](classes.md#1526-class-body)). +A class having a required member ([§15.7.1](classes.md#1571-general)) directly (that is, not through inheritance) shall be treated as if it were decorated with the attribute `System.Runtime.CompilerServices.RequiredMemberAttribute` (§RequiredMember). + A class declaration shall not supply *type_parameter_constraints_clause*s unless it also supplies a *type_parameter_list*. A class declaration that supplies a *type_parameter_list* is a generic class declaration. Additionally, any class nested inside a generic class declaration or a generic struct declaration is itself a generic class declaration, since type arguments for the containing type shall be supplied to create a constructed type ([§8.4](types.md#84-constructed-types)). @@ -1055,7 +1057,7 @@ The inherited members of a constructed class type are the members of the immedia ### 15.3.5 The new modifier -A *class_member_declaration* is permitted to declare a member with the same name or signature as an inherited member. When this occurs, the derived class member is said to *hide* the base class member. See [§7.6.2.3](basic-concepts.md#7623-hiding-through-inheritance) for a precise specification of when a member hides an inherited member. +A *class_member_declaration* is permitted to declare a member with the same name or signature as an inherited member. When this occurs, the derived class member is said to *hide* the base class member. However, it is an error to hide a required member ([§15.7.1](classes.md#1571-general)). See [§7.6.2.3](basic-concepts.md#7623-hiding-through-inheritance) for a precise specification of when a member hides an inherited member. An inherited member `M` is considered to be ***available*** if `M` is accessible and there is no other inherited accessible member N that already hides `M`. Implicitly hiding an inherited member is not considered an error, but a compiler shall issue a warning unless the declaration of the derived class member includes a `new` modifier to explicitly indicate that the derived member is intended to hide the base member. If one or more parts of a partial declaration ([§15.2.7](classes.md#1527-partial-type-declarations)) of a nested type include the `new` modifier, no warning is issued if the nested type hides an available inherited member. @@ -1676,6 +1678,7 @@ field_modifier | 'static' | 'readonly' | 'volatile' + | 'required' | unsafe_modifier // unsafe code support ; @@ -1690,7 +1693,7 @@ variable_declarator *unsafe_modifier* ([§24.2](unsafe-code.md#242-unsafe-contexts)) is only available in unsafe code ([§24](unsafe-code.md#24-unsafe-code)). -A *field_declaration* may include a set of *attributes* ([§23](attributes.md#23-attributes)), a `new` modifier ([§15.3.5](classes.md#1535-the-new-modifier)), a valid combination of the four access modifiers ([§15.3.6](classes.md#1536-access-modifiers)), and a `static` modifier ([§15.5.2](classes.md#1552-static-and-instance-fields)). In addition, a *field_declaration* may include a `readonly` modifier ([§15.5.3](classes.md#1553-readonly-fields)) or a `volatile` modifier ([§15.5.4](classes.md#1554-volatile-fields)), but not both. The attributes and modifiers apply to all of the members declared by the *field_declaration*. It is an error for the same modifier to appear multiple times in a *field_declaration*. +A *field_declaration* may include a set of *attributes* ([§23](attributes.md#23-attributes)), a `new` modifier ([§15.3.5](classes.md#1535-the-new-modifier)), a valid combination of the four access modifiers ([§15.3.6](classes.md#1536-access-modifiers)), and a `static` modifier ([§15.5.2](classes.md#1552-static-and-instance-fields)). In addition, a *field_declaration* may include a `readonly` modifier ([§15.5.3](classes.md#1553-readonly-fields)) or a `volatile` modifier ([§15.5.4](classes.md#1554-volatile-fields)), but not both. A *field_declaration* may also include a `required` modifier ([§15.7.1](classes.md#1571-general)), provided it does not have a `readonly` or `fixed` modifier, and provided the field is not a constant. For a ref field, this restriction on `readonly` applies to the field itself, not to the variable it references. The attributes and modifiers apply to all of the members declared by the *field_declaration*. It is an error for the same modifier to appear multiple times in a *field_declaration*. The *type* of a *field_declaration* specifies the type of the members introduced by the declaration. The type is followed by a list of *variable_declarator*s, each of which introduces a new member. A *variable_declarator* consists of an *identifier* that names that member, optionally followed by an “`=`” token and a *variable_initializer* ([§15.5.6](classes.md#1556-variable-initializers)) that gives the initial value of that member. @@ -3458,6 +3461,7 @@ property_modifier | 'abstract' | 'extern' | 'readonly' // struct members only + | 'required' | unsafe_modifier // unsafe code support ; @@ -3505,6 +3509,27 @@ In a *ref_property_body* an expression body consisting of `=>` followed by `ref` When a property declaration includes an `extern` modifier, the property is said to be an ***external property***. Because an external property declaration provides no actual implementation, each of the *accessor_body*s in its *accessor_declarations* shall be a semicolon. +The modifier `required` indicates the instance member being declared is required to be set during object initialization, which forces the instance creator to provide an initial value for the member in an object initializer at the creation site. (See [§12.8.21](expressions.md#12821-default-value-expressions) and §SetsRequiredMembers for exemptions to this requirement.) A required member shall not be static. A required member shall be at least as accessible as its containing type. + +> *Note*: Although a required member declaration may include an initializer (*property_initializer* for a property, *variable_initializer* for a field), ordinarily, that initializer serves no purpose, as the instance creator is required to provide an initial value for that member anyway. However, if a constructor is decorated with the `SetsRequiredMembers` attribute the compiler assumes that member has been initialized correctly, and will not require an explicit initializer by the instance creator, resulting in the member’s initial value being that of its initializer, if one is present. *end note* + +A ***required member list*** is a list of all the members of a type that are required. A type automatically inherits this list from its base type. +To build the required member list `R` for a type `T`, the following steps are used: + +1. Initialize `R` to an empty list. +1. For each type `Tb`, starting with `T` and proceeding through its base types up to, but not including, `object`: + 1. If `Tb` is decorated with the `RequiredMember` attribute (§RequiredMember), gather into `Rb` all members declared in `Tb` that are marked with that attribute. + 1. Otherwise, continue with the next base type. + 1. For each member `Ri` in `Rb`: + 1. If `Ri` is overridden by any member of `R`, skip `Ri`. + 1. Otherwise, if `Ri` is hidden by a member of `R`, the lookup of required members fails and no further steps are taken. Calling any constructor of `T` not decorated with the `SetsRequiredMembers` attribute (§SetsRequiredMembers) is an error. + 1. Otherwise, add `Ri` to `R`. + +A required member shall be treated as if it were decorated with the attribute `System.Runtime.CompilerServices.RequiredMemberAttribute` (§RequiredMember). +With regard to nullable reference type analysis ([§8.9](types.md#89-reference-types-and-nullability)), a required member need not be initialized to a valid nullable state when any of its instance constructors returns. Any required member in a type and its base types is considered by nullable analysis to have its default value at the beginning of any instance constructor in that type, unless it chains to a `this` or `base` constructor that is decorated with the `SetsRequiredMembersAttribute` attribute. + +Nullable analysis shall warn about all required members from the current and base types that do not have a valid nullable state at the end of a constructor decorated with the `SetsRequiredMembersAttribute` attribute. + ### 15.7.2 Static and instance properties When a property declaration includes a `static` modifier, the property is said to be a ***static property***. When no `static` modifier is present, the property is said to be an ***instance property***. @@ -4299,6 +4324,8 @@ An accessor that is used to implement an interface shall not have an *accessor_m > > *end example* +A required property ([§15.7.1](classes.md#1571-general)) shall not be ref-valued. A required property shall have a set or init accessor and that accessor shall be at least as accessible as the property’s containing type. + ### 15.7.6 Virtual, sealed, override, and abstract accessors *Note*: This subclause applies to both properties ([§15.7](classes.md#157-properties)) and indexers ([§15.9](classes.md#159-indexers)). The subclause is written in terms of properties, when reading for indexers substitute indexer/indexers for property/properties and consult the list of differences between properties and indexers given in [§15.9.2](classes.md#1592-indexer-and-property-differences). *end note* @@ -4307,6 +4334,10 @@ A virtual property declaration specifies that the accessors of the property are An abstract property declaration specifies that the accessors of the property are virtual, but does not provide an actual implementation of the accessors. Instead, non-abstract derived classes are required to provide their own implementation for the accessors by overriding the property. Because an accessor for an abstract property declaration provides no actual implementation, its *accessor_body* simply consists of a semicolon. An abstract property shall not have a `private` accessor. +A property that overrides a base property that is required ([§15.7.1](classes.md#1571-general)) shall itself be required. A property that overrides a base property that is not required may itself be required, in which case, the derived type member is added to that type’s required member list. + +> *Note*: A type is permitted to override a required virtual property. This means that if the base virtual property has storage, and the derived type tries to access the base implementation of that property, it could observe uninitialized storage. *end note* + A property declaration that includes both the `abstract` and `override` modifiers specifies that the property is abstract and overrides a base property. The accessors of such a property are also abstract. Abstract property declarations are only permitted in abstract classes ([§15.2.2.2](classes.md#15222-abstract-classes)) and interfaces ([§19.4.4](interfaces.md#1944-interface-properties)). The accessors of an inherited virtual property can be overridden in a derived class or interface by including a property declaration that specifies an `override` directive. This is known as an ***overriding property declaration***. An overriding property declaration does not declare a new property. Instead, it simply specializes the implementations of the accessors of an existing virtual property. @@ -5276,6 +5307,15 @@ Each of the types referenced in the *parameter_list* of an instance constructor The optional *constructor_initializer* specifies another instance constructor to invoke before executing the statements given in the *constructor_body* of this instance constructor. This is described further in [§15.11.2](classes.md#15112-constructor-initializers). +All instance constructors on a type that has a required member list ([§15.7.1](classes.md#1571-general)), except those decorated with the `SetsRequiredMembers` attribute (§SetsRequiredMembers), automatically advertise a contract that consumers of the type shall initialize all of the members in the list. It is an error for an instance constructor to advertise a contract that requires a member that is not at least as accessible as the constructor itself. +An instance constructor whose *constructor_initializer* chains to another constructor decorated with the `SetsRequiredMembers` attribute (§SetsRequiredMembers), shall also be decorated with that attribute. +For every instance constructor `Ci` in type `T` with required members `R`, unless `Ci` is decorated with the attribute `SetsRequiredMembers`, consumers calling `Ci` shall do one of the following: + +- Assign to each member of `R` in an *object_initializer* on the *object_creation_expression*, as specified in [§12.8.17.2.2](expressions.md#1281722-object-initializers), +- Or set all members of `R` via the *named_argument_list* of an *attribute*. + +If the current context does not permit an *object_initializer* or is not an *attribute* with *named_argument_list*, and `Ci` is not decorated with `SetsRequiredMembers`, then it is an error to call `Ci`. + When a constructor declaration includes an `extern` modifier, the constructor is said to be an ***external constructor***. Because an external constructor declaration provides no actual implementation, its *constructor_body* consists of a semicolon. For all other constructors, the *constructor_body* consists of either - a *block*, which specifies the statements to initialize a new instance of the class; or @@ -6299,6 +6339,8 @@ A ***copy constructor*** for a type `T` is a constructor having a single paramet In certain circumstances ([§15.16.6.4](classes.md#151664-copy-and-clone-members)), a copy constructor may be provided by the compiler, and called by provided code. +A copy constructor provided by the implementation for a record class that has a required member list ([§15.7.1](classes.md#1571-general)) shall be decorated with `SetsRequiredMembersAttribute` (§SetsRequiredMembers). + #### 15.16.6.3 Equality members If a record class is derived directly from `object`, the record class type has a provided property declared as follows: diff --git a/standard/expressions.md b/standard/expressions.md index 142e87ff6..66cc1acdd 100644 --- a/standard/expressions.md +++ b/standard/expressions.md @@ -2806,6 +2806,8 @@ A member initializer that specifies an object initializer after the equals sign A member initializer that specifies a collection initializer after the equals sign is an initialization of an embedded collection. Instead of assigning a new collection to the target field, property, or indexer, the elements given in the initializer are added to the collection referenced by the target. The target shall be of a collection type that satisfies the requirements specified in [§12.8.17.2.3](expressions.md#1281723-collection-initializers). +When the invoked constructor requires a member to be initialized ([§15.11.1](classes.md#15111-general)), the object initializer shall include an assignment to that member, whether a simple assignment ([§12.24.2](expressions.md#12242-simple-assignment)) or, for a ref field, a ref assignment ([§12.24.4](expressions.md#12244-ref-assignment)). A nested object initializer or collection initializer for that member is a compile-time error. If the constructor is decorated with the `SetsRequiredMembers` attribute (§SetsRequiredMembers), this requirement does not apply. + When an initializer target refers to an indexer, the arguments to the indexer shall always be evaluated exactly once. Thus, even if the arguments end up never getting used (e.g., because of an empty nested initializer), they are evaluated for their side effects. > *Example*: The following class represents a point with two coordinates: @@ -3570,6 +3572,25 @@ A *default_value_expression* is a constant expression ([§12.26](expressions.md# - one of the following value types: `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `nint`, `nuint`, `long`, `ulong`, `char`, `float`, `double`, `decimal`, `bool`; or - any enumeration type. +When an instance of a struct `S` having a required member list ([§15.7.1](classes.md#1571-general)) is created with a value of `default` or `default(S)`, required-member setting is not enforced. However, such setting is enforced for an instance created with `new S()`, even when `S` has no parameterless constructor and the default struct constructor is used. + +> *Example*: Consider the following: +> +> +> ```csharp +> S v1 = default; // OK: no checking for required members +> S v2 = default(S); // OK: no checking for required members +> S v3 = new S(); // error: required member must be set +> S v4 = new S() { reqInt = default }; // OK: required member is set +> +> public struct S +> { +> public required int reqInt; +> } +> ``` +> +> *end example* + ### 12.8.22 Stack allocation A stack allocation expression allocates a block of memory from the execution stack. The ***execution stack*** is an area of memory where local variables are stored. The execution stack is not part of the managed heap. The memory used for local variable storage is automatically recovered when the current function returns. diff --git a/standard/interfaces.md b/standard/interfaces.md index aeaa0015a..35a72e050 100644 --- a/standard/interfaces.md +++ b/standard/interfaces.md @@ -299,7 +299,7 @@ This clause augments the description of fields in classes [§15.5](classes.md#15 Interface fields are declared using *field_declaration*s ([§15.5.1](classes.md#1551-general)) with the following additional rules: -- It is a compile-time error for *field_declaration* to declare an instance field. +- It is a compile-time error for *field_declaration* to declare an instance field. (As such, an interface field cannot be marked `required`.) > *Example*: The following program contains static members of various kinds: > @@ -443,6 +443,8 @@ Interface properties are declared using *property_declaration*s ([§15.7.1](clas - An interface property or interface property accessor declaration that has a block body or expression body is `virtual`; the `virtual` modifier is not required, but is allowed. - An instance *property_declaration* that has no implementation is `abstract`; the `abstract` modifier is not required, but is allowed. It is *never* considered to be an automatically implemented property ([§15.7.4](classes.md#1574-automatically-implemented-properties)). +It is a compile-time error for an interface property to be marked `required`. + ### 19.4.5 Interface events This clause augments the description of events in classes [§15.8](classes.md#158-events) for events declared in interfaces. @@ -978,6 +980,8 @@ The qualified interface member name of an explicit interface member implementati > > *end example* +It is a compile-time error for an explicit interface implementation of a member to have the modifier `required`. + ### 19.6.3 Uniqueness of implemented interfaces The interfaces implemented by a generic type declaration shall remain unique for all possible constructed types. Without this rule, it would be impossible to determine the correct method to call for certain constructed types. diff --git a/standard/lexical-structure.md b/standard/lexical-structure.md index 86a73301c..b746d4440 100644 --- a/standard/lexical-structure.md +++ b/standard/lexical-structure.md @@ -629,9 +629,9 @@ contextual_keyword | 'group' | 'init' | 'into' | 'join' | 'let' | 'managed' | 'nameof' | 'nint' | 'not' | 'notnull' | 'nuint' | 'on' | 'or' | 'orderby' | 'partial' - | 'record' | 'remove' | 'select' | 'set' | 'Stdcall' - | 'Thiscall' | 'unmanaged' | 'value' | 'var' | 'when' - | 'where' | 'yield' + | 'record' | 'remove' | 'required' | 'select' | 'set' + | 'Stdcall' | 'Thiscall' | 'unmanaged' | 'value' | 'var' + | 'when' | 'where' | 'yield' ; ``` diff --git a/standard/standard-library.md b/standard/standard-library.md index 5e491c7a2..653ffe41e 100644 --- a/standard/standard-library.md +++ b/standard/standard-library.md @@ -777,6 +777,13 @@ namespace System.Diagnostics.CodeAnalysis { public NotNullWhenAttribute(bool returnValue); } + + [System.AttributeUsage(System.AttributeTargets.Constructor, + AllowMultiple=false, Inherited=false)] + public sealed class SetsRequiredMembersAttribute : Attribute + { + public SetsRequiredMembersAttribute() {} + } } namespace System.Linq.Expressions @@ -927,6 +934,14 @@ namespace System.Runtime.CompilerServices public ModuleInitializerAttribute() { } } + [System.AttributeUsage(System.AttributeTargets.Class | + System.AttributeTargets.Field | System.AttributeTargets.Property | + System.AttributeTargets.Struct, AllowMultiple=false, Inherited=false)] + public sealed class RequiredMemberAttribute : Attribute + { + public RequiredMemberAttribute() {} + } + public readonly struct TaskAwaiter : ICriticalNotifyCompletion, INotifyCompletion { @@ -1479,6 +1494,7 @@ The following library types are referenced in this specification. The full names - `global::System.Diagnostics.CodeAnalysis.NotNullAttribute` - `global::System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute` - `global::System.Diagnostics.CodeAnalysis.NotNullWhenAttribute` +- `global::System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute` - `global::System.Linq.Expressions.Expression` - `global::System.Reflection.MemberInfo` - `global::System.Runtime.CompilerServices.AsyncMethodBuilderAttribute` @@ -1495,6 +1511,7 @@ The following library types are referenced in this specification. The full names - `global::System.Runtime.CompilerServices.InterpolatedStringHandlerAttribute` - `global::System.Runtime.CompilerServices.ITuple` - `global::System.Runtime.CompilerServices.ModuleInitializerAttribute` +- `global::System.Runtime.CompilerServices.RequiredMemberAttribute` - `global::System.Runtime.CompilerServices.TaskAwaiter` - `global::System.Runtime.CompilerServices.TaskAwaiter` - `global::System.Runtime.CompilerServices.ValueTaskAwaiter` diff --git a/standard/structs.md b/standard/structs.md index 44c310734..d899f142d 100644 --- a/standard/structs.md +++ b/standard/structs.md @@ -42,6 +42,7 @@ A ***struct*** is declared by *non_record_struct_declaration*, and a ***record s A *non_record_struct_declaration* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *struct_modifier*s ([§16.2.2](structs.md#1622-struct-modifiers)), followed by an optional `ref` modifier ([§16.2.3](structs.md#1623-ref-modifier)), followed by an optional partial modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `struct` and an *identifier* that names the struct, followed by an optional *type_parameter_list* specification ([§15.2.3](classes.md#1523-type-parameters)), followed by an optional *struct_interfaces* specification ([§16.2.5](structs.md#1625-struct-interfaces)), followed by an optional *type_parameter_constraints-clauses* specification ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by a *struct_body* ([§16.2.6](structs.md#1626-struct-body)), optionally followed by a semicolon. +A struct having a required member ([§15.7.1](classes.md#1571-general)) directly (that is, not through inheritance) shall be treated as if it were decorated with the attribute `System.Runtime.CompilerServices.RequiredMemberAttribute` (§RequiredMember). A *struct_declaration* shall not supply *type_parameter_constraints_clause*s unless it also supplies a *type_parameter_list*. A *struct_declaration* that supplies a *type_parameter_list* is a generic struct declaration. Additionally, any struct nested inside a generic class declaration or a generic struct declaration is itself a generic struct declaration, since type arguments for the containing type shall be supplied to create a constructed type ([§8.4](types.md#84-constructed-types)). diff --git a/standard/types.md b/standard/types.md index 5e158def1..bb19dc293 100644 --- a/standard/types.md +++ b/standard/types.md @@ -753,7 +753,7 @@ Whenever a constructed type or generic method is referenced, the supplied type a - `A` is a `struct` type or `enum` type, but not a nullable value type. > *Note*: `System.ValueType` and `System.Enum` are reference types that do not satisfy this constraint. *end note* - `A` is a type parameter having the value type constraint ([§15.2.5](classes.md#1525-type-parameter-constraints)). -- If the constraint is the constructor constraint `new()`, the type `A` shall not be `abstract` and shall have a public parameterless constructor. This is satisfied if one of the following is true: +- If the constraint is the constructor constraint `new()`, the type `A` shall not be `abstract`, and it shall have a public parameterless constructor. If `A` is a class or struct type with required members ([§15.7.1](classes.md#1571-general)), that constructor shall be decorated with the `SetsRequiredMembers` attribute (§SetsRequiredMembers). The public parameterless constructor requirement is satisfied if one of the following is true: - `A` is a value type, since all value types have a public parameterless instance constructor ([§8.3.3](types.md#833-default-constructors)). - `A` is a type parameter having the constructor constraint ([§15.2.5](classes.md#1525-type-parameter-constraints)). - `A` is a type parameter having the value type constraint ([§15.2.5](classes.md#1525-type-parameter-constraints)).