Skip to content

Nullability should be inferred from deconstruction #29618

@cston

Description

@cston
using System.Runtime.CompilerServices;
[NonNullTypes]
class C
{
    static (string?, string) F()
    {
        return (string.Empty, string.Empty);
    }
    void Deconstruct(out string? x, out string y)
    {
        x = string.Empty;
        y = string.Empty;
    }
    static void M()
    {
        (var x, var y) = new C();
        x.ToString(); // warning: maybe null
        y.ToString();
        (var z, var w) = F();
        z.ToString(); // warning: maybe null
        w.ToString();
    }
}

(jcouv update:)
Note: I've added references to this issue to replace PROTOTYPE markers. I believe that once the deconstruction scenario is properly handled, it should be possible to remove the following two methods:

        // PROTOTYPE(NullableReferenceTypes): Remove this method. Initial binding should not infer nullability.
        internal static TypeSymbolWithAnnotations GetTypeAndNullability(this BoundExpression expr)
        {
...
        }

        // PROTOTYPE(NullableReferenceTypes): Remove this method. Initial binding should not infer nullability.
        private static bool? IsNullableInternal(this BoundExpression expr)
        {
...
      
        }

As part of this, we should also review NullableWalker.VisitForEachIterationVariables.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions