Skip to content

Null-Coalescing Changes in V8 #737

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

Closed
RexJaeschke opened this issue Feb 15, 2023 · 2 comments · Fixed by #1323
Closed

Null-Coalescing Changes in V8 #737

RexJaeschke opened this issue Feb 15, 2023 · 2 comments · Fixed by #1323
Assignees
Labels
meeting: priority Review before meeting. Merge, merge with issues, or reject at the next TC49-TC2 meeting
Milestone

Comments

@RexJaeschke
Copy link
Contributor

@BillWagner

This issue interacts with PR #609, “Add support for Null-coalescing assignment”.

Background

  1. Historically, compound-assignment operators have been defined in terms of their non-assignment counterparts. For example, in §11.19.3, “Compound assignment,” the spec says, “An operation of the form x «op»= y is processed by applying binary operator overload resolution (§11.4.5) as if the operation was written x «op» y.” and then goes on to talk about assignment specifics.
  2. The V4 standard introduced the null-coalescing operator, ??11.14 in V7); however, support for ??= was not included.
  3. V8 adds support for ??=. Using the MS spec for this feature, Rex created Draft PR 609.
  4. V8 also has an empty placeholder MS spec for allowing unconstrained type parameters with ?? and ??=. Here’s a simple example:
private static void Mx<T>(T a, T b)
{
    Console.WriteLine(a ?? b);
    Console.WriteLine(a ??= b);
}

Current Situation

More than a year after he wrote the PR 609 text, Rex recognized that PR and the unconstrained type parameters topic are related, and he is now trying to figure out what the spec should be for allowing unconstrained type parameters. His current thinking is:

  1. This feature addresses both ?? and ??=.
  2. Rather than treating the addition of ??= and supporting unconstrained type parameters as separate features, it seems appropriate to combine them into one PR.
  3. The new text re the handling of the unconstrained type parameters should go in the ?? operator section, and only if that impacts ??= should it be mentioned in the compound-assignment section as well.
  4. In PR 609, the main edits for ??= went into the “Assignment operators” sections; there were no changes to the ?? operator section itself. In hindsight, we should review the two sections to make sure they work together, and preserve the general notion that the compound-assignment operators be defined in terms of their non-assignment counterparts.
  5. As to the specific changes to support unconstrained type parameters, it seems this would (mostly?) involve the addition of a new, first, step to the bullet list in §11.14, “The null coalescing operator.”

Bottom line

Rex needs someone to

  1. Write the words for the new step to support unconstrained type parameters, and to see if they need to overflow into the compound-assignment section.
  2. Review the edits in PR 609 and reconcile them with these new words; that is, to unify the two sets of edits.
@RexJaeschke RexJaeschke added this to the C# 8.0 milestone Feb 15, 2023
@Nigel-Ecma
Copy link
Contributor

Nigel-Ecma commented Feb 24, 2023

@RexJaeschke

Based on some testing using unconstrained type parameters with ??:

  • Both arguments must be typed with the same unconstrained type parameter, say T
  • If T at runtime is a reference or nullable value type then the semantics follow ?? with reference or nullable value type arguments respectively
  • If T at runtime is a non-nullable value type then a ?? b is a
  • T cannot be unmanaged at runtime as that requires a constraint...

Can you wordsmith from here?

@KalleOlaviNiemitalo
Copy link
Contributor

Make sure it allows

public class C {
    public void M<T1, T2>(T1 a, ref T2 b) where T1 : T2 {
        b ??= a;
    }
}

@BillWagner BillWagner self-assigned this Apr 30, 2025
@BillWagner BillWagner added the meeting: priority Review before meeting. Merge, merge with issues, or reject at the next TC49-TC2 meeting label Apr 30, 2025
BillWagner added a commit to BillWagner/csharpstandard that referenced this issue May 7, 2025
Fixes dotnet#737

Add the language to the null-coalescing operator so that `??` can be used with an unconstrained type parameter.
BillWagner added a commit that referenced this issue May 14, 2025
* Unconstrainted type parameters and `??`

Fixes #737

Add the language to the null-coalescing operator so that `??` can be used with an unconstrained type parameter.

* Revert "Unconstrainted type parameters and `??`"

This reverts commit a958a37.

* Do this the easy way.

I think this can be done the easy way. After reverting the original change, this commit removes the phrase "or a reference type" from the first bullet. By doing that, an unconstrained type parameter `T` is allowed. It's not a reference type.

I tested the case Nigel and Kalle described for using `??` with an unconstrained type parameter, and the existing rules produce the behavior they described.

I ran a few other cases to validate, and those worked correctly as well.

* Update standard/expressions.md

Co-authored-by: Nigel-Ecma <[email protected]>

* add exclusion on unmanaged types

Respond to Nigel's question

* respond to feedback

Change "a non-nullable value type" to "known to be a non-nullable value type".

Add an example to illustrate that `a ?? b` is `a` when the type of `a` is a non-nullable value type used as a type argument to an unconstrained type parameter.

* Update standard/expressions.md

Co-authored-by: Jon Skeet <[email protected]>

* extend the bulleted list.

* fix test harness issue

* Update standard/expressions.md

* Update standard/expressions.md

---------

Co-authored-by: Nigel-Ecma <[email protected]>
Co-authored-by: Jon Skeet <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meeting: priority Review before meeting. Merge, merge with issues, or reject at the next TC49-TC2 meeting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants