-
Notifications
You must be signed in to change notification settings - Fork 87
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
Nullability: conversion behavior #1242
Open
jnm2
wants to merge
4
commits into
draft-v8
Choose a base branch
from
nullability/conversions
base: draft-v8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -859,6 +859,8 @@ | |
|
||
**The remainder of this subclause is conditionally normative.** | ||
|
||
#### 8.9.5.1 Flow analysis | ||
|
||
A compiler that generates diagnostic warnings conforms to these rules. | ||
|
||
Every expression has one of three ***null state***s: | ||
|
@@ -925,10 +927,10 @@ | |
> int length = p.Length; // Warning: p is maybe null | ||
> | ||
> string s = p; // No warning. p is not null | ||
> | ||
> | ||
> if (s != null) | ||
> { | ||
> int l2 = s.Length; // No warning. s is not null | ||
> int l2 = s.Length; // No warning. s is not null | ||
> } | ||
> int l3 = s.Length; // Warning. s is maybe null | ||
> } | ||
|
@@ -946,13 +948,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 | ||
> } | ||
|
@@ -998,13 +1000,13 @@ | |
> { | ||
> get | ||
> { | ||
> string tmp = _field; | ||
> _field = null; | ||
> return tmp; | ||
> string tmp = _field; | ||
> _field = null; | ||
> return tmp; | ||
> } | ||
> set | ||
> { | ||
> _field = value; | ||
> _field = value; | ||
> } | ||
> } | ||
> | ||
|
@@ -1012,7 +1014,7 @@ | |
> { | ||
> var t = new Test(); | ||
> if (t.DisappearingProperty != null) | ||
> { | ||
> int len = t.DisappearingProperty.Length; // No warning. A compiler can assume property is stateful | ||
> } | ||
> } | ||
|
@@ -1031,7 +1033,7 @@ | |
> public class C | ||
> { | ||
> private C? child; | ||
> | ||
> | ||
> public void M() | ||
> { | ||
> _ = child.child.child; // Warning. Dereference possible null value | ||
|
@@ -1042,4 +1044,78 @@ | |
> | ||
> *end example* | ||
|
||
#### 8.9.5.2 Type conversions | ||
|
||
For the purpose of determining whether a conversion is *permitted*, a compiler must consider every nullable-annotated type to be equivalent to its unannotated version. A compiler may issue warnings if the annotations of the types are not compatible. | ||
|
||
(examples: `List<string>` to `IEnumerable<object?>`, or `List<string?>?` to `IEnumerable<object>`, ...) | ||
|
||
A compiler may follow rules for interface variance ([§18.2.3.3](interfaces.md#18233-variance-conversion)), delegate variance ([§20.4](delegates.md#204-delegate-compatibility)), and array covariance ([§1.7.6](arrays.md#176-array-covariance)) in determining whether to issue a warning for type conversions. | ||
(Do we need to list each type here? E.g. tuple types...) | ||
|
||
> <!-- Example: {template:"code-in-class-lib", name:"NullVariance"} --> | ||
> ```csharp | ||
> #nullable enable | ||
> public class C | ||
> { | ||
> public void M1(IEnumerable<string> p) | ||
> { | ||
> IEnumerable<string?> v1 = p; // No warning | ||
> } | ||
> | ||
> public void M2(IEnumerable<string?> p) | ||
> { | ||
> IEnumerable<string> v1 = p; // Warning | ||
> IEnumerable<string> v2 = p!; // No warning | ||
> } | ||
> | ||
> public void M3(Action<string?> p) | ||
> { | ||
> Action<string> v1 = p; // No warning | ||
> } | ||
> | ||
> public void M4(Action<string> p) | ||
> { | ||
> Action<string?> v1 = p; // Warning | ||
> Action<string?> v2 = p!; // No warning | ||
> } | ||
> | ||
> public void M5(string[] p) | ||
> { | ||
> string?[] v1 = p; // No warning | ||
> } | ||
Comment on lines
+1083
to
+1086
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. https://github.com/dotnet/csharplang/blob/main/meetings/2017/LDM-2017-10-04.md#array-covariance We allow |
||
> | ||
> public void M6(string?[] p) | ||
> { | ||
> string[] v1 = p; // Warning | ||
> string[] v2 = p!; // No warning | ||
> } | ||
> } | ||
> ``` | ||
> | ||
> *end example* | ||
|
||
A compiler may issue a warning when nullability differs in either direction in types which do not permit a variant conversion. | ||
|
||
> <!-- Example: {template:"code-in-class-lib", name:"NullInvariance"} --> | ||
> ```csharp | ||
> #nullable enable | ||
> public class C | ||
> { | ||
> public void M1(List<string> p) | ||
> { | ||
> List<string?> v1 = p; // Warning | ||
> List<string?> v1 = p!; // No Warning | ||
> } | ||
> | ||
> public void M2(List<string?> p) | ||
> { | ||
> List<string> v1 = p; // Warning | ||
> List<string> v1 = p!; // No Warning | ||
> } | ||
> } | ||
> ``` | ||
> | ||
> *end example* | ||
|
||
***End of conditionally normative text*** |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@Nigel-Ecma When aiming to only talk about the bare minimum, we're only thinking about the analysis that produces warnings. But for correctness (is this program allowed or not), do the following concerns already fall out from an existing place in the spec?
IEnumerable<string>
toIEnumerable<string?>
orList<string>
toIEnumerable<object?>
)IXyz<string>
andIXyz<string?>
on the same typeThere 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.
In other words, do we speak on whether (and where)
Xyz
andXyz?
are the same type or different types?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.
Hopefully, I will not be so rash as to say yes as TG2 are but fallible humans 😉
Not withstanding the use of the nomenclature of “nullable reference type” and ”non-nullable reference type” there is in fact only one kind of reference type in C#. The statement in §8.9.1 “There is no semantic difference between a non-nullable reference type and its corresponding nullable type, both can either be a reference to an object or
null
”, along with the definition of?
as an annotation, is one place (there may be others) this is intended to be conveyed.A key feature of this design is that if every nullable annotation, null-forgiving operator, nullable analysis related pragma and attribute is erased in a C# program then the result is semantically identical to the original (and should be compiled to the same executable).
Following §8.9.1 (and maybe elsewhere) this shouldn’t need to be stated, but an informative note might be worthwhile if there isn’t one already.
Similarly, as you can’t have two implementations of the same type that this is an error shouldn’t need to be stated – but an informative note might be worthwhile.
The Standard does, but I suspect like most things it could be improved.