-
Notifications
You must be signed in to change notification settings - Fork 93
Allow arrays of nullable reference types #1386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: draft-v8
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,16 +54,13 @@ | |
; | ||
|
||
array_type | ||
: non_array_type rank_specifier+ | ||
: array_type nullable_type_annotation rank_specifier+ | ||
| non_array_type rank_specifier+ | ||
Comment on lines
+57
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, no MLR, well done ;-) This change impacts §17.2 which now needs to be revised. |
||
; | ||
|
||
non_array_type | ||
: value_type | ||
| class_type | ||
| interface_type | ||
| delegate_type | ||
| 'dynamic' | ||
| type_parameter | ||
| (class_type | interface_type | delegate_type | 'dynamic' | type_parameter) nullable_type_annotation? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes the issue where
jnm2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pointer_type // unsafe code support | ||
; | ||
|
||
|
@@ -732,7 +729,25 @@ | |
|
||
> *Note:* The types `R` and `R?` are represented by the same underlying type, `R`. A variable of that underlying type can either contain a reference to an object or be the value `null`, which indicates “no reference.” *end note* | ||
|
||
The syntactic distinction between a *nullable reference type* and its corresponding *non-nullable reference type* enables a compiler to generate diagnostics. A compiler must allow the *nullable_type_annotation* as defined in [§8.2.1](types.md#821-general). The diagnostics must be limited to warnings. Neither the presence or absence of nullable annotations, nor the state of the nullable context can change the compile time or runtime behavior of a program except for changes in any diagnostic messages generated at compile time. | ||
The syntactic distinction between a *nullable reference type* and its corresponding *non-nullable reference type* enables a compiler to generate diagnostics. A compiler must allow the *nullable_type_annotation* as defined in [§8.2.1](types.md#821-general). The diagnostics must be limited to warnings. Neither the presence or absence of nullable annotations, nor the state of the nullable context can change the compile time or runtime behavior of a program except for changes in any diagnostic messages generated at compile time, with one exception: | ||
|
||
A compiler must respect the effect that *nullable_type_annotation* has on the ordering of array rank specifiers. Whereas `A[][,]` is a single *array_type* with two *rank_specifier*s, the presence of a nullable annotation between the rank specifiers in `A[]?[,]` causes it to no longer be a single *array_type*, but rather two: an outer *array_type* with a single *rank_specifier* of `[,]`, and an element type of `A[]?` which is a *nullable_reference_type* containing an inner *array_type* with a single *rank_specifier* of `[]`. Because of this, `A[]?[,]` and `A[,][]` are represented by the same underlying type, while `A[]?[,]` and `A[][,]` are not. | ||
Comment on lines
+740
to
+742
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
> *Example*: The array ranks are interrupted by the '?' in the parameter type, changing the meaning of the underlying array type: | ||
> | ||
> <!-- Example: {template:"code-in-class-lib", name:"ArraysOfNullAbleArrays"} --> | ||
> ```csharp | ||
> #nullable enable | ||
> class C | ||
> { | ||
> void M(string[][,]?[,,][,,,] arrays) | ||
> { | ||
> string? value = arrays[3, 3, 3][4, 4, 4, 4]?[1][2, 2]; | ||
> } | ||
> } | ||
> ``` | ||
> | ||
> *end example* | ||
Comment on lines
+743
to
+758
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with the above this needs to be in §17 somewhere. Giving examples of how adding nullable annotations to examples already in §17 could help the understanding, e.g. In §17.2.1 there is:
In these terms what is An example demonstrating the differences, in the same terms as above, between |
||
|
||
### 8.9.2 Non-nullable reference types | ||
|
||
|
@@ -946,13 +961,13 @@ | |
> public void M(string s) | ||
> { | ||
> int length = s.Length; // No warning. s is not null | ||
> | ||
> _ = s == null; // Null check by testing equality. The null state of s is maybe null | ||
> length = s.Length; // Warning, and changes the null state of s to not null | ||
> | ||
> _ = s?.Length; // The ?. is a null check and changes the null state of s to maybe null | ||
> if (s.Length > 4) // Warning. Changes null state of s to not null | ||
> { | ||
> _ = s?[4]; // ?[] is a null check and changes the null state of s to maybe null | ||
> _ = s.Length; // Warning. s is maybe null | ||
> } | ||
|
@@ -1012,7 +1027,7 @@ | |
> { | ||
> var t = new Test(); | ||
> if (t.DisappearingProperty != null) | ||
> { | ||
> int len = t.DisappearingProperty.Length; // No warning. A compiler can assume property is stateful | ||
> } | ||
> } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has the property that when parsing
string?[][,]?[,,][,,,]?
we end up with twoarray_type
nodes: one beingstring?[][,]?[,,][,,,]
, and one being the innerstring?[][,]
.@Nigel-Ecma It's not mutual left recursion! 😁