-
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
base: draft-v8
Are you sure you want to change the base?
Conversation
> public void M5(string[] p) | ||
> { | ||
> string?[] v1 = p; // No warning | ||
> } |
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.
Yes. https://github.com/dotnet/csharplang/blob/main/meetings/2017/LDM-2017-10-04.md#array-covariance
We allow string[]
to be assigned to object[]
, and so we also allow string[]
to be assigned to string?[]
.
@@ -1042,4 +1044,78 @@ A compiler may use any expression that dereferences a variable, property, or eve | |||
> | |||
> *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. |
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?
- Allowing conversions (not just top-level differences in nullability, but nested ones like
IEnumerable<string>
toIEnumerable<string?>
orList<string>
toIEnumerable<object?>
) - Not allowing implementation of both
IXyz<string>
andIXyz<string?>
on the same type
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.
In other words, do we speak on whether (and where) Xyz
and Xyz?
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.
@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?
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).
- Allowing conversions (not just top-level differences in nullability, but nested ones like
IEnumerable<string>
toIEnumerable<string?>
orList<string>
toIEnumerable<object?>
)
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.
- Not allowing implementation of both
IXyz<string>
andIXyz<string?>
on the same type
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.
In other words, do we speak on whether (and where)
Xyz
andXyz?
are the same type or different types?
The Standard does, but I suspect like most things it could be improved.
Maybe we jinxed it after all?
This is my first PR to the repo. Guidance is very helpful. Think of the lines of this PR as a series of questions: how far to go, whether it's worth going here, how do we do things?