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

Roslyn doesn't handle inferred nullability in the presence of goto. #39220

Closed
TessenR opened this issue Oct 10, 2019 · 1 comment · Fixed by #39301
Closed

Roslyn doesn't handle inferred nullability in the presence of goto. #39220

TessenR opened this issue Oct 10, 2019 · 1 comment · Fixed by #39301
Assignees
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Bug New Language Feature - Nullable Reference Types Nullable Reference Types
Milestone

Comments

@TessenR
Copy link

TessenR commented Oct 10, 2019

Version Used:

Branch master (23 Sep 2019)
Latest commit 89b4f60 by Charles Stoner:
Allow conversion of collection initializer Add extension method this arg (#38732)

Steps to Reproduce:

Compile and run the following code:

#nullable enable
class C<T>
{
  public C(T t) => Property = t;
  public T Property { get; }
}

class Program
{
  static void Main()
  {
    Test("");
  }
  
  static void Test(string? s)
  {
    if (s == null) return;
    
    hell:
    var c = GetC(s);
    switch (c)
    {
      case { Property: var prop }:
        prop.ToString();
        break;
    }
    
    s = null;
    goto hell;
  }
  
  public static C<T> GetC<T>(T t) => new C<T>(t);
}

Expected Behavior:
warning CS8602: Dereference of a possibly null reference. reported for prop.ToString()

Actual Behavior:
No warnings are reported for the program above. The program crashes at runtime with a NullReferenceException

Notes
If you specify the type of c explicitly i.e. C<string?> c = GetC(s); you'll get the correct warning.

@gafter gafter changed the title Roslyn doesn't handle inferred nullability in pattern matching Roslyn doesn't handle inferred nullability in the presence of goto. Oct 14, 2019
@gafter
Copy link
Member

gafter commented Oct 14, 2019

This has nothing to do with pattern-matching. The following program also incorrectly reports no warnings:

#nullable enable
class C<T>
{
    public C(T t) => Property = t;
    public T Property { get; }
}

class Program
{
    static void Main()
    {
        Test("");
    }

    static void Test(string? s)
    {
        if (s == null) return;

        hell:
        var c = GetC(s);
        var prop = c.Property;
        prop.ToString();
        s = null;
        goto hell;
    }

    public static C<T> GetC<T>(T t) => new C<T>(t);
}

@gafter gafter self-assigned this Oct 14, 2019
@gafter gafter added this to the 16.4 milestone Oct 14, 2019
@agocke agocke modified the milestones: 16.4, 16.5 Nov 1, 2019
gafter pushed a commit to gafter/roslyn that referenced this issue Jan 2, 2020
Fixes dotnet#39220

Filter out "bad" slots not a member of the container.

Fixes dotnet#33428
@gafter gafter added 4 - In Review A fix for the issue is submitted for review. and removed 3 - Working labels Jan 13, 2020
@jcouv jcouv modified the milestones: 16.5, 16.6 Feb 14, 2020
gafter pushed a commit that referenced this issue Feb 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Bug New Language Feature - Nullable Reference Types Nullable Reference Types
Projects
None yet
4 participants