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

List-patterns: symbols not correctly updated with nullability information #57884

Open
jcouv opened this issue Nov 19, 2021 · 2 comments
Open

Comments

@jcouv
Copy link
Member

jcouv commented Nov 19, 2021

This will likely be fixed as side-effect of pulling Receiver out of IndexerAccess (issue #57855)

        [CompilerTrait(CompilerFeature.IOperation)]
        [Fact]
        public void TestIsPatternExpression_ListPatterns_Collection_Nullability()
        {
            string source = @"
#nullable enable
class X<T>
{
    static X<U> Create<U>(U u) => throw null!;
    void M(string s)
    {
        s = null;
        var a = Create(s);
        _ = /*<bind>*/a is [var item]/*</bind>*/;
    }
    public int Length => throw null!;
    public char this[int i] => throw null!;
}
";
            // The LengthSymbol and IndexerSymbol should reflect updated nullability:
            // LengthSymbol: System.Int32 X<System.String?>.Length { get; }, IndexerSymbol: System.Char X<System.String?>.this[System.Int32 i] { get; })
            string expectedOperationTree = @"
IIsPatternOperation (OperationKind.IsPattern, Type: System.Boolean) (Syntax: 'a is [var item]')
  Value:
    ILocalReferenceOperation: a (OperationKind.LocalReference, Type: X<System.String?>) (Syntax: 'a')
  Pattern:
    IListPatternOperation (OperationKind.ListPattern, Type: null) (Syntax: '[var item]') (InputType: X<System.String>, NarrowedType: X<System.String>, DeclaredSymbol: null, LengthSymbol: System.Int32 X<System.String>.Length { get; }, IndexerSymbol: System.Char X<System.String>.this[System.Int32 i] { get; })
      Patterns (1):
          IDeclarationPatternOperation (OperationKind.DeclarationPattern, Type: null) (Syntax: 'var item') (InputType: System.Char, NarrowedType: System.Char, DeclaredSymbol: System.Char item, MatchesNull: True)
";
            var expectedDiagnostics = new[]
            {
                // (8,13): warning CS8600: Converting null literal or possible null value to non-nullable type.
                //         s = null;
                Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(8, 13)
            };

            var comp = CreateCompilationWithIndexAndRange(new[] { source, TestSources.GetSubArray });
            VerifyOperationTreeAndDiagnosticsForTest<IsPatternExpressionSyntax>(comp, expectedOperationTree, expectedDiagnostics);
        }
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Nov 19, 2021
@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Nov 19, 2021
@jcouv jcouv added this to the 17.1 milestone Nov 19, 2021
@AlekseyTs
Copy link
Contributor

It looks like this issue is not specific to list patterns. For example, nullability of symbols for property sub-patterns isn't updated as well:

        [CompilerTrait(CompilerFeature.IOperation)]
        [Fact]
        public void Test()
        {
            string source = @"
#nullable enable
class X<T>
{
    static X<U> Create<U>(U u) => throw null!;
    void M(string s)
    {
        s = null;
        var a = Create(s);
        _ = /*<bind>*/a is { Length: 1 }/*</bind>*/;
    }
    public int Length => throw null!;
    public char this[int i] => throw null!;
}
";
            string expectedOperationTree = @"
IIsPatternOperation (OperationKind.IsPattern, Type: System.Boolean) (Syntax: 'a is { Length: 1 }')
  Value:
    ILocalReferenceOperation: a (OperationKind.LocalReference, Type: X<System.String?>) (Syntax: 'a')
  Pattern:
    IRecursivePatternOperation (OperationKind.RecursivePattern, Type: null) (Syntax: '{ Length: 1 }') (InputType: X<System.String>, NarrowedType: X<System.String>, DeclaredSymbol: null, MatchedType: X<System.String>, DeconstructSymbol: null)
      DeconstructionSubpatterns (0)
      PropertySubpatterns (1):
          IPropertySubpatternOperation (OperationKind.PropertySubpattern, Type: null) (Syntax: 'Length: 1')
            Member:
              IPropertyReferenceOperation: System.Int32 X<System.String>.Length { get; } (OperationKind.PropertyReference, Type: System.Int32) (Syntax: 'Length')
                Instance Receiver:
                  IInstanceReferenceOperation (ReferenceKind: PatternInput) (OperationKind.InstanceReference, Type: X<System.String>, IsImplicit) (Syntax: 'Length')
            Pattern:
              IConstantPatternOperation (OperationKind.ConstantPattern, Type: null) (Syntax: '1') (InputType: System.Int32, NarrowedType: System.Int32)
                Value:
                  ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 1) (Syntax: '1')
";
            var expectedDiagnostics = new[]
            {
                // (8,13): warning CS8600: Converting null literal or possible null value to non-nullable type.
                //         s = null;
                Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(8, 13)
            };

            var comp = CreateCompilation(source);
            VerifyOperationTreeAndDiagnosticsForTest<IsPatternExpressionSyntax>(comp, expectedOperationTree, expectedDiagnostics);
        }

@AlekseyTs
Copy link
Contributor

CC @333fred

@jcouv jcouv removed their assignment Jan 20, 2022
@AlekseyTs AlekseyTs modified the milestones: 17.1, C# 11.0 Feb 18, 2022
@jaredpar jaredpar modified the milestones: C# 11.0, 17.4 Jun 24, 2022
@jaredpar jaredpar modified the milestones: 17.4, 17.5 Oct 7, 2022
@jaredpar jaredpar modified the milestones: 17.5, 17.6 Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

3 participants