Skip to content
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
wants to merge 4 commits into
base: draft-v8
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Nits
  • Loading branch information
jnm2 committed Jan 1, 2025
commit 5ff046a20c3b4a37b05e4856c95401427824d568
6 changes: 3 additions & 3 deletions standard/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -948,13 +948,13 @@
> public void M(string s)
> {
> int length = s.Length; // No warning. s is not null
>

Check warning on line 951 in standard/types.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/types.md#L951

MDC032::Line length 87 > maximum 81
> _ = 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
>

Check warning on line 954 in standard/types.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/types.md#L954

MDC032::Line length 90 > maximum 81
> _ = 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
> {

Check warning on line 957 in standard/types.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/types.md#L957

MDC032::Line length 87 > maximum 81
> _ = s?[4]; // ?[] is a null check and changes the null state of s to maybe null
> _ = s.Length; // Warning. s is maybe null
> }
Expand Down Expand Up @@ -1014,7 +1014,7 @@
> {
> var t = new Test();
> if (t.DisappearingProperty != null)
> {

Check warning on line 1017 in standard/types.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/types.md#L1017

MDC032::Line length 110 > maximum 81
> int len = t.DisappearingProperty.Length; // No warning. A compiler can assume property is stateful
> }
> }
Expand Down Expand Up @@ -1046,12 +1046,12 @@

#### 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. The compiler may however issue warnings if the annotations of the types are not compatible.
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.
Copy link
Contributor Author

@jnm2 jnm2 Jan 3, 2025

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> to IEnumerable<string?> or List<string> to IEnumerable<object?>)
  • Not allowing implementation of both IXyz<string> and IXyz<string?> on the same type

Copy link
Contributor Author

@jnm2 jnm2 Jan 3, 2025

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?

Copy link
Contributor

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> to IEnumerable<string?> or List<string> to IEnumerable<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> and IXyz<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 and Xyz? are the same type or different types?

The Standard does, but I suspect like most things it could be improved.


(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.
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.

Check failure on line 1053 in standard/types.md

View workflow job for this annotation

GitHub Actions / TOC and Anchor updater

standard/types.md#L1053

TOC002::`§1.7.6` not found
(Do we need to list each type here? E.g. tuple types...)

Check warning on line 1054 in standard/types.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/types.md#L1053-L1054

MDC019::Mismatch: link anchor is '§1.7.6', should be '§17.6'

> <!-- Example: {template:"code-in-class-lib", name:"NullVariance"} -->
> ```csharp
Expand Down Expand Up @@ -1095,7 +1095,7 @@
>
> *end example*

Types that are invariant likewise may produce a warning when the conversion differs only in the nullability of type arguments.
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
Expand Down
Loading