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

Nullable Reference Types: Compiler should not report CS8603 when assignment exists in the right-hand side of a null coalescing operator #27009

Open
PathogenDavid opened this issue May 21, 2018 · 3 comments
Assignees
Labels
Area-Compilers New Language Feature - Nullable Reference Types Nullable Reference Types Test Test failures in roslyn-CI
Milestone

Comments

@PathogenDavid
Copy link
Contributor

PathogenDavid commented May 21, 2018

Version Used: 05/14/18 Nullable Reference Types Preview (csc reports 2.8.0.62830 (e595ee27)) with Visual Studio 15.7.1

Demonstration Code:

I've created PathogenPlayground/NullCoalescingOperatorCs8603 to demonstrate the issue.

Steps to Reproduce:

Compile the following code, where Foo is a class:

private static Foo? _foo = null;
public static Foo FooProperty
{
    get => _foo ?? (_foo = new Foo()); // warning CS8603: Possible null reference return.
}

public static Foo FooProperty2
{
    get
    {
        Foo? ret = null;
        return ret ?? (ret = new Foo()); // warning CS8603: Possible null reference return.
    }
}

Edit: The issue also happens when the null coalescing operator is not in play:

private static Foo? _foo4 = null;
public static Foo FooProperty4
{
    get
    {
        if (_foo4 == null)
        { _foo4 = new Foo(); }

        return _foo4; // warning CS8603: Possible null reference return.
    }
}

Expected Behavior:

No warnings are reported.

Actual Behavior:

Possible null reference return is reported for the return statement in each property.


The readme on PathogenPlayground/NullCoalescingOperatorCs8603 contains some additional details.

FooProperty2 exists to make sure the problem isn't somehow related to #9978

@PathogenDavid
Copy link
Contributor Author

I've also updated PathogenPlayground/NullCoalescingOperatorCs860 with non-static variants of the properties since I found out that the behavior differs for FooProperty4 when the property and field aren't static.

@cston I suspect that the FooProperty4 issue is more related to #23063 than the null-coalescing problems. Would you prefer I submit a separate issue for it?

@jcouv jcouv added this to the 16.0 milestone Jun 10, 2018
@jcouv
Copy link
Member

jcouv commented Jun 10, 2018

Confirmed.
I think this is indeed a bug. Thanks for reporting.

It seems caused by the field being static (issue tracked by #26651)

@jaredpar jaredpar added the Bug label Aug 30, 2018
@jcouv jcouv modified the milestones: 16.0, 16.0.P2 Nov 10, 2018
@jaredpar jaredpar self-assigned this Jan 26, 2019
@jaredpar jaredpar modified the milestones: 16.0, 16.1.P1 Jan 26, 2019
@jaredpar
Copy link
Member

Issue appears to be fixed now. Assigning to @jaredpar to add a regression test.

@jcouv jcouv added Test Test failures in roslyn-CI and removed Bug labels Mar 14, 2019
@gafter gafter modified the milestones: 16.1.P1, 16.1.P3 Apr 9, 2019
@jcouv jcouv modified the milestones: 16.1.P3, 16.1, 16.2 Apr 18, 2019
@jcouv jcouv modified the milestones: 16.2, 16.3 Jun 25, 2019
@jcouv jcouv modified the milestones: 16.3, Compiler.Next Jul 8, 2019
@jaredpar jaredpar removed this from the Compiler.Next milestone Sep 12, 2023
@jaredpar jaredpar added this to the Backlog milestone Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers New Language Feature - Nullable Reference Types Nullable Reference Types Test Test failures in roslyn-CI
Projects
None yet
Development

No branches or pull requests

4 participants