Skip to content

Different reference types: nullable and non-nullable - #1124

Merged
BillWagner merged 33 commits into
dotnet:draft-v8from
BillWagner:nullable-type-definitions
Oct 9, 2024
Merged

BillWagner merged 33 commits into
dotnet:draft-v8from
BillWagner:nullable-type-definitions

Conversation

@BillWagner

@BillWagner BillWagner commented May 29, 2024 •

Copy link
Copy Markdown
Member

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.

Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated

@jskeet jskeet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More comments, but I think we're making progress :)

Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
@BillWagner
BillWagner force-pushed the nullable-type-definitions branch from 9cdf304 to 53d837a Compare May 31, 2024 13:55

@BillWagner BillWagner left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments on updates per feedback.

Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated

@jskeet jskeet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting there :) Sorry to keep picking nits, but I'm definitely keen on getting this one over the line rather than the alternative that has null-oblivious.

Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated

@Nigel-Ecma Nigel-Ecma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I like this approach better than #1105, but do see some issues.

Comment thread standard/types.md
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md
Comment thread standard/types.md
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated

@Nigel-Ecma Nigel-Ecma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few language changes, some comments to consider.

As with #1123 I am recommending we don’t merge this, we get it to a state we’re currently happy with, move on to others in the group, circle back around as needed, and then merge/reject the group as a whole.

Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
@gafter

gafter commented Jul 10, 2024 •

Copy link
Copy Markdown
Member

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) {}

@BillWagner

Copy link
Copy Markdown
Member Author

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

@jskeet

jskeet commented Jul 10, 2024

Copy link
Copy Markdown
Contributor

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.

@BillWagner

BillWagner commented Jul 10, 2024 •

Copy link
Copy Markdown
Member Author
#nullable enable
string s = M();

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

@BillWagner
BillWagner force-pushed the nullable-type-definitions branch from 7fb7b7e to 137a8d2 Compare July 11, 2024 20:30
@RexJaeschke

Copy link
Copy Markdown
Contributor

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

@BillWagner
BillWagner force-pushed the nullable-type-definitions branch from 82f075e to 06145dd Compare September 16, 2024 14:44
The first commit ports in the text from dotnet#700 substantially without change. The only editorial change is to move the clause on "Nullable directives" in lexical-structure before the clause on Pragma directives.
This will likely cause new failures, as I turned on nullable annotations and warnings in one of the templates.

Update code samples for the template in use to remove nullable warnings.

I did this as we plan to have nullable annotations on by default in this version of the spec.
At our September meeting, we decided that the standard should limit its normative text to the syntax required for nullable reference types, and a basic description explaining that the purpose of the annotations is to provide diagnostics.
@BillWagner
BillWagner force-pushed the nullable-type-definitions branch from 0e001e0 to 346a776 Compare September 18, 2024 12:06
This makes sense to add to this PR logically.
I think this may fix the left recursion error as well. Even if it doesn't, it's still correct.
State the nullable type for implicitly typed variables
Comment thread standard/types.md
Comment thread standard/expressions.md Outdated
Comment thread standard/expressions.md Outdated
Comment thread standard/statements.md Outdated
Comment thread standard/types.md
Comment thread standard/types.md
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated

@BillWagner BillWagner left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments during our meeting

Comment thread standard/expressions.md Outdated
Comment thread standard/expressions.md Outdated
Comment thread standard/types.md
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Comment thread standard/types.md Outdated
Co-authored-by: Jon Skeet <skeet@pobox.com>

@jskeet jskeet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Big step forward :)

Additional edits from comments at the Oct 2 meeting. This resolves all open concerns.
@BillWagner

Copy link
Copy Markdown
Member Author

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

@Nigel-Ecma Nigel-Ecma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a missing word and a one word change suggestion.

Comment thread standard/statements.md Outdated
Comment thread standard/types.md
Co-authored-by: Nigel-Ecma <perryresearch@zoot.net.nz>
@BillWagner
BillWagner merged commit 64cfcec into dotnet:draft-v8 Oct 9, 2024
@BillWagner
BillWagner deleted the nullable-type-definitions branch October 9, 2024 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

meeting: discuss This issue should be discussed at the next TC49-TG2 meeting

Projects

None yet

5 participants