csharpstandard icon indicating copy to clipboard operation
csharpstandard copied to clipboard

Different reference types: nullable and non-nullable

Open BillWagner opened this issue 1 year ago • 5 comments

Fixes #1089 Fixes #1090

This is an alternative to #1105

The changes were done on top of the branch for #1105. The final commit has the updates to remove null oblivious types.

It removes the text for a "null-oblivious" type. Instead, it relies on the nullable context for how to interpret the nullability of a reference type T.

BillWagner avatar May 29 '24 20:05 BillWagner

We may need three types, not two. For example:

void f(
#nullable disable
string x, // could x (oblivious) be seen as nullable type but non-nullable initial state?
#nullable enable
string y,
string? z)
{
 if (...) {
   // x state is not null? or oblivious?
   g1(ref x); // okay
   g1(ref y); // not okay
   g1(ref z); // okay
   // x state is possibly null
 } else {
   // x state is not null? or oblivious?
   g2(ref x); // okay
   g2(ref y); // okay
   g2(ref z); // not okay
   // x state is possibly null
 }
}
void g1(ref string? x) {}
void g2(ref string x) {}

gafter avatar Jul 10 '24 21:07 gafter

Very short version:

f("x", "y", "z");
void f(
#nullable disable
    string x,
#nullable enable
    string y,
    string? z)
{
    y = x; // no warning
    y = z; // warning. Assignment of null to non-nullable variable.
}

BillWagner avatar Jul 10 '24 21:07 BillWagner

Example of why I believe it makes sense to view a variable of type T declared in a nullable-disabled context as a nullable type:

# nullable disable
string x;
# nullable enable
x = "some value";
x = null;

This does not issue a warning, even for the last line. The state of x just before the last line is surely "definitely not null" as we've assigned a string literal to it... so to be able to assign null to it means its type can't be a not-null type.

jskeet avatar Jul 10 '24 21:07 jskeet

#nullable enable
string s = M();

#nullable disable
// What is the type of the return value?
string M() => null;

BillWagner avatar Jul 10 '24 21:07 BillWagner

Note that from 2024-8-20 through 2024-09-03, there was a lengthy thread on email re this topic, under the subject "ECMA TC49-TG2 agenda for September 4th."

RexJaeschke avatar Sep 05 '24 13:09 RexJaeschke

I've addressed all open comments, and resolved all conversations. This is ready for a final (asynchronous) pass and approval.

@jskeet @Nigel-Ecma @MadsTorgersen @ericlippert @RexJaeschke @gafter

BillWagner avatar Oct 07 '24 21:10 BillWagner