From 05a0dd0460edfbc87ab886e1699d18d32f5581b2 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Tue, 7 Apr 2026 08:03:45 -0400 Subject: [PATCH 1/4] Add using alias types feature Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/namespaces.md | 48 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/standard/namespaces.md b/standard/namespaces.md index 598486cb8..11f34d5ee 100644 --- a/standard/namespaces.md +++ b/standard/namespaces.md @@ -402,7 +402,7 @@ A *using_alias_directive* introduces an identifier that serves as an alias for a ```ANTLR using_alias_directive - : 'using' identifier '=' namespace_or_type_name ';' + : 'using' 'unsafe'? identifier '=' (namespace_name | type) ';' ; ``` @@ -574,7 +574,7 @@ Just like regular members, names introduced by *alias_directives* are hidden by > > *end example* -The order in which *extern_alias_directive*s are written has no significance. Likewise, the order in which *using_alias_directive*s are written has no significance, but all *using_alias_directives* shall come after all *extern_alias_directive*s in the same compilation unit or namespace body. Resolution of the *namespace_or_type_name* referenced by a *using_alias_directive* is not affected by the *using_alias_directive* itself or by other *using_directive*s in the immediately containing compilation unit or namespace body, but may be affected by *extern_alias_directive*s in the immediately containing compilation unit or namespace body. And, if the *using_alias_directive* is immediately contained in a compilation unit, is not affected by the *global_using_directive*s in the program. In other words, the *namespace_or_type_name* of a *using_alias_directive* is resolved as if the immediately containing compilation unit or namespace body had no *using_directive*s and, if the *using_alias_directive* is immediately contained in a compilation unit, the program had no *global_using_directive*s but has the correct set of *extern_alias_directive*s. +The order in which *extern_alias_directive*s are written has no significance. Likewise, the order in which *using_alias_directive*s are written has no significance, but all *using_alias_directives* shall come after all *extern_alias_directive*s in the same compilation unit or namespace body. Resolution of the `(namespace_name | type)` referenced by a *using_alias_directive* is not affected by the *using_alias_directive* itself or by other *using_directive*s in the immediately containing compilation unit or namespace body, but may be affected by *extern_alias_directive*s in the immediately containing compilation unit or namespace body. And, if the *using_alias_directive* is immediately contained in a compilation unit, is not affected by the *global_using_directive*s in the program. In other words, the `(namespace_name | type)` of a *using_alias_directive* is resolved as if the immediately containing compilation unit or namespace body had no *using_directive*s and, if the *using_alias_directive* is immediately contained in a compilation unit, the program had no *global_using_directive*s but has the correct set of *extern_alias_directive*s. > *Example*: In the following code > @@ -596,6 +596,44 @@ The order in which *extern_alias_directive*s are written has no significance. Li > the last *using_alias_directive* results in a compile-time error because it is not affected by the previous *using_alias_directive*. The first *using_alias_directive* does not result in an error since the scope of the extern alias X includes the *using_alias_directive*. > > *end example* + + + +> *Note*: Allowing the creation of an alias to a *type* rather than just to a *type_name* (as was previously the restriction in earlier editions of this specification) allows both named and unnamed types to be aliased. *end note* +The presence of `unsafe` permits pointer types ([§23.3](unsafe-code.md#233-pointer-types)) to be aliased. It causes the entire textual extent of the *type* (but not the *namespace_name*) to become an unsafe context. + +> *Example*: +> +> +> ```csharp +> namespace N +> { +> using A1 = (int, string); +> using A2 = int[]; +> using unsafe A3 = int*; +> using unsafe A4 = delegate**; +> } +> ``` +> +> *end example* + +When making an alias to a *type*, *type* shall not be a *nullable_reference_type*. + +> *Example*: +> +> +> ```csharp +> namespace N +> { +> using A1 = string?; // Error; nullable reference type not permitted +> using A2 = List; // OK; List is a non-nullable reference type +> // even though string? is not +> using A3 = int?; // OK; int? is a nullable value type +> using A4 = (string?, string?)?; // OK; tuple is a nullable value type +> } +> ``` +> +> *end example* A *using_alias_directive* can create an alias for any namespace or type, including the namespace within which it appears and any namespace or type nested within that namespace. @@ -839,7 +877,7 @@ Furthermore, when more than one namespace or type imported by *using_namespace_d Like a *using_alias_directive*, a *using_namespace_directive* does not contribute any new members to the underlying declaration space of the compilation unit or namespace, but, rather, affects only the compilation unit or namespace body in which it appears. -The *namespace_name* referenced by a *using_namespace_directive* is resolved in the same way as the *namespace_or_type_name* referenced by a *using_alias_directive*. Thus, *using_namespace_directive*s in the same compilation unit or namespace body do not affect each other and can be written in any order. +The *namespace_name* referenced by a *using_namespace_directive* is resolved in the same way as the `(namespace_name | type)` referenced by a *using_alias_directive*. Thus, *using_namespace_directive*s in the same compilation unit or namespace body do not affect each other and can be written in any order. ### 14.6.4 Using static directives @@ -847,7 +885,7 @@ A *using_static_directive* imports the nested types and static members contained ```ANTLR using_static_directive - : 'using' 'static' type_name ';' + : 'using' 'static' 'unsafe'? type_name ';' ; ``` @@ -958,6 +996,8 @@ A *using_static_directive* only imports members and types declared directly in t Ambiguities between multiple *using_namespace_directives* and *using_static_directives* are discussed in [§14.6.3](namespaces.md#1463-using-namespace-directives). +The presence of `unsafe` permits pointer types ([§24.3](unsafe-code.md#243-pointer-types)) to be aliased. It causes the entire textual extent of the *type_name* to become an unsafe context. + ## 14.7 Namespace member declarations A *namespace_member_declaration* is either a *namespace_declaration* ([§14.3](namespaces.md#143-namespace-declarations)) or a *type_declaration* ([§14.8](namespaces.md#148-type-declarations)). From c758927f2bc65a81a0bd43d4340e59cc2120141f Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Tue, 7 Apr 2026 08:10:33 -0400 Subject: [PATCH 2/4] fix md formatting Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/namespaces.md | 1 + 1 file changed, 1 insertion(+) diff --git a/standard/namespaces.md b/standard/namespaces.md index 11f34d5ee..9aa8a4574 100644 --- a/standard/namespaces.md +++ b/standard/namespaces.md @@ -600,6 +600,7 @@ The order in which *extern_alias_directive*s are written has no significance. Li > *Note*: Allowing the creation of an alias to a *type* rather than just to a *type_name* (as was previously the restriction in earlier editions of this specification) allows both named and unnamed types to be aliased. *end note* + The presence of `unsafe` permits pointer types ([§23.3](unsafe-code.md#233-pointer-types)) to be aliased. It causes the entire textual extent of the *type* (but not the *namespace_name*) to become an unsafe context. > *Example*: From 288c14b34be1741a062efd015e7b23c0a7199899 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 21 Sep 2026 17:09:39 -0400 Subject: [PATCH 3/4] Fix using alias pointer type cross-reference Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: bce5e82a-89fd-4655-bc08-6d6ba97f0cc6 Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/namespaces.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/standard/namespaces.md b/standard/namespaces.md index 9aa8a4574..56fdb79c7 100644 --- a/standard/namespaces.md +++ b/standard/namespaces.md @@ -601,7 +601,7 @@ The order in which *extern_alias_directive*s are written has no significance. Li > *Note*: Allowing the creation of an alias to a *type* rather than just to a *type_name* (as was previously the restriction in earlier editions of this specification) allows both named and unnamed types to be aliased. *end note* -The presence of `unsafe` permits pointer types ([§23.3](unsafe-code.md#233-pointer-types)) to be aliased. It causes the entire textual extent of the *type* (but not the *namespace_name*) to become an unsafe context. +The presence of `unsafe` permits pointer types ([§24.3](unsafe-code.md#243-pointer-types)) to be aliased. It causes the entire textual extent of the *type* (but not the *namespace_name*) to become an unsafe context. > *Example*: > From 8517b1729e5a3b88ff38b1f18538b1b506456945 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 25 Nov 2026 16:46:52 -0500 Subject: [PATCH 4/4] A few clarifications Make a few clarifications on the use of type aliases (such as tuple types), and unsafe aliases. --- standard/namespaces.md | 12 ++++++++---- standard/unsafe-code.md | 6 ++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/standard/namespaces.md b/standard/namespaces.md index 56fdb79c7..8cc452440 100644 --- a/standard/namespaces.md +++ b/standard/namespaces.md @@ -293,10 +293,12 @@ The *identifier* of a *global_using_alias_directive* shall be unique within the Just like regular members, names introduced by *global_using_alias_directive*s are hidden by similarly named members in nested scopes. -The order in which *global_using_alias_directive*s are written has no significance, and resolution of the *namespace_or_type_name* referenced by a *global_using_alias_directive* is not affected by the *global_using_alias_directive* itself or by other *global_using_directive*s or *using_directive*s in the program. In other words, the *namespace_or_type_name* of a *global_using_alias_directive* is resolved as if the immediately containing compilation unit had no *using_directive*s and the entire containing program had no *global_using_directive*s. A *global_using_alias_directive* may however be affected by *extern_alias_directive*s in the immediately containing compilation unit. +The order in which *global_using_alias_directive*s are written has no significance, and resolution of the namespace or type referenced by a *global_using_alias_directive* is not affected by the *global_using_alias_directive* itself or by other *global_using_directive*s or *using_directive*s in the program. In other words, the namespace or type referenced by a *global_using_alias_directive* is resolved as if the immediately containing compilation unit had no *using_directive*s and the entire containing program had no *global_using_directive*s. A *global_using_alias_directive* may however be affected by *extern_alias_directive*s in the immediately containing compilation unit. A *global_using_alias_directive* can create an alias for any namespace or type. +The restrictions on alias target types and the effect of `unsafe` are the same as for a *using_alias_directive* ([§14.6.2](namespaces.md#1462-using-alias-directives)). + Accessing a namespace or type through an alias yields exactly the same result as accessing that namespace or type through its declared name. Using aliases can name a closed constructed type, but cannot name an unbound generic type declaration without supplying type arguments. @@ -352,7 +354,7 @@ Furthermore, when more than one namespace or type imported by *global_using_name > > *end example* -The *namespace_name* referenced by a *global_using_namespace_directive* is resolved in the same way as the *namespace_or_type_name* referenced by a *global_using_alias_directive*. Thus, *global_using_namespace_directive*s in the same program do not affect each other and can be written in any order. +The *namespace_name* referenced by a *global_using_namespace_directive* is resolved in the same way as the namespace or type referenced by a *global_using_alias_directive*. Thus, *global_using_namespace_directive*s in the same program do not affect each other and can be written in any order. ### 14.5.4 Global using static directives @@ -364,6 +366,8 @@ global_using_static_directive ; ``` +The *type_name* referenced by a *global_using_static_directive* is resolved in the same way as the namespace or type referenced by a *global_using_alias_directive* ([§14.5.2](namespaces.md#1452-global-using-alias-directives)). The effect of `unsafe` is the same as for a *using_static_directive* ([§14.6.4](namespaces.md#1464-using-static-directives)). + Within global attributes, top-level statements, and member declarations in a program that contains a *global_using_static_directive*, the accessible nested types and static members (except extension methods) contained directly in the declaration of the given type can be referenced directly. A *global_using_static_directive* specifically does not import extension methods directly as static methods, but makes them available for extension method invocation. @@ -601,7 +605,7 @@ The order in which *extern_alias_directive*s are written has no significance. Li > *Note*: Allowing the creation of an alias to a *type* rather than just to a *type_name* (as was previously the restriction in earlier editions of this specification) allows both named and unnamed types to be aliased. *end note* -The presence of `unsafe` permits pointer types ([§24.3](unsafe-code.md#243-pointer-types)) to be aliased. It causes the entire textual extent of the *type* (but not the *namespace_name*) to become an unsafe context. +The presence of `unsafe` establishes an unsafe context for the target *type* ([§24.2](unsafe-code.md#242-unsafe-contexts)), permitting pointer types ([§24.3](unsafe-code.md#243-pointer-types)) to be aliased. > *Example*: > @@ -997,7 +1001,7 @@ A *using_static_directive* only imports members and types declared directly in t Ambiguities between multiple *using_namespace_directives* and *using_static_directives* are discussed in [§14.6.3](namespaces.md#1463-using-namespace-directives). -The presence of `unsafe` permits pointer types ([§24.3](unsafe-code.md#243-pointer-types)) to be aliased. It causes the entire textual extent of the *type_name* to become an unsafe context. +The presence of `unsafe` establishes an unsafe context for the target *type_name* ([§24.2](unsafe-code.md#242-unsafe-contexts)), permitting pointer types ([§24.3](unsafe-code.md#243-pointer-types)) within that target type syntax. ## 14.7 Namespace member declarations diff --git a/standard/unsafe-code.md b/standard/unsafe-code.md index ff46e8ec2..ffc7adcab 100644 --- a/standard/unsafe-code.md +++ b/standard/unsafe-code.md @@ -18,14 +18,16 @@ An implementation that does not support unsafe code is required to diagnose any ## 24.2 Unsafe contexts -The unsafe features of C# are available only in unsafe contexts. An unsafe context is introduced by including an `unsafe` modifier in the declaration of a type, member, or local function, or by employing an *unsafe_statement*: +The unsafe features of C# are available only in unsafe contexts. An unsafe context is introduced by including an `unsafe` modifier in the declaration of a type, member, or local function, by employing an *unsafe_statement*, or by including `unsafe` in a using alias or using static directive: - A declaration of a class, struct, interface, or delegate may include an `unsafe` modifier, in which case, the entire textual extent of that type declaration (including the body of the class, struct, or interface) is considered an unsafe context. > *Note*: If the *type_declaration* is partial, only that part is an unsafe context. *end note* - A declaration of a field, method, property, event, indexer, operator, instance constructor, finalizer, static constructor, or local function may include an `unsafe` modifier, in which case, the entire textual extent of that member declaration is considered an unsafe context. - An *unsafe_statement* enables the use of an unsafe context within a *block*. The entire textual extent of the associated *block* is considered an unsafe context. A local function declared within an unsafe context is itself unsafe. +- A *using_alias_directive* ([§14.6.2](namespaces.md#1462-using-alias-directives)) or *global_using_alias_directive* ([§14.5.2](namespaces.md#1452-global-using-alias-directives)) may include `unsafe`, in which case, only the entire textual extent of its target *type* (not a target *namespace_name*) is considered an unsafe context. +- A *using_static_directive* ([§14.6.4](namespaces.md#1464-using-static-directives)) or *global_using_static_directive* ([§14.5.4](namespaces.md#1454-global-using-static-directives)) may include `unsafe`, in which case, only the entire textual extent of its target *type_name* is considered an unsafe context. -The associated grammar extensions are shown below and in subsequent subclauses. +The associated grammar extensions are shown below, in subsequent subclauses, and in the using directive productions referenced above. ```ANTLR unsafe_modifier