diff --git a/standard/classes.md b/standard/classes.md index 7f702e22f..9f0a6fc0f 100644 --- a/standard/classes.md +++ b/standard/classes.md @@ -2169,9 +2169,9 @@ Grammar notes: > *Note*: The overlapping of, and priority between, alternatives here is solely for descriptive convenience; the grammar rules could be elaborated to remove the overlap. ANTLR, and other grammar systems, adopt the same convenience and so *method_body* has the specified semantics automatically. *end note* -A *method_declaration* may include a set of *attributes* ([§23](attributes.md#23-attributes)) and one of the permitted kinds of declared accessibility ([§15.3.6](classes.md#1536-access-modifiers)), the `new` ([§15.3.5](classes.md#1535-the-new-modifier)), `static` ([§15.6.3](classes.md#1563-static-and-instance-methods)), `virtual` ([§15.6.4](classes.md#1564-virtual-methods)), `override` ([§15.6.5](classes.md#1565-override-methods)), `sealed` ([§15.6.6](classes.md#1566-sealed-methods)), `abstract` ([§15.6.7](classes.md#1567-abstract-methods)), `extern` ([§15.6.8](classes.md#1568-external-methods)) and `async` ([§15.14](classes.md#1514-async-functions)). Additionally a *method_declaration* that is contained directly by a *struct_declaration* may include the `readonly` modifier ([§16.6.12](structs.md#16612-methods)). +A *method_declaration* may include a set of *attributes* ([§23](attributes.md#23-attributes)) and one of the permitted kinds of declared accessibility ([§15.3.6](classes.md#1536-access-modifiers)), the `new` ([§15.3.5](classes.md#1535-the-new-modifier)), `static` ([§15.6.3](classes.md#1563-static-and-instance-methods)), `virtual` ([§15.6.4](classes.md#1564-virtual-methods)), `override` ([§15.6.5](classes.md#1565-override-methods)), `sealed` ([§15.6.6](classes.md#1566-sealed-methods)), `abstract` ([§15.6.7](classes.md#1567-abstract-methods)), `extern` ([§15.6.8](classes.md#1568-external-methods)) and `async` ([§15.14](classes.md#1514-async-functions)) modifiers. Additionally a *method_declaration* that is contained directly by a *struct_declaration* may include the `readonly` modifier ([§16.6.12](structs.md#16612-methods)). -A declaration has a valid combination of modifiers if all of the following are true: +A *method_declaration* has a valid combination of modifiers if all of the following are true. (These rules are modified slightly in the context of an interface; see [§19.4.1](interfaces.md#1941-general).): - The declaration includes a valid combination of access modifiers ([§15.3.6](classes.md#1536-access-modifiers)). - The declaration does not include the same modifier multiple times. @@ -2200,7 +2200,7 @@ A generic *method_declaration*, either with an `override` modifier, or for an ex The *member_name* specifies the name of the method. Unless the method is an explicit interface member implementation ([§19.6.2](interfaces.md#1962-explicit-interface-member-implementations)), the *member_name* is simply an *identifier*. -For an explicit interface member implementation, the *member_name* consists of an *interface_type* followed by a “`.`” and an *identifier*. In this case, the declaration shall include no modifiers other than (possibly) `extern` or `async`. +For an explicit interface member implementation, the *member_name* consists of an *interface_type* followed by a “`.`” and an *identifier*. The permitted modifiers are specified in [§19.6.2](interfaces.md#1962-explicit-interface-member-implementations). The optional *parameter_list* specifies the parameters of the method ([§15.6.2](classes.md#1562-method-parameters)). @@ -4963,6 +4963,9 @@ operator_modifier : 'public' | 'static' | 'extern' + | 'abstract' + | 'virtual' + | 'sealed' | unsafe_modifier // unsafe code support ; @@ -4973,7 +4976,8 @@ operator_declarator ; unary_operator_declarator - : type 'operator' overloadable_unary_operator '(' fixed_parameter ')' + : type (interface_type '.')? 'operator' overloadable_unary_operator + '(' fixed_parameter ')' ; logical_negation_operator @@ -4985,7 +4989,7 @@ overloadable_unary_operator ; binary_operator_declarator - : type 'operator' overloadable_binary_operator + : type (interface_type '.')? 'operator' overloadable_binary_operator '(' fixed_parameter ',' fixed_parameter ')' ; @@ -4995,8 +4999,8 @@ overloadable_binary_operator ; conversion_operator_declarator - : 'implicit' 'operator' type '(' fixed_parameter ')' - | 'explicit' 'operator' type '(' fixed_parameter ')' + : 'implicit' (interface_type '.')? 'operator' type '(' fixed_parameter ')' + | 'explicit' (interface_type '.')? 'operator' type '(' fixed_parameter ')' ; operator_body @@ -5014,14 +5018,16 @@ There are three categories of overloadable operators: Unary operators ([§15.10. The *operator_body* is either a semicolon, a block body ([§15.6.1](classes.md#1561-general)) or an expression body ([§15.6.1](classes.md#1561-general)). A block body consists of a *block*, which specifies the statements to execute when the operator is invoked. The *block* shall conform to the rules for value-returning methods described in [§15.6.11](classes.md#15611-method-body). An expression body consists of `=>` followed by an expression and a semicolon, and denotes a single expression to perform when the operator is invoked. -For `extern` operators, the *operator_body* consists simply of a semicolon. For all other operators, the *operator_body* is either a block body or an expression body. +For `extern` operators, the *operator_body* consists simply of a semicolon. For an abstract operator declaration in an interface ([§19.4.7](interfaces.md#1947-interface-operators)), the *operator_body* is a semicolon. For all other operators, the *operator_body* is either a block body or an expression body. The following rules apply to all operator declarations: -- An operator declaration shall include both a `public` and a `static` modifier. +- An operator declaration shall include a `static` modifier. Except in an explicit interface member implementation, an operator declared in a class or struct shall also include a `public` modifier. Operators declared in interfaces are subject to [§19.4.7](interfaces.md#1947-interface-operators). +- The `abstract`, `virtual`, and `sealed` modifiers are permitted only on operators declared in interfaces. +- An operator declarator that includes an *interface_type* followed by a “`.`” is an explicit interface member implementation and is subject to [§19.6.2](interfaces.md#1962-explicit-interface-member-implementations). The operand and result type restrictions in [§15.10.2](classes.md#15102-unary-operators), [§15.10.3](classes.md#15103-binary-operators), and [§15.10.4](classes.md#15104-conversion-operators) apply to the implemented interface declaration rather than to its explicit implementation. - The parameters of an operator shall have no modifiers other than `in`. -- The signature of an operator ([§15.10.2](classes.md#15102-unary-operators), [§15.10.3](classes.md#15103-binary-operators), [§15.10.4](classes.md#15104-conversion-operators)) shall differ from the signatures of all other operators declared in the same class. -- All types referenced in an operator declaration shall be at least as accessible as the operator itself ([§7.4.5](basic-concepts.md#745-accessibility-constraints)). +- Except in an explicit interface member implementation, the signature of an operator ([§15.10.2](classes.md#15102-unary-operators), [§15.10.3](classes.md#15103-binary-operators), [§15.10.4](classes.md#15104-conversion-operators)) shall differ from the signatures of all other non-explicitly implemented operators declared in the same type. +- Except in an explicit interface member implementation, all types referenced in an operator declaration shall be at least as accessible as the operator itself ([§7.4.5](basic-concepts.md#745-accessibility-constraints)). - It is an error for the same modifier to appear multiple times in an operator declaration. Each operator category imposes additional restrictions, as described in the following subclauses. @@ -5106,17 +5112,17 @@ A conversion operator declaration that includes the `explicit` keyword introduce A conversion operator converts from a source type, indicated by the parameter type of the conversion operator, to a target type, indicated by the return type of the conversion operator. -For a given source type `S` and target type `T`, if `S` or `T` are nullable value types, let `S₀` and `T₀` refer to their underlying types; otherwise, `S₀` and `T₀` are equal to `S` and `T` respectively. A class or struct is permitted to declare a conversion from a source type `S` to a target type `T` only if all of the following are true: +For a given source type `S` and target type `T`, if `S` or `T` are nullable value types, let `S₀` and `T₀` refer to their underlying types; otherwise, `S₀` and `T₀` are equal to `S` and `T` respectively. A class, struct, or interface is permitted to declare a conversion from a source type `S` to a target type `T` only if all of the following are true, except for explicit interface member implementations as specified in [§15.10.1](classes.md#15101-general): - `S₀` and `T₀` are different types. -- Either `S₀` or `T₀` is the instance type of the class or struct that contains the operator declaration. +- Either `S₀` or `T₀` is the instance type of the class or struct that contains the operator declaration, or is a type parameter that counts as the instance type of the containing interface as specified in [§19.4.7](interfaces.md#1947-interface-operators). - Neither `S₀` nor `T₀` is an *interface_type*. - Excluding user-defined conversions, a conversion does not exist from `S` to `T` or from `T` to `S`. -For the purposes of these rules, any type parameters associated with `S` or `T` are considered to be unique types that have no inheritance relationship with other types, and any constraints on those type parameters are ignored. +For the purposes of these rules, any type parameters associated with `S` or `T` are considered to be unique types that have no inheritance relationship with other types. Their constraints are considered only to determine whether a type parameter counts as the instance type of the containing interface. > *Example*: In the following: > @@ -5136,7 +5142,7 @@ For the purposes of these rules, any type parameters associated with `S` or `T > > *end example* -From the second rule, it follows that a conversion operator shall convert either to or from the class or struct type in which the operator is declared. +From the second rule, it follows that a conversion operator declared in a class or struct, other than an explicit interface member implementation, shall convert either to or from that class or struct type. > *Example*: It is possible for a class or struct type `C` to define a conversion from `C` to `int` and from `int` to `C`, but not from `int` to `bool`. *end example* diff --git a/standard/conversions.md b/standard/conversions.md index a329b449c..4f56635ea 100644 --- a/standard/conversions.md +++ b/standard/conversions.md @@ -781,9 +781,14 @@ A user-defined implicit conversion from an expression `E` to a type `T` is pro - If `E` has a type, let `S` be that type. - If `S` or `T` are nullable value types, let `Sᵢ` and `Tᵢ` be their underlying types, otherwise let `Sᵢ` and `Tᵢ` be `S` and `T`, respectively. - If `Sᵢ` or `Tᵢ` are type parameters, let `S₀` and `T₀` be their effective base classes, otherwise let `S₀` and `T₀` be `Sᵢ` and `Tᵢ`, respectively. -- Find the set of types, `D`, from which user-defined conversion operators will be considered. This set consists of `S₀` (if `S₀` exists and is a class or struct), the base classes of `S₀` (if `S₀` exists and is a class), and `T₀` (if `T₀` is a class or struct). A type is added to the set `D` only if an identity conversion to another type already included in the set does not exist. - -- Find the set of applicable user-defined and lifted conversion operators, `U`. This set consists of the user-defined and lifted implicit conversion operators declared by the classes or structs in `D` that convert from a type encompassing `E` to a type encompassed by `T`. If `U` is empty, the conversion is undefined and a compile-time error occurs. +- Find the set of applicable user-defined and lifted conversion operators, `U`. + - Find the set of types, `D₁`, from which user-defined conversion operators will be considered. This set consists of `S₀` (if `S₀` exists and is a class or struct), the base classes of `S₀` (if `S₀` exists and is a class), and `T₀` (if `T₀` is a class or struct). A type is added to `D₁` only if an identity conversion to another type already included in the set does not exist. + - Find the set of applicable user-defined and lifted conversion operators, `U₁`. This set consists of the user-defined and lifted implicit conversion operators declared by the classes or structs in `D₁` that convert from a type encompassing `E` to a type encompassed by `T`. + - If `U₁` is not empty, then `U` is `U₁`. Otherwise, + - Find the set of types, `D₂`, from which user-defined conversion operators will be considered. This set consists of `Sᵢ` *effective interface set* and their base interfaces (if `Sᵢ` is a type parameter), and `Tᵢ` *effective interface set* (if `Tᵢ` is a type parameter). + - Find the set of applicable user-defined and lifted conversion operators, `U₂`. This set consists of the user-defined and lifted implicit conversion operators declared by the interfaces in `D₂` that convert from a type encompassing `E` to a type encompassed by `T`. + - If `U₂` is not empty, then `U` is `U₂`. +- If `U` is empty, the conversion is undefined and a compile-time error occurs. - Find the most-specific source type, `Sₓ`, of the operators in `U`: - If `S` exists and any of the operators in `U` convert from `S`, then `Sₓ` is `S`. - Otherwise, `Sₓ` is the most-encompassed type in the combined set of source types of the operators in `U`. If exactly one most-encompassed type cannot be found, then the conversion is ambiguous and a compile-time error occurs. @@ -809,8 +814,14 @@ A user-defined explicit conversion from an expression `E` to a type `T` is pro - If `E` has a type, let `S` be that type. - If `S` or `T` are nullable value types, let `Sᵢ` and `Tᵢ` be their underlying types, otherwise let `Sᵢ` and `Tᵢ` be `S` and `T`, respectively. - If `Sᵢ` or `Tᵢ` are type parameters, let `S₀` and `T₀` be their effective base classes, otherwise let `S₀` and `T₀` be `Sᵢ` and `Tᵢ`, respectively. -- Find the set of types, `D`, from which user-defined conversion operators will be considered. This set consists of `S₀` (if `S₀` exists and is a class or struct), the base classes of `S₀` (if `S₀` exists and is a class), `T₀` (if `T₀` is a class or struct), and the base classes of `T₀` (if `T₀` is a class). A type is added to the set `D` only if an identity conversion to another type already included in the set does not exist. -- Find the set of applicable user-defined and lifted conversion operators, `U`. This set consists of the user-defined and lifted implicit or explicit conversion operators declared by the classes or structs in `D` that convert from a type encompassing `E` or encompassed by `S` (if it exists) to a type encompassing or encompassed by `T`. If `U` is empty, the conversion is undefined and a compile-time error occurs. +- Find the set of applicable user-defined and lifted conversion operators, `U`. + - Find the set of types, `D₁`, from which user-defined conversion operators will be considered. This set consists of `S₀` (if `S₀` exists and is a class or struct), the base classes of `S₀` (if `S₀` exists and is a class), `T₀` (if `T₀` is a class or struct), and the base classes of `T₀` (if `T₀` is a class). A type is added to `D₁` only if an identity conversion to another type already included in the set does not exist. + - Find the set of applicable user-defined and lifted conversion operators, `U₁`. This set consists of the user-defined and lifted implicit or explicit conversion operators declared by the classes or structs in `D₁` that convert from a type encompassing `E` or encompassed by `S` (if it exists) to a type encompassing or encompassed by `T`. + - If `U₁` is not empty, then `U` is `U₁`. Otherwise, + - Find the set of types, `D₂`, from which user-defined conversion operators will be considered. This set consists of `Sᵢ` *effective interface set* and their base interfaces (if `Sᵢ` is a type parameter), and `Tᵢ` *effective interface set* and their base interfaces (if `Tᵢ` is a type parameter). + - Find the set of applicable user-defined and lifted conversion operators, `U₂`. This set consists of the user-defined and lifted implicit or explicit conversion operators declared by the interfaces in `D₂` that convert from a type encompassing `E` or encompassed by `S` (if it exists) to a type encompassing or encompassed by `T`. + - If `U₂` is not empty, then `U` is `U₂`. +- If `U` is empty, the conversion is undefined and a compile-time error occurs. - Find the most-specific source type, `Sₓ`, of the operators in `U`: - If `S` exists and any of the operators in `U` convert from `S`, then `Sₓ` is `S`. - Otherwise, if any of the operators in `U` convert from types that encompass `E`, then `Sₓ` is the most-encompassed type in the combined set of source types of those operators. If no most-encompassed type can be found, then the conversion is ambiguous and a compile-time error occurs. diff --git a/standard/expressions.md b/standard/expressions.md index 142e87ff6..a7b911487 100644 --- a/standard/expressions.md +++ b/standard/expressions.md @@ -245,7 +245,7 @@ The descriptions of individual operators in [§12.9](expressions.md#129-unary-op An operation of the form `«op» x` or `x «op»`, where «op» is an overloadable unary operator, and `x` is an expression of type `X`, is processed as follows: -- The set of candidate user-defined operators provided by `X` for the operation `operator «op»(x)` is determined using the rules of [§12.4.6](expressions.md#1246-candidate-user-defined-operators). +- The set of class or struct candidate user-defined operators provided by `X` for the operation `operator «op»(x)` is determined using [§12.4.6](expressions.md#1246-candidate-user-defined-operators). If that set is empty, the interface candidates provided by `X` are used instead. - If the set of candidate user-defined operators is not empty, then this becomes the set of candidate operators for the operation. Otherwise, the predefined unary `operator «op»` implementations, including their lifted forms, become the set of candidate operators for the operation. The predefined implementations of a given operator are specified in the description of the operator. The predefined operators provided by an enum or delegate type are only included in this set when the binding-time type—or the underlying type if it is a nullable type—of either operand is the enum or delegate type. - The overload resolution rules of [§12.6.4](expressions.md#1264-overload-resolution) are applied to the set of candidate operators to select the best operator with respect to the argument list `(x)`, and this operator becomes the result of the overload resolution process. If overload resolution fails to select a single best operator, a binding-time error occurs. @@ -253,7 +253,7 @@ An operation of the form `«op» x` or `x «op»`, where «op» is an overloada An operation of the form `x «op» y`, where «op» is an overloadable binary operator, `x` is an expression of type `X`, and `y` is an expression of type `Y`, is processed as follows: -- The set of candidate user-defined operators provided by `X` and `Y` for the operation `operator «op»(x, y)` is determined. The set consists of the union of the candidate operators provided by `X` and the candidate operators provided by `Y`, each determined using the rules of [§12.4.6](expressions.md#1246-candidate-user-defined-operators). For the combined set, candidates are merged as follows: +- The set of class or struct candidate user-defined operators provided by `X` and `Y` for the operation `operator «op»(x, y)` is determined using [§12.4.6](expressions.md#1246-candidate-user-defined-operators). If their union is empty, the union of the interface candidates provided by `X` and `Y` is used instead. For shift operators, only `X` provides candidates in either phase. For the combined set, candidates are merged as follows: - If `X` and `Y` are identity convertible, or if `X` and `Y` are derived from a common base type, then shared candidate operators only occur in the combined set once. - If there is an identity conversion between `X` and `Y`, an operator `«op»Y` provided by `Y` has the same return type as an `«op»X` provided by `X` and the operand types of `«op»Y` have an identity conversion to the corresponding operand types of `«op»X` then only `«op»X` occurs in the set. - If the set of candidate user-defined operators is not empty, then this becomes the set of candidate operators for the operation. Otherwise, the predefined binary `operator «op»` implementations, including their lifted forms, become the set of candidate operators for the operation. The predefined implementations of a given operator are specified in the description of the operator. For predefined enum and delegate operators, the only operators considered are those provided by an enum or delegate type that is the binding-time type of one of the operands. @@ -261,12 +261,21 @@ An operation of the form `x «op» y`, where «op» is an overloadable binary ### 12.4.6 Candidate user-defined operators -Given a type `T` and an operation `operator «op»(A)`, where «op» is an overloadable operator and `A` is an argument list, the set of candidate user-defined operators provided by `T` for operator `«op»(A)` is determined as follows: +Given a type `T` and an operation `operator «op»(A)`, where «op» is an overloadable operator and `A` is an argument list, the set of class or struct candidate user-defined operators provided by `T` for operator `«op»(A)` is determined as follows: - Determine the type `T₀`. If `T` is a nullable value type, `T₀` is its underlying type; otherwise, `T₀` is equal to `T`. -- For all `operator «op»` declarations in `T₀` and all lifted forms of such operators, if at least one operator is applicable ([§12.6.4.2](expressions.md#12642-applicable-function-member)) with respect to the argument list `A`, then the set of candidate operators consists of all such applicable operators in `T₀`. +- If `T₀` is a type parameter, replace it with its effective base class ([§15.2.5](classes.md#1525-type-parameter-constraints)). If `T₀` is an interface, the class or struct candidate set is empty. +- Otherwise, for all non-explicitly implemented `operator «op»` declarations in `T₀` and all lifted forms of such operators, if at least one operator is applicable ([§12.6.4.2](expressions.md#12642-applicable-function-member)) with respect to the argument list `A`, then the set of candidate operators consists of all such applicable operators in `T₀`. - Otherwise, if `T₀` is `object`, the set of candidate operators is empty. -- Otherwise, the set of candidate operators provided by `T₀` is the set of candidate operators provided by the direct base class of `T₀`, or the effective base class of `T₀` if `T₀` is a type parameter. +- Otherwise, repeat the class or struct candidate search with the direct base class of `T₀`. + +The set of interface candidates provided by `T` is determined as follows: + +- If `T` is a type parameter, let `D` be its effective interface set ([§15.2.5](classes.md#1525-type-parameter-constraints)) together with all base interfaces of those interfaces. If `T` is an interface, let `D` consist of `T` and all its base interfaces. Otherwise, the interface candidate set is empty. +- For each interface `I` in `D`, determine the applicable `operator «op»` declarations in `I`, including their lifted forms. Abstract and virtual declarations are considered only when `T` is a type parameter. Explicit interface member implementations are not candidates. +- The candidate set is the union of those applicable operators, excluding operators declared in a base interface of an interface in `D` that declares an applicable operator for this operation. An inapplicable declaration does not exclude base-interface candidates. + +An abstract or virtual operator selected through a type parameter invokes the implementation for the type argument as specified in [§12.8.7](expressions.md#1287-member-access). ### 12.4.7 Numeric promotions @@ -375,7 +384,7 @@ Member lookup considers not only the name of a member but also the number of typ A member lookup of a name `N` with `K` type arguments in a type `T` is processed as follows: - First, a set of accessible members named `N` is determined: - - If `T` is a type parameter, then the set is the union of the sets of accessible members named `N` in each of the types specified as a primary constraint or secondary constraint ([§15.2.5](classes.md#1525-type-parameter-constraints)) for `T`, along with the set of accessible members named `N` in `object`. + - If `T` is a type parameter, then the set is the union of the sets of accessible members named `N` in its effective base class and effective interface set ([§15.2.5](classes.md#1525-type-parameter-constraints)), including their inherited members. This includes static abstract and virtual interface members. - Otherwise, the set consists of all accessible ([§7.4](basic-concepts.md#74-member-access)) members named `N` in `T`, including inherited members and the accessible members named `N` in `object`. If `T` is a constructed type, the set of members is obtained by substituting type arguments as described in [§15.3.3](classes.md#1533-members-of-constructed-types). Members that include an `override` modifier are excluded from the set. - Next, if `K` is zero, all nested types whose declarations include type parameters are removed. If `K` is not zero, all members with a different number of type parameters are removed. When `K` is zero, methods having type parameters are not removed, since the type inference process ([§12.6.3](expressions.md#1263-type-inference)) might be able to infer the type arguments. - Next, if the member is invoked, all non-invocable members are removed from the set. @@ -1836,6 +1845,7 @@ The *member_access* is evaluated and classified as follows: - If `e` is zero and `E` is a namespace and `E` contains a nested namespace with name `I`, then the result is that namespace. - Otherwise, if `E` is a namespace and `E` contains an accessible type having name `I` and `K` type parameters, then the result is that type constructed with the given type arguments. +- If `E` is classified as a type parameter and member lookup of `I` in `E` with `K` type arguments produces a match, then a static abstract or virtual interface property or event is classified as a property or event access with no associated instance expression. One or more methods are classified as a method group with no associated instance expression; only static abstract or virtual interface methods may be accessed through this form. Other matches are invalid in this form of access. - If `E` is classified as a type, if `E` is not a type parameter, and if a member lookup ([§12.5](expressions.md#125-member-lookup)) of `I` in `E` with `K` type parameters produces a match, then `E.I` is evaluated and classified as follows: > *Note*: When the result of such a member lookup is a method group and `K` is zero, the method group can contain methods having type parameters. This allows such methods to be considered for type argument inferencing. *end note* - If `I` identifies a type, then the result is that type constructed with any given type arguments. @@ -1866,6 +1876,8 @@ The *member_access* is evaluated and classified as follows: - Otherwise, the result is an event access with an associated instance expression of `E`. - Otherwise, an attempt is made to process `E.I` as an extension method invocation ([§12.8.10.3](expressions.md#128103-extension-method-invocations)). If this fails, `E.I` is an invalid member reference, and a binding-time error occurs. +A static abstract or virtual interface member shall be accessed only through a type parameter constrained by an interface that declares or inherits that member. It cannot be accessed directly through an interface name or an instance expression. For such an access through a type parameter, the implementation invoked is the most specific implementation for the actual type argument ([§19.4.10](interfaces.md#19410-most-specific-implementation)), including an explicit implementation or a default implementation supplied by an interface. The same dispatch rule applies to abstract or virtual operators and conversions selected through a type parameter. + #### 12.8.7.2 Identical simple names and type names In a member access of the form `E.I`, if `E` is a single identifier, and if the meaning of `E` as a *simple_name* ([§12.8.4](expressions.md#1284-simple-names)) is a constant, field, property, local variable, or parameter with the same type as the meaning of `E` as a *type_name* ([§7.7.1](basic-concepts.md#771-general)), then both possible meanings of `E` are permitted. The member lookup of `E.I` is never ambiguous, since `I` shall necessarily be a member of the type `E` in both cases. In other words, the rule simply permits access to the static members and nested types of `E` where a compile-time error would otherwise have occurred. @@ -6410,6 +6422,8 @@ The same technique applied here to capture local variables can also be used when ***Query expressions*** provide a language-integrated syntax for queries that is similar to relational and hierarchical query languages such as SQL and XQuery. +A type parameter shall not be used as the source expression of a query expression. + ```ANTLR query_expression : from_clause query_body diff --git a/standard/interfaces.md b/standard/interfaces.md index aeaa0015a..97155b727 100644 --- a/standard/interfaces.md +++ b/standard/interfaces.md @@ -94,7 +94,7 @@ If a generic interface is declared in multiple parts ([§15.2.3](classes.md#1523 #### 19.2.3.2 Variance safety -The occurrence of variance annotations in the type parameter list of a type restricts the places where types can occur within the type declaration. However, these restrictions do not apply to occurrences of types within a declaration of a static member. +The occurrence of variance annotations in the type parameter list of a type restricts the places where types can occur within the type declaration. However, these restrictions do not apply to occurrences of types within a declaration of a non-virtual, non-abstract static member. A type `T` is ***output-unsafe*** if one of the following holds: @@ -233,17 +233,18 @@ interface_member_declaration ; ``` -This clause augments the description of members in classes ([§15.3](classes.md#153-class-members)) with the differences and restrictions for interfaces: +This subclause augments the description of members in classes ([§15.3](classes.md#153-class-members)) with the differences and restrictions for interfaces: - A *finalizer_declaration* is not allowed. - Instance constructors, *constructor_declaration*s, are not allowed. -- All interface members implicitly have public access; however, an explicit access modifier ([§7.4.2](basic-concepts.md#742-declared-accessibility)) is permitted except on static constructors ([§15.12](classes.md#1512-static-constructors)). -- The `abstract` modifier is implied for interface function members ([§12.6](expressions.md#126-function-members)) without bodies; that modifier may be given explicitly. -- An interface instance function member whose declaration includes a body is an implicitly `virtual` member unless the `sealed` or `private` modifier is used. The `virtual` modifier may be given explicitly. +- Interface members other than explicit interface member implementations implicitly have public access; however, an explicit access modifier ([§7.4.2](basic-concepts.md#742-declared-accessibility)) is permitted except on static constructors ([§15.12](classes.md#1512-static-constructors)). Explicit interface member implementations are subject to [§19.6.2](interfaces.md#1962-explicit-interface-member-implementations). +- The `abstract` modifier is implied for interface instance function members ([§12.6](expressions.md#126-function-members)) without bodies; that modifier may be given explicitly. For non-extern interface static function members without bodies the `abstract` modifier shall be present. +- An interface instance function member whose declaration includes a body is an implicitly `virtual` member unless the `sealed` or `private` modifier is used. The `virtual` modifier may be given explicitly. An interface static member whose declaration includes a body may have a `virtual` modifier. - A `private` or `sealed` function member of an interface shall have a body. -- A `private` function member shall not have the modifier `sealed`. +- A `private` instance function member shall not have the modifier `sealed`. - A derived interface may override an abstract or virtual member declared in a base interface. - An explicitly implemented function member shall not have the modifier `sealed`. +- A static method, property, event, or operator may combine `static` with one of `abstract`, `virtual`, or `sealed`; these three modifiers are mutually exclusive. An abstract member has no implementation and shall not be declared `extern`, a virtual member provides an implementation, and a sealed member is non-virtual. A static member without any of these three modifiers is also non-virtual. Some declarations, such as *constant_declaration* ([§15.4](classes.md#154-constants)) have no restrictions in interfaces. @@ -348,11 +349,11 @@ This clause augments the description of methods in classes [§15.6](classes.md#1 Interface methods are declared using *method_declaration*s ([§15.6](classes.md#156-methods))). The *attributes*, *return_type*, *ref_return_type*, *identifier*, and *parameter_list* of an interface method declaration have the same meaning as those of a method declaration in a class. Interface methods have the following additional rules: - *method_modifier* shall not include `override`. -- A method whose body is a semi-colon (`;`) is `abstract`; the `abstract` modifier is not required, but is allowed. -- An interface method declaration that has a block body or expression body as a *method_body* is `virtual`; the `virtual` modifier is not required, but is allowed. +- A non-extern instance method whose body is a semi-colon (`;`) is `abstract`; the `abstract` modifier is not required, but is allowed. A non-extern static method whose body is a semi-colon (`;`) shall include the `abstract` modifier. +- An interface instance method declaration that has a block body or expression body as a *method_body* is `virtual`; the `virtual` modifier is not required, but is allowed. For a static method the `virtual` modifier is permitted. - A *method_declaration* shall not have *type_parameter_constraints_clause*s unless it also has a *type_parameter_list*. - The list of requirements for valid combinations of modifiers stated for a class method is extended, as follows: - - A static declaration that is not extern shall have a block body or expression body as a *method_body*. + - A static declaration that is not extern shall have a block body or expression body as a *method_body*, or shall be declared `abstract`. - A virtual declaration that is not extern shall have a block body or expression body as a *method_body*. - A private declaration that is not extern shall have a block body or expression body as a *method_body*. - A sealed declaration that is not extern shall have a block body or expression body as a *method_body*. @@ -429,36 +430,38 @@ A virtual method with implementation declared in an interface may be overridden ### 19.4.4 Interface properties -This clause augments the description of properties in classes [§15.7](classes.md#157-properties) for properties declared in interfaces. +This subclause augments the description of properties in classes [§15.7](classes.md#157-properties) for properties declared in interfaces. Interface properties are declared using *property_declaration*s ([§15.7.1](classes.md#1571-general)) with the following additional rules: - *property_modifier* shall not include `override`. - An explicit interface member implementation shall not contain an *accessor_modifier* ([§15.7.3](classes.md#1573-accessors)). -- A derived interface may explicitly implement an abstract interface property declared in a base interface. +- A derived interface may explicitly implement an abstract or virtual interface property declared in a base interface. > *Note*: As an interface cannot contain instance fields, an interface property cannot be an instance auto-property, as that would require the declaration of implicit hidden instance fields. *end note* - The type of an interface property shall be output-safe if there is a get accessor, and shall be input-safe if there is a set or init accessor. -- 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)). +- An interface instance 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. For a static property or property accessor the `virtual` modifier is permitted. +- 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)). However, the `abstract` modifier shall be present if a static property is to be abstract. +- A *property_declaration* may contain the `sealed` modifier. ### 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. +This subclause augments the description of events in classes [§15.8](classes.md#158-events) for events declared in interfaces. Interface events are declared using *event_declaration*s ([§15.8.1](classes.md#1581-general)), with the following additional rules: - *event_modifier* shall not include `override`. -- A derived interface may implement an abstract interface event declared in a base interface ([§15.8.5](classes.md#1585-virtual-sealed-override-and-abstract-accessors)). -- It is a compile-time error for *variable_declarators* in an interface *event_declaration* to contain any *variable_initializer*s. -- An interface event with the `virtual` or `sealed` modifiers must declare accessors. It is *never* considered to be an automatically implemented field-like event ([§15.8.2](classes.md#1582-field-like-events)). -- An interface event with the `abstract` modifier must not declare accessors. +- A derived interface may implement an abstract or virtual interface event declared in a base interface ([§15.8.5](classes.md#1585-virtual-sealed-override-and-abstract-accessors)). +- It is a compile-time error for *variable_declarators* in an instance *event_declaration* to contain any *variable_initializer*s. +- An instance event with the `virtual` or `sealed` modifiers shall declare accessors. It is *never* considered to be an automatically implemented field-like event ([§15.8.2](classes.md#1582-field-like-events)). +- An abstract event shall not declare accessors or variable initializers. +- A static event may have the `abstract`, `virtual`, or `sealed` modifier. - The type of an interface event shall be input-safe. ### 19.4.6 Interface indexers -This clause augments the description of indexers in classes [§15.9](classes.md#159-indexers) for indexers declared in interfaces. +This subclause augments the description of indexers in classes [§15.9](classes.md#159-indexers) for indexers declared in interfaces. Interface indexers are declared using *indexer_declaration*s ([§15.9](classes.md#159-indexers)), with the following additional rules: @@ -474,15 +477,22 @@ Interface indexers are declared using *indexer_declaration*s ([§15.9](classes.m ### 19.4.7 Interface operators -This clause augments the description of *operator_declaration* members in classes [§15.10](classes.md#1510-operators) for operators declared in interfaces. +This subclause augments the description of *operator_declaration* members in classes [§15.10](classes.md#1510-operators) for operators declared in interfaces. -For an *operator_declaration* in an interface the *operator_body* shall only be a block body ([§15.6.1](classes.md#1561-general)) or an expression body ([§15.6.1](classes.md#1561-general)). +For an abstract *operator_declaration* in an interface the *operator_body* shall be a semicolon. For a non-abstract, non-extern operator it shall be a block body or an expression body ([§15.6.1](classes.md#1561-general)). -It is a compile-time error for an interface to declare a conversion, equality, or inequality operator. +A static *operator_declaration* may have the `abstract`, `virtual`, or `sealed` modifier. An operator without one of these modifiers is non-virtual. An operator other than an explicit interface member implementation shall be public, either implicitly or explicitly. + +For an interface operator other than an explicit interface member implementation, the operand type requirements in [§15.10.2](classes.md#15102-unary-operators) and [§15.10.3](classes.md#15103-binary-operators) may be satisfied by the instance type of the containing interface or by a type parameter that counts as that instance type. In order for a type parameter `T` to count as the instance type it shall meet the following requirements: + +- `T` is a direct type parameter on the interface in which the operator declaration occurs, and +- `T` is directly constrained by the instance type; i.e., the surrounding interface with its own type parameters used as type arguments. + +For an equality operator (`==` or `!=`) other than an explicit interface member implementation, at least one operand shall be such a type parameter, rather than the containing interface type. Equality and conversion operator declarations in interfaces shall be abstract or virtual, or shall explicitly implement an inherited abstract or virtual operator. Conversion operators are additionally subject to [§15.10.4](classes.md#15104-conversion-operators). ### 19.4.8 Interface static constructors -This clause augments the description of static constructors in classes [§15.12](classes.md#1512-static-constructors) for static constructors declared in interfaces. +This subclause augments the description of static constructors in classes [§15.12](classes.md#1512-static-constructors) for static constructors declared in interfaces. The static constructor for a closed ([§8.4.3](types.md#843-open-and-closed-types)) interface executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following actions to occur within an application domain: @@ -498,7 +508,7 @@ To initialize a new closed interface type, first a new set of static fields for ### 19.4.9 Interface nested types -This clause augments the description of nested types in classes [§15.3.9](classes.md#1539-nested-types) for nested types declared in interfaces. +This subclause augments the description of nested types in classes [§15.3.9](classes.md#1539-nested-types) for nested types declared in interfaces. It is an error to declare a class type, struct type, or enum type within the scope of a type parameter that was declared with a *variance_annotation* ([§19.2.3.1](interfaces.md#19231-general)). @@ -517,7 +527,11 @@ It is an error to declare a class type, struct type, or enum type within the sco ### 19.4.10 Most specific implementation -Every class and struct shall have a most specific implementation for every virtual member declared in all interfaces implemented by that type among the implementations appearing in the type or its direct and indirect interfaces. The ***most specific implementation*** is a unique implementation that is more specific than every other implementation. +Every class and struct shall have a most specific implementation for every instance or static abstract or virtual member declared in all interfaces implemented by that type among the implementations appearing in the type or its direct and indirect interfaces. The ***most specific implementation*** is a unique implementation that is more specific than every other implementation. + +These specificity rules also apply when determining the most specific implementation in an interface among that interface and its direct and indirect base interfaces. An explicit implementation in a derived interface is more specific than the declaration or implementation it overrides, including when that explicit implementation reabstracts the member. A most specific reabstraction supplies no implementation body. + +An interface is permitted as a type argument only if each static abstract or virtual member it declares or inherits has a most specific implementation in that interface that is not abstract. This requirement does not apply to instance members. > *Note*: The most specific implementation rule ensures that an ambiguity arising from diamond interface inheritance is resolved explicitly by the programmer at the point where the conflict occurs. *end note* @@ -794,7 +808,7 @@ The base interfaces of a generic class declaration shall satisfy the uniqueness ### 19.6.2 Explicit interface member implementations -For purposes of implementing interfaces, a class, struct, or interface may declare ***explicit interface member implementation***s. An explicit interface member implementation is a method, property, event, or indexer declaration that references a qualified interface member name. A class or struct that implements a non-public member in a base interface must declare an explicit interface member implementation. An interface that implements a member in a base interface must declare an explicit interface member implementation. +For purposes of implementing interfaces, a class, struct, or interface may declare ***explicit interface member implementation***s. An explicit interface member implementation is a method, property, event, indexer, or operator declaration that references a qualified interface member name. A class or struct that implements a non-public member in a base interface must declare an explicit interface member implementation. An interface that implements a member in a base interface must declare an explicit interface member implementation. A derived interface member that satisfies interface mapping ([§19.6.5](interfaces.md#1965-interface-mapping)) hides the base interface member ([§7.6.2](basic-concepts.md#762-name-hiding)). The compiler shall issue a warning unless the `new` modifier is present. @@ -850,9 +864,9 @@ A derived interface member that satisfies interface mapping ([§19.6.5](interfac > > *end example* -It is not possible to access an explicit interface member implementation through its qualified interface member name in a method invocation, property access, event access, or indexer access. An explicit interface instance member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name. An explicit interface static member implementation can only be accessed through the interface name. +It is not possible to access an explicit interface member implementation through its qualified interface member name in a method invocation, property access, event access, or indexer access. An explicit interface instance member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name. An explicit interface static member implementation is accessed through the interface contract using a constrained type parameter ([§12.8.7](expressions.md#1287-member-access)), or through the corresponding operator or conversion on such a type parameter. -It is a compile-time error for an explicit interface member implementation to include any modifiers ([§15.6](classes.md#156-methods)) other than `extern` or `async`. +It is a compile-time error for an explicit interface member implementation to include any modifiers ([§15.6](classes.md#156-methods)) other than `extern`, `async`, or `static`, except that an explicit implementation in an interface may include `abstract` to reabstract the member. An explicit interface member implementation shall be static if and only if the implemented member is static. An explicit interface member implementation inherits any type parameter constraints from the interface. @@ -1097,12 +1111,12 @@ When a generic method implicitly implements an interface method, the constraints A class or struct shall provide implementations for all abstract members of the interfaces that are listed in the base class list of the class or struct which do not have a reachable implementation; where an implementation can become unreachable due to reabstraction [§19.4.3](interfaces.md#1943-interface-methods). The process of locating implementations of interface members in an implementing class or struct is known as ***interface mapping***. -Interface mapping for a class or struct `C` locates an implementation for each member of each interface specified in the base class list of `C`. The implementation of a particular interface member `I.M`, where `I` is the interface in which the member `M` is declared, is determined by examining each class, interface, or struct `S`, starting with `C` and repeating for each successive base class and implemented interface of `C`, until a match is located: +Interface mapping for a class or struct `C` locates an implementation for each instance or static abstract or virtual member of each interface specified in the base class list of `C`. The implementation of a particular interface member `I.M`, where `I` is the interface in which the member `M` is declared, is determined by examining each class or struct `S`, starting with `C` and repeating for each successive base class of `C`, until a match is located: - If `S` contains a declaration of an explicit interface member implementation that matches `I` and `M`, then this member is the implementation of `I.M`. -- Otherwise, if `S` contains a declaration of a non-static public member that matches `M`, then this member is the implementation of `I.M`. If more than one member matches, it is unspecified which member is the implementation of `I.M`. This situation can only occur if `S` is a constructed type where the two members as declared in the generic type have different signatures, but the type arguments make their signatures identical. +- Otherwise, if `M` is public and `S` contains a declaration of a public member that matches `M`, then this member is the implementation of `I.M`. If more than one member matches, it is unspecified which member is the implementation of `I.M`. This situation can only occur if `S` is a constructed type where the two members as declared in the generic type have different signatures, but the type arguments make their signatures identical. -A compile-time error occurs if implementations cannot be located for all members of all interfaces specified in the base class list of `C`. The members of an interface include those members that are inherited from base interfaces. +If no class or struct member is located, the most specific implementation in the implemented interfaces is used ([§19.4.10](interfaces.md#19410-most-specific-implementation)). A compile-time error occurs if there is no unique most specific implementation, or if that implementation is abstract and no class or struct member implements it. An abstract class may implement an instance member with an abstract member as specified in [§19.6.8](interfaces.md#1968-abstract-classes-and-interfaces). Non-virtual static interface members do not require or participate in interface mapping. Members of a constructed interface type are considered to have any type parameters replaced with the corresponding type arguments as specified in [§15.3.3](classes.md#1533-members-of-constructed-types). @@ -1126,17 +1140,23 @@ Members of a constructed interface type are considered to have any type paramete > > *end example* -For purposes of interface mapping, a class, interface, or struct member `A` matches an interface member `B` when: +For purposes of interface mapping, a class, interface, or struct member `A` matches an interface member `B` as follows. The members shall both be static or both be instance members. In addition, one of the following conditions shall be satisfied: - `A` and `B` are methods, and the name, type, and parameter lists of `A` and `B` are identical. - `A` and `B` are properties, the name and type of `A` and `B` are identical, and `A` has the same accessors as `B` (`A` is permitted to have additional accessors if it is not an explicit interface member implementation). - `A` and `B` are events, and the name and type of `A` and `B` are identical. - `A` and `B` are indexers, the type and parameter lists of `A` and `B` are identical, and `A` has the same accessors as `B` (`A` is permitted to have additional accessors if it is not an explicit interface member implementation). +- `A` and `B` are unary or binary operators with the same operator token, return type, and parameter types. +- `A` and `B` are conversion operators with the same conversion kind (`implicit` or `explicit`), source type, and target type. + +For an explicit implementation, the qualified interface member name identifies `B`; for other implementations, the unqualified member names are compared. + +For operator matching, `A` and `B` shall agree in whether the `checked` modifier is present. Notable implications of the interface-mapping algorithm are: - Explicit interface member implementations take precedence over other members in the same class or struct when determining the class or struct member that implements an interface member. -- Neither non-public nor static members participate in interface mapping. +- Non-public class or struct members participate in interface mapping only as explicit interface member implementations. > *Example*: In the following code > diff --git a/standard/patterns.md b/standard/patterns.md index a84e96f2d..3b3d12e02 100644 --- a/standard/patterns.md +++ b/standard/patterns.md @@ -218,6 +218,8 @@ constant_pattern A constant pattern `P` is *applicable to* a type `T` if there is an implicit conversion from the constant expression of `P` to the type `T`. +For a numeric constant, if the input type is, implements, or inherits a constructed `System.Numerics.INumberBase` interface ([§C.3](standard-library.md#c3-standard-library-types-not-defined-in-isoiec-23271)), or is a type parameter whose effective interface set includes such an interface, there shall be an identity conversion, implicit numeric conversion, or implicit constant expression conversion from the constant expression to the input type. + For a constant pattern `P`, its *converted value* is - if the pattern input value’s type is an integral type or an enum type, the pattern’s constant value converted to that type; otherwise @@ -617,7 +619,7 @@ The *relational_expression* in a *relational_pattern* is required to evaluate to Relational patterns support the relational operators `<`, `<=`, `>`, and `>=` on all of the built-in types that support such binary relational operators with both operands having the same type: `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `char`, `float`, `double`, `decimal`, `nint`, `nuint`, and enums. -A *relational_pattern* is *applicable to* a type `T` if a suitable built-in binary relational operator is defined with both operands of type `T`, or if an explicit nullable or unboxing conversion exists from `T` to the type of the constant expression. +A *relational_pattern* is *applicable to* a type `T` if a suitable built-in binary relational operator is defined with both operands of type `T`, or if an explicit nullable or unboxing conversion exists from `T` to the type of the constant expression. If `T` is, implements, or inherits a constructed `System.Numerics.INumberBase` interface, or is a type parameter whose effective interface set includes such an interface, the suitable built-in binary relational operator is required. It is a compile-time error if the expression evaluates to `double.NaN`, `float.NaN`, or a null constant. diff --git a/standard/standard-library.md b/standard/standard-library.md index 5e491c7a2..cd79913a0 100644 --- a/standard/standard-library.md +++ b/standard/standard-library.md @@ -982,6 +982,14 @@ namespace System.Threading } } +namespace System.Numerics +{ + public interface INumberBase + where TSelf : INumberBase + { + } +} + namespace System.Threading.Tasks { public class Task diff --git a/standard/types.md b/standard/types.md index 5e158def1..a7aa3a6b8 100644 --- a/standard/types.md +++ b/standard/types.md @@ -716,6 +716,8 @@ type_argument Each type argument shall satisfy any constraints on the corresponding type parameter ([§15.2.5](classes.md#1525-type-parameter-constraints)). A reference type argument whose nullability does not match the nullability of the type parameter satisfies the constraint; however a warning may be issued. +An interface type argument shall satisfy the most specific implementation requirement for static abstract and virtual members ([§19.4.10](interfaces.md#19410-most-specific-implementation)). + ### 8.4.3 Open and closed types A type is either an ***open type*** or a ***closed type***. An open type is a type that involves type parameters. More specifically: @@ -795,7 +797,7 @@ Since a type parameter can be instantiated with many different type arguments, t > - The literal `null` cannot be converted to a type given by a type parameter, except if the type parameter is known to be a reference type ([§10.2.12](conversions.md#10212-implicit-conversions-involving-type-parameters)). However, a default expression ([§12.8.21](expressions.md#12821-default-value-expressions)) can be used instead. In addition, a value with a type given by a type parameter *can* be compared with null using `==` and `!=` ([§12.15.7](expressions.md#12157-reference-type-equality-operators)) unless the type parameter has the value type constraint. > - A `new` expression ([§12.8.17.2](expressions.md#128172-object-creation-expressions)) can only be used with a type parameter if the type parameter is constrained by a *constructor_constraint* or the value type constraint ([§15.2.5](classes.md#1525-type-parameter-constraints)). > - A type parameter cannot be used anywhere within an attribute ([§23.2.1](attributes.md#2321-general)). -> - A type parameter cannot be used in a member access ([§12.8.7](expressions.md#1287-member-access)) or type name ([§7.7](basic-concepts.md#77-namespace-and-type-names)) to identify a static member or a nested type. +> - A type parameter can identify a static abstract or virtual interface member in a member access as specified in [§12.8.7](expressions.md#1287-member-access). It cannot identify other static members or nested types. > - A type parameter can only be used as an *unmanaged_type* ([§8.8](types.md#88-unmanaged-types)) if the type parameter is constrained by the unmanaged constraint ([§15.2.5](classes.md#1525-type-parameter-constraints)). > - Except when a type parameter is explicitly constrained to value types, the nullable type annotation (`?`) can only be applied to an occurrence of a type parameter when the nullable annotations flag is enabled ([§6.5.9](lexical-structure.md#659-nullable-directive), [§15.2.5](classes.md#1525-type-parameter-constraints)). >