dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.95k stars 4.02k forks source link

Roslyn does not learn from deconstruction methods' annotations when they are called implicitly #36985

Open TessenR opened 5 years ago

TessenR commented 5 years ago

Version Used:

Branch master (21 Jun 2019)
Latest commit 898bed by Heejae Chang:
added NFW to get some data on incremental parsing bug where source si� (#36620)

* added NFW to get some data on incremental parsing bug where source size and tree size is different

* more comments

Steps to Reproduce:

Compile the following code:

#nullable enable
using System.Diagnostics.CodeAnalysis;

public class C
{
    public void M1(C? c)
    {
      var (x, y) = c;
      c.ToString();
    }

    public void M2(C? c)
    {
      Ext.Deconstruct(c, out int x, out int y);
      c.ToString();
    }
}

static class Ext
{
  public static extern void Deconstruct([NotNull] this C? c, out int x, out int y);
}

namespace System.Diagnostics.CodeAnalysis
{
    public sealed class NotNullAttribute : Attribute
    {
    }
}

Expected Behavior: Both methods have the same set of warnings because they are equivalent.

Actual Behavior: The first method has a warning CS8602: Dereference of a possibly null reference. for c.ToString(). The second method doesn't.

It looks like Roslyn does not learn from the [NotNull] annotation when deconstruct method is called implicitly.

jcouv commented 5 years ago

This is a bug indeed