diff --git a/standard/basic-concepts.md b/standard/basic-concepts.md index 845a86529..62beba210 100644 --- a/standard/basic-concepts.md +++ b/standard/basic-concepts.md @@ -146,7 +146,8 @@ A declaration defines a name in the ***declaration space*** to which the declara > *Note*: However, these declarations could introduce ambiguities if included in the same application. *end note* - Two or more methods with the same name but distinct signatures are allowed in the same declaration space ([§7.5](basic-concepts.md#75-signatures-and-overloading)). - Two or more type declarations with the same name but distinct numbers of type parameters are allowed in the same declaration space ([§7.7.2](basic-concepts.md#772-unqualified-names)). -- Two or more type declarations with the partial modifier in the same declaration space may share the same name, same number of type parameters and same classification (class, record class, struct, record struct, or interface). The keywords `record` and `record class` specify the same classification. In this case, the type declarations contribute to a single type and are themselves aggregated to form a single declaration space ([§15.2.7](classes.md#1527-partial-type-declarations)). +- A file-local type ([§15.2.2.1](classes.md#15221-general)) may share a name with a namespace or type declared entirely in other compilation units. Such entities are distinct. +- Two or more type declarations with the partial modifier in the same declaration space may share the same name, same number of type parameters and same classification (class, record class, struct, record struct, or interface). The keywords `record` and `record class` specify the same classification. Such declarations are combined as specified in [§15.2.7](classes.md#1527-partial-type-declarations), sharing a declaration space for each resulting type. - A namespace declaration and a type declaration in the same declaration space can share the same name as long as the type declaration has at least one type parameter ([§7.7.2](basic-concepts.md#772-unqualified-names)). There are several different types of declaration spaces, as described in the following. @@ -947,6 +948,11 @@ A declaration of a new member hides an inherited member only within the scope of Several contexts in a C# program require a *namespace_name* or a *type_name* to be specified. +Namespace and type name resolution, simple-name resolution ([§12.8.4](expressions.md#1284-simple-names)), member access ([§12.8.7](expressions.md#1287-member-access)), and qualified alias member resolution ([§14.9.1](namespaces.md#1491-general)) apply the following filtering rules. At each lookup step that considers namespace members or a set of imported members, candidates with the required name and number of type parameters are filtered before namespace or type selection and ambiguity checks: + +- A file-local type is a candidate only in the compilation unit in which it is declared. +- If the candidate set includes a file-local type, only file-local types are retained. + ```ANTLR namespace_name : namespace_or_type_name @@ -1081,7 +1087,7 @@ Every namespace and type declaration has a ***fully qualified name,*** which uni - If `N` is a member of the global namespace, its fully qualified name is `N`. - Otherwise, its fully qualified name is `S.N`, where `S` is the fully qualified name of the namespace or type declaration in which `N` is declared. -In other words, the fully qualified name of `N` is the complete hierarchical path of identifiers and *generic_dimension_specifier*s that lead to `N`, starting from the global namespace. Because every member of a namespace or type shall have a unique name, it follows that the fully qualified name of a namespace or type declaration is always unique. It is a compile-time error for the same fully qualified name to refer to two distinct entities. In particular: +In other words, the fully qualified name of `N` is the complete hierarchical path of identifiers and *generic_dimension_specifier*s that lead to `N`, starting from the global namespace. Subject to the file-local type exception in [§7.2](basic-concepts.md#72-declarations), it is a compile-time error for the same fully qualified name to refer to two distinct entities. This exception also applies to types nested within those distinct types. In particular, for declarations to which the exception does not apply: - It is an error for both a namespace declaration and a type declaration to have the same fully qualified name. - It is an error for two different kinds of type declarations to have the same fully qualified name (for example, if both a struct and class declaration have the same fully qualified name). diff --git a/standard/classes.md b/standard/classes.md index 7f702e22f..597aa3d18 100644 --- a/standard/classes.md +++ b/standard/classes.md @@ -49,6 +49,7 @@ class_modifier | 'abstract' | 'sealed' | 'static' + | 'file' | unsafe_modifier // unsafe code support ; ``` @@ -61,10 +62,38 @@ The `new` modifier is permitted on nested classes. It specifies that the class h The `public`, `protected`, `internal`, and `private` modifiers control the accessibility of the class. Depending on the context in which the class declaration occurs, some of these modifiers might not be permitted ([§7.4.2](basic-concepts.md#742-declared-accessibility)). +No declaration of a file-local type shall include an accessibility modifier. + When a partial type declaration ([§15.2.7](classes.md#1527-partial-type-declarations)) includes an accessibility specification (via the `public`, `protected`, `internal`, and `private` modifiers), that specification shall agree with all other parts that include an accessibility specification. If no part of a partial type includes an accessibility specification, the type is given the appropriate default accessibility ([§7.4.2](basic-concepts.md#742-declared-accessibility)). The `abstract`, `sealed`, and `static` modifiers are discussed in the following subclauses. +The `file` modifier specifies that the type being declared is local to its parent compilation unit. Declaration identity and name conflicts for file-local types are specified in [§7.2](basic-concepts.md#72-declarations). (`file` is a contextual keyword (§6.4.4) that has special meaning when used as a top-level type modifier.) + +The `file` modifier shall only appear in a type declaration for a top-level type. + +When a type declaration contains the `file` modifier, that type is said to be a ***file-local type***. + +Distinct types under the rules of [§7.2](basic-concepts.md#72-declarations) shall be distinct at runtime, including file-local types with the same fully qualified name. The name and accessibility of a file-local type in metadata are implementation-defined. + +A type ***involves a file-local type*** if it is a file-local type, is nested in a type that involves a file-local type, or has any of the following that involves a file-local type: + +- An element type, for an array or pointer type. +- An underlying type, for a nullable value type. +- A type argument, for a constructed type. +- An element type, for a tuple type. +- A parameter or return type, for a function pointer type. + +For a type that is neither file-local nor nested within a file-local type, the following types shall not involve a file-local type: + +- Its direct base class, if it is a class, or its explicit base interfaces, if it is an interface. +- The constraint types specified for its type parameters or for the type parameters of its members. +- The types of constants, fields, properties, events, and indexers declared in it. +- The parameter and return types of function members declared in it. +- Its parameter and return types, if it is a delegate. + +A file-local class that is an attribute type may be used as an attribute within both file-local types and non-file-local types, just as if the attribute type were a non-file-local class. + #### 15.2.2.2 Abstract classes The `abstract` modifier is used to indicate that a class is incomplete and that it is intended to be used only as a base class. An ***abstract class*** differs from a ***non-abstract class*** in the following ways: @@ -249,6 +278,8 @@ The direct base class of a class type shall be at least as accessible as the cla The direct base class of a class type shall not be any of the following types: `System.Array`, `System.Delegate`, `System.Enum`, `System.ValueType` or the `dynamic` type. Furthermore, a generic class declaration shall not use `System.Attribute` as a direct or indirect base class ([§23.2.1](attributes.md#2321-general)). +The file-local type restrictions ([§15.2.2.1](classes.md#15221-general)) apply to the direct base class. + In determining the meaning of the direct base class specification `A` of a class `B`, the direct base class of `B` is temporarily assumed to be `object`, which ensures that the meaning of a base class specification cannot recursively depend on itself. Given this definition, the complete set of types upon which a class depends is the transitive closure of the *directly depends on* relationship. > *Example*: The following @@ -785,6 +816,8 @@ The modifier `partial` is used when defining a class, struct, or interface type Each part of a ***partial type*** declaration shall include a `partial` modifier and shall be declared in the same namespace or containing type as the other parts. The `partial` modifier indicates that additional parts of the type declaration might exist elsewhere, but the existence of such additional parts is not a requirement; it is valid for the only declaration of a type to include the `partial` modifier. It is valid for only one declaration of a partial type to include the base class or implemented interfaces. However, all declarations of a base class or implemented interfaces shall match, including the nullability of any specified type arguments. +For top-level partial type declarations with the same namespace, name, number of type parameters, and classification ([§7.2](basic-concepts.md#72-declarations)), each compilation unit containing a declaration with the `file` modifier contributes a distinct file-local type consisting of the matching declarations in that compilation unit. Matching declarations in compilation units containing no such `file` declaration contribute to a separate non-file-local type. The modifier restrictions for file-local types apply to all parts of a file-local type. + All parts of a partial type shall be compiled together such that the parts can be merged at compile-time. Partial types specifically do not allow already compiled types to be extended. Nested types may be declared in multiple parts by using the `partial` modifier. Typically, the containing type is declared using `partial` as well, and each part of the nested type is declared in a different part of the containing type. @@ -863,6 +896,8 @@ class_member_declaration ; ``` +The file-local type restrictions ([§15.2.2.1](classes.md#15221-general)) apply to members declared in a class. + The members of a class are divided into the following categories: - Constants, which represent constant values associated with the class ([§15.4](classes.md#154-constants)). diff --git a/standard/delegates.md b/standard/delegates.md index c76c88799..71f6e0175 100644 --- a/standard/delegates.md +++ b/standard/delegates.md @@ -29,6 +29,7 @@ delegate_modifier | 'protected' | 'internal' | 'private' + | 'file' | unsafe_modifier // unsafe code support ; ``` @@ -41,7 +42,11 @@ A delegate declaration that supplies a *variant_type_parameter_list* is a generi The `new` modifier is only permitted on delegates declared within another type, in which case it specifies that such a delegate hides an inherited member by the same name, as described in [§15.3.5](classes.md#1535-the-new-modifier). -The `public`, `protected`, `internal`, and `private` modifiers control the accessibility of the delegate type. Depending on the context in which the delegate declaration occurs, some of these modifiers might not be permitted ([§7.4.2](basic-concepts.md#742-declared-accessibility)). +The `public`, `protected`, `internal`, and `private` modifiers control the accessibility of the delegate type. Depending on the context in which the delegate declaration occurs, some of these modifiers might not be permitted ([§7.4.2](basic-concepts.md#742-declared-accessibility)). It is a compile-time error for any of the `public`, `protected`, `internal`, and `private` modifiers to be combined with the `file` modifier. + +The modifier `file` has the same meaning as that for a class declaration ([§15.2.2](classes.md#1522-class-modifiers)). + +The file-local type restrictions ([§15.2.2.1](classes.md#15221-general)) apply to the delegate's parameter types, return type, and type parameter constraints. The delegate’s type name is *identifier*. diff --git a/standard/enums.md b/standard/enums.md index 35dbeb53b..4ce22401c 100644 --- a/standard/enums.md +++ b/standard/enums.md @@ -85,11 +85,14 @@ enum_modifier | 'protected' | 'internal' | 'private' + | 'file' ; ``` It is a compile-time error for the same modifier to appear multiple times in an enum declaration. +It is a compile-time error for any of the `public`, `protected`, `internal`, and `private` modifiers to be combined with the `file` modifier. + The modifiers of an enum declaration have the same meaning as those of a class declaration ([§15.2.2](classes.md#1522-class-modifiers)). However, the `abstract`, and `sealed`, and `static` modifiers are not permitted in an enum declaration. Enums cannot be abstract and do not permit derivation. ## 20.4 Enum members diff --git a/standard/expressions.md b/standard/expressions.md index 142e87ff6..c935ceb27 100644 --- a/standard/expressions.md +++ b/standard/expressions.md @@ -368,6 +368,8 @@ A ***lifted operator*** permits predefined and user-defined operators that opera A member lookup is the process whereby the meaning of a name in the context of a type is determined. A member lookup can occur as part of evaluating a *simple_name* ([§12.8.4](expressions.md#1284-simple-names)) or a *member_access* ([§12.8.7](expressions.md#1287-member-access)) in an expression. If the *simple_name* or *member_access* occurs as the *primary_expression* of an *invocation_expression* ([§12.8.10.2](expressions.md#128102-method-invocations)), the member is said to be *invoked*. +Lookup of namespace members follows [§7.7.1](basic-concepts.md#771-general) and [§12.8.7](expressions.md#1287-member-access), including the file-local type filtering rules. + If a member is a method or event, or if it is a constant, field or property of either a delegate type ([§21](delegates.md#21-delegates)) or the type `dynamic` ([§8.2.4](types.md#824-the-dynamic-type)), then the member is said to be *invocable.* Member lookup considers not only the name of a member but also the number of type parameters the member has and whether the member is accessible. For the purposes of member lookup, generic methods and nested generic types have the number of type parameters indicated in their respective declarations and all other members have zero type parameters. @@ -1703,6 +1705,8 @@ A *simple_name* is either of the form `I` or of the form `I`, For the namespace lookup steps in this subclause and [§7.7.1](basic-concepts.md#771-general), let *D* be the set of directives considered at namespace `N`. If `N` is the global namespace, *D* consists of the *extern_alias_directive*s and *using_directive*s in the containing compilation unit, together with the *global_using_directive*s in all compilation units in the program. Otherwise, *D* consists of the *extern_alias_directive*s and *using_directive*s in the enclosing namespace declaration for `N`, if any. Only directives whose scope includes the location of the name being resolved are considered. The exclusions when resolving directive targets are specified in [§14.5](namespaces.md#145-global-using-directives) and [§14.6](namespaces.md#146-using-directives). +Namespace members and imported members are subject to the file-local type filtering rules in [§7.7.1](basic-concepts.md#771-general). + > *Note*: Global using directives participate at the global-namespace step, not at each inner-namespace step. Their scope includes nested namespaces, but lookup in an inner namespace can find a declaration or import before reaching the global namespace. *end note* The *simple_name* is evaluated and classified as follows: @@ -1832,7 +1836,7 @@ A *member_access* is either of the form `E.I` or of the form `E.I *Note*: For example, it is a compile-time error to specify a `private` or `internal` interface in the *interface_base* of a `public` interface. *end note* It is a compile-time error for an interface to directly or indirectly inherit from itself. diff --git a/standard/lexical-structure.md b/standard/lexical-structure.md index 86a73301c..d20b9725b 100644 --- a/standard/lexical-structure.md +++ b/standard/lexical-structure.md @@ -623,15 +623,15 @@ A ***contextual keyword*** is an identifier-like sequence of characters that has ```ANTLR contextual_keyword - : 'add' | 'alias' | 'and' | 'ascending' | 'async' - | 'await' | 'by' | 'Cdecl' | 'descending' | 'dynamic' - | 'equals' | 'Fastcall' | 'from' | 'get' | 'global' - | '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' + : 'add' | 'alias' | 'and' | 'ascending' | 'async' + | 'await' | 'by' | 'Cdecl' | 'descending' | 'dynamic' + | 'equals' | 'Fastcall' | 'file' | 'from' | 'get' + | 'global' | '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' ; ``` diff --git a/standard/namespaces.md b/standard/namespaces.md index 8f1e2df22..70bc8499b 100644 --- a/standard/namespaces.md +++ b/standard/namespaces.md @@ -372,6 +372,8 @@ A *global_using_static_directive* only imports members and types declared direct Ambiguities between multiple *global_using_namespace_directive*s and *global_using_static_directives* are discussed in [§14.5.3](namespaces.md#1453-global-using-namespace-directives). +It is a compile-time error to use a file-local type in a *global_using_static_directive*. + ## 14.6 Using directives ### 14.6.1 General @@ -1018,7 +1020,7 @@ A *qualified_alias_member* has one of two forms: - `N::I`, where `N` and `I` represent identifiers, and `` is a type argument list. (`e` is always at least one.) - `N::I`, where `N` and `I` represent identifiers. (In this case, `e` is considered to be zero.) -Using this notation, the meaning of a *qualified_alias_member* is determined as follows: +Using this notation, the meaning of a *qualified_alias_member* is determined as follows. The namespace searched for the right-hand identifier is subject to the file-local type filtering rules in [§7.7.1](basic-concepts.md#771-general). - If `N` is the identifier `global`, then the global namespace is searched for `I`: - If the global namespace contains a namespace named `I` and `e` is zero, then the *qualified_alias_member* refers to that namespace. diff --git a/standard/structs.md b/standard/structs.md index 44c310734..4fcd9350a 100644 --- a/standard/structs.md +++ b/standard/structs.md @@ -60,6 +60,7 @@ struct_modifier | 'internal' | 'private' | 'readonly' + | 'file' | unsafe_modifier // unsafe code support ; ``` @@ -68,6 +69,8 @@ struct_modifier It is a compile-time error for the same modifier to appear multiple times in a struct declaration. +It is a compile-time error for any of the `public`, `protected`, `internal`, and `private` modifiers to be combined with the `file` modifier. + Except for `readonly`, the modifiers of a struct declaration have the same meaning as those of a class declaration ([§15.2.2](classes.md#1522-class-modifiers)). The `readonly` modifier indicates that the *struct_declaration* declares a type whose instances are immutable.