Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions standard/namespaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -402,7 +406,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) ';'
;
```

Expand Down Expand Up @@ -574,7 +578,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
>
Expand All @@ -596,6 +600,45 @@ 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*
<!-- markdownlint-disable MD028 -->

<!-- markdownlint-enable MD028 -->
> *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` 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*:
>
> <!-- Example: {template:"standalone-lib-without-using", name:"UsingAliasDirectives14"} -->
> ```csharp
> namespace N
> {
> using A1 = (int, string);
> using A2 = int[];
> using unsafe A3 = int*;
> using unsafe A4 = delegate*<void>*;
> }
> ```
>
> *end example*

When making an alias to a *type*, *type* shall not be a *nullable_reference_type*.

> *Example*:
>
> <!-- Example: {template:"standalone-lib", name:"UsingAliasDirectives15", expectedErrors:["CS9132"]} -->
> ```csharp
> namespace N
> {
> using A1 = string?; // Error; nullable reference type not permitted
> using A2 = List<string?>; // 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.

Expand Down Expand Up @@ -839,15 +882,15 @@ 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

A *using_static_directive* imports the nested types and static members contained directly in a type declaration into the immediately enclosing compilation unit or namespace body, enabling the identifier of each member and type to be used without qualification.

```ANTLR
using_static_directive
: 'using' 'static' type_name ';'
: 'using' 'static' 'unsafe'? type_name ';'
;
```

Expand Down Expand Up @@ -958,6 +1001,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` 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

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)).
Expand Down
6 changes: 4 additions & 2 deletions standard/unsafe-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading