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

Integration of null coalescing assignment and nullable reference type #31555

Closed
Logerfo opened this issue Dec 5, 2018 · 2 comments
Closed

Integration of null coalescing assignment and nullable reference type #31555

Logerfo opened this issue Dec 5, 2018 · 2 comments

Comments

@Logerfo
Copy link
Contributor

Logerfo commented Dec 5, 2018

Context

Currently, both null coalescing assignment and nullable reference types are available together in Visual Studio 19 Preview 1. However, I don't think they work together very well.

Code

#nullable enable
namespace A {
   public class C {
      public void M() {
         C? c = null;
         c ??= new C();
         c.M();
      }
   }
}

Expected behavior

The following code emits the same IL as the code above and has no warnings:

#nullable enable
namespace A {
   public class C {
      public void M() {
         C? c = null;
         if (c == null)
            c = new C();

         c.M();
      }
   }
}

For consistency reasons, the expected behavior for the stated code should also be zero warnings.

Atual behavior

Warning CS8602 Possible dereference of a null reference.

@Logerfo
Copy link
Contributor Author

Logerfo commented Dec 6, 2018

Found this listed on #22152, seems like it's already planned for preview 2.

@jaredpar
Copy link
Member

Thanks for reporting. This is fixed now and covered sufficiently by NullCoalescingAssignment_UseInExpression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants