Skip to content

Commit 72864c3

Browse files
committed
Port text from dotnet#700
The first commit ports in the text from dotnet#700 substantially without change. The only editorial change is to move the clause on "Nullable directives" in lexical-structure before the clause on Pragma directives. In addition, it fixes build warnings
1 parent 1ba5991 commit 72864c3

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

standard/basic-concepts.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ The behavior of the garbage collector can be controlled, to some degree, via sta
10241024
> {
10251025
> static void Main()
10261026
> {
1027-
> B b = new B(new A());
1027+
> B? b = new B(new A());
10281028
> b = null;
10291029
> GC.Collect();
10301030
> GC.WaitForPendingFinalizers();
@@ -1069,19 +1069,19 @@ The behavior of the garbage collector can be controlled, to some degree, via sta
10691069
>
10701070
> class B
10711071
> {
1072-
> public A Ref;
1072+
> public A? Ref;
10731073
>
10741074
> ~B()
10751075
> {
10761076
> Console.WriteLine("Finalize instance of B");
1077-
> Ref.F();
1077+
> Ref?.F();
10781078
> }
10791079
> }
10801080
>
10811081
> class Test
10821082
> {
1083-
> public static A RefA;
1084-
> public static B RefB;
1083+
> public static A? RefA;
1084+
> public static B? RefB;
10851085
>
10861086
> static void Main()
10871087
> {

standard/lexical-structure.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ right_shift_assignment
10261026

10271027
### 6.5.1 General
10281028

1029-
The pre-processing directives provide the ability to conditionally skip sections of compilation units, to report error and warning conditions, and to delineate distinct regions of source code.
1029+
The pre-processing directives provide the ability to conditionally skip sections of compilation units, to report error and warning conditions, to delineate distinct regions of source code, and to set the nullable context.
10301030

10311031
> *Note*: The term “pre-processing directives” is used only for consistency with the C and C++ programming languages. In C#, there is no separate pre-processing step; pre-processing directives are processed as part of the lexical analysis phase. *end note*
10321032
@@ -1042,6 +1042,7 @@ fragment PP_Kind
10421042
| PP_Diagnostic
10431043
| PP_Region
10441044
| PP_Pragma
1045+
| PP_Nullable
10451046
;
10461047
10471048
// Only recognised at the beginning of a line
@@ -1078,10 +1079,11 @@ The following pre-processing directives are available:
10781079
- `#error`, which is used to issue errors ([§6.5.6](lexical-structure.md#656-diagnostic-directives)).
10791080
- `#region` and `#endregion`, which are used to explicitly mark sections of source code ([§6.5.7](lexical-structure.md#657-region-directives)).
10801081
- `#pragma`, which is used to specify optional contextual information to a compiler ([§6.5.9](lexical-structure.md#659-pragma-directives)).
1082+
- `#nullable`, which is used to specify the nullable context (§Nullable-Directives).
10811083

10821084
A pre-processing directive always occupies a separate line of source code and always begins with a `#` character and a pre-processing directive name. White space may occur before the `#` character and between the `#` character and the directive name.
10831085

1084-
A source line containing a `#define`, `#undef`, `#if`, `#elif`, `#else`, `#endif`, `#line`, or `#endregion` directive can end with a single-line comment. Delimited comments (the `/* */` style of comments) are not permitted on source lines containing pre-processing directives.
1086+
A source line containing a `#define`, `#undef`, `#if`, `#elif`, `#else`, `#endif`, `#line`, `#endregion`, or `#nullable` directive can end with a single-line comment. Delimited comments (the `/* */` style of comments) are not permitted on source lines containing pre-processing directives.
10851087

10861088
Pre-processing directives are not part of the syntactic grammar of C#. However, pre-processing directives can be used to include or exclude sequences of tokens and can in that way affect the meaning of a C# program.
10871089

@@ -1507,6 +1509,40 @@ A `#line hidden` directive has no effect on the compilation unit and line number
15071509
15081510
> *Note*: Although a *PP_Compilation_Unit_Name* might contain text that looks like an escape sequence, such text is not an escape sequence; in this context a ‘`\`’ character simply designates an ordinary backslash character. *end note*
15091511
1512+
### §Nullable-Directives Nullable directives
1513+
1514+
Nullable directives control the nullable contextsNullable-Contexts), as described below.
1515+
1516+
```ANTLR
1517+
fragment PP_Nullable
1518+
: PP_Whitespace? '#' PP_Whitespace? 'nullable' PP_Whitespace PP_Nullable_Action
1519+
(PP_Whitespace PP_Nullable_Target)? PP_New_Line
1520+
;
1521+
fragment PP_Nullable_Action
1522+
: 'disable'
1523+
| 'enable'
1524+
| 'restore'
1525+
;
1526+
fragment PP_Nullable_Target
1527+
: 'warnings'
1528+
| 'annotations'
1529+
;
1530+
```
1531+
1532+
A nullable directive sets the denoted nullable context(s) for subsequent lines of code, until another nullable directive overrides it, or until the end of the source code is reached. The effect of each form of nullable directive is, as follows:
1533+
1534+
- `#nullable disable`: Sets both nullable contexts to “disabled”
1535+
- `#nullable enable`: Sets both nullable contexts to “enabled”
1536+
- `#nullable restore`: Restores both nullable contexts to the states specified by the external mechanism, if any
1537+
- `#nullable disable annotations`: Sets the nullable annotation context to “disabled”
1538+
- `#nullable enable annotations`: Sets the nullable annotation context to “enabled”
1539+
- `#nullable restore annotations`: Restores the nullable annotation context to the state specified by the external mechanism, if any
1540+
- `#nullable disable warnings`: Sets the nullable warning context to “disabled”
1541+
- `#nullable enable warnings`: Sets the nullable warning context to “enabled”
1542+
- `#nullable restore warnings`: Restores the nullable warning context to the state specified by the external mechanism, if any
1543+
1544+
Disabling a nullable context that is already disabled has no effect. Likewise, enabling a nullable context that is already enabled has no effect.
1545+
15101546
### 6.5.9 Pragma directives
15111547
15121548
The `#pragma` preprocessing directive is used to specify contextual information to a compiler.

tools/example-templates/standalone-console/Project.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>disable</Nullable>
7+
<Nullable>annotations</Nullable>
88
<AssemblyName>$example-name</AssemblyName>
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1010
<Nullable>annotations</Nullable>

0 commit comments

Comments
 (0)