Skip to content

Commit d5abbe3

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. fix build warnings This will likely cause new failures, as I turned on nullable annotations and warnings in one of the templates. Update code samples for the template in use to remove nullable warnings. I did this as we plan to have nullable annotations on by default in this version of the spec. Revert "fix build warnings" This reverts commit dcb3731. test for nullable annotations If the samples compile cleanly in this mode, I can continue in this PR without breaking all the samples. revert changes for type system.
1 parent ff77f03 commit d5abbe3

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

standard/attributes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ The attribute instance represented by `T`, `C`, `P`, and `N`, and associated wi
490490
<!-- markdownlint-enable MD028 -->
491491
> *Example*: In an implementation of the CLI, the `Help` attribute instances in the assembly created by compiling the example program in [§22.2.3](attributes.md#2223-positional-and-named-parameters) can be retrieved with the following program:
492492
>
493-
> <!-- Example: {template:"standalone-console", name:"RuntimeAttributeInstanceRetrieval", expectedOutput:["Type : HelpAttribute","Type : InterrogateHelpUrls"], additionalFiles:["HelpAttribute.cs"], executionArgs:["RuntimeAttributeInstanceRetrieval"]} -->
493+
> <!-- Example: {template:"standalone-console", name:"RuntimeAttributeInstanceRetrieval", expectedOutput:["Type : Microsoft.CodeAnalysis.EmbeddedAttribute", "Type : System.Runtime.CompilerServices.NullableAttribute", "Type : System.Runtime.CompilerServices.NullableContextAttribute", "Type : HelpAttribute","Type : InterrogateHelpUrls"], additionalFiles:["HelpAttribute.cs"], executionArgs:["RuntimeAttributeInstanceRetrieval"]} -->
494494
> ```csharp
495495
> public sealed class InterrogateHelpUrls
496496
> {

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
@@ -1025,7 +1025,7 @@ right_shift_assignment
10251025

10261026
### 6.5.1 General
10271027

1028-
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.
1028+
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.
10291029

10301030
> *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*
10311031
@@ -1041,6 +1041,7 @@ fragment PP_Kind
10411041
| PP_Diagnostic
10421042
| PP_Region
10431043
| PP_Pragma
1044+
| PP_Nullable
10441045
;
10451046
10461047
// Only recognised at the beginning of a line
@@ -1077,10 +1078,11 @@ The following pre-processing directives are available:
10771078
- `#error`, which is used to issue errors ([§6.5.6](lexical-structure.md#656-diagnostic-directives)).
10781079
- `#region` and `#endregion`, which are used to explicitly mark sections of source code ([§6.5.7](lexical-structure.md#657-region-directives)).
10791080
- `#pragma`, which is used to specify optional contextual information to a compiler ([§6.5.9](lexical-structure.md#659-pragma-directives)).
1081+
- `#nullable`, which is used to specify the nullable context (§Nullable-Directives).
10801082

10811083
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.
10821084

1083-
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.
1085+
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.
10841086

10851087
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.
10861088

@@ -1506,6 +1508,40 @@ A `#line hidden` directive has no effect on the compilation unit and line number
15061508
15071509
> *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*
15081510
1511+
### §Nullable-Directives Nullable directives
1512+
1513+
Nullable directives control the nullable contextsNullable-Contexts), as described below.
1514+
1515+
```ANTLR
1516+
fragment PP_Nullable
1517+
: PP_Whitespace? '#' PP_Whitespace? 'nullable' PP_Whitespace PP_Nullable_Action
1518+
(PP_Whitespace PP_Nullable_Target)? PP_New_Line
1519+
;
1520+
fragment PP_Nullable_Action
1521+
: 'disable'
1522+
| 'enable'
1523+
| 'restore'
1524+
;
1525+
fragment PP_Nullable_Target
1526+
: 'warnings'
1527+
| 'annotations'
1528+
;
1529+
```
1530+
1531+
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:
1532+
1533+
- `#nullable disable`: Sets both nullable contexts to “disabled”
1534+
- `#nullable enable`: Sets both nullable contexts to “enabled”
1535+
- `#nullable restore`: Restores both nullable contexts to the states specified by the external mechanism, if any
1536+
- `#nullable disable annotations`: Sets the nullable annotation context to “disabled”
1537+
- `#nullable enable annotations`: Sets the nullable annotation context to “enabled”
1538+
- `#nullable restore annotations`: Restores the nullable annotation context to the state specified by the external mechanism, if any
1539+
- `#nullable disable warnings`: Sets the nullable warning context to “disabled”
1540+
- `#nullable enable warnings`: Sets the nullable warning context to “enabled”
1541+
- `#nullable restore warnings`: Restores the nullable warning context to the state specified by the external mechanism, if any
1542+
1543+
Disabling a nullable context that is already disabled has no effect. Likewise, enabling a nullable context that is already enabled has no effect.
1544+
15091545
### 6.5.9 Pragma directives
15101546
15111547
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
</PropertyGroup>

0 commit comments

Comments
 (0)