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

Nullable analysis fails to understand complicated is (pattern) expression #31881

Closed
pdelvo opened this issue Dec 17, 2018 · 0 comments
Closed
Assignees
Labels
Area-Compilers Bug Feature - Nullable Reference Types Nullable Reference Types Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Milestone

Comments

@pdelvo
Copy link
Contributor

pdelvo commented Dec 17, 2018

Version Used: Latest supported by SharpLap and VS 2019 Preview 1.1

Steps to Reproduce:

using System;
#nullable enable

public class C
{
    public object? AProperty { get; set; }
    public void M(C? input)
    {
    	if (input?.AProperty is string str)
        {
            Console.WriteLine(str.ToString());
            
            // The compiler produces a "warning CS8602: Possible dereference of a null reference." for all the following three lines:
            Console.WriteLine(input?.AProperty.ToString());
            Console.WriteLine(input.AProperty.ToString());
            Console.WriteLine(input.ToString());
        }
    }
}

https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQQCYEsYHsEDGuAdmFgOapbGpEC2ADlgDYQIA0MIAbm1mAE92GEAGoAPgAEADAAJJARgDcAWABQAYmJxmzKMFayIxfa3XrJAZnkAmWQGF1Ab3UBIK7NzAAVhAIwAflkAQQAFBFwGNhgBWSdZcggYJVlkJJSAX3VZHPlrSQAWWQBZAAp7IOoGOBgASmzclzVc2Vd+WVKqmoCAOjCIqIQY2SxkeQU5ZBgEeuaWxob51sUATlKphB6AFVwAZWnqclLa2tU5pfcFNa7AvvDI6IFtvYPiI5OzpZzL6+JqmDuA0ez32CEOx1Oi3mP06fxqINe70h53mWTUrjRGSAA==

Note that changing the is pattern to a normal is expression does not make a difference.
Expected Behavior:
No warning is produced.
Actual Behavior:
The compiler emits three CS8602 warnings in the code.

Because of the if statement we know that input?.AProperty is not null. This implies that both input and input.AProperty are not null. So none on these statements should produce a warning.

@gafter gafter self-assigned this Jan 30, 2019
@gafter gafter added this to the 16.1 milestone Jan 30, 2019
gafter added a commit to gafter/roslyn that referenced this issue Mar 19, 2019
gafter pushed a commit to gafter/roslyn that referenced this issue Mar 19, 2019
gafter pushed a commit to gafter/roslyn that referenced this issue Mar 19, 2019
jaredpar pushed a commit that referenced this issue Mar 30, 2019
* Implement pattern-matching in the nullable walker

Fixes #29909
Fixes #31881
Fixes #30952
Fixes #33499
Fixes #30597
Fixes #32414
Fixes #23944

* Remove infinite recursion by using an empty struct cache.

* Changes per code review comments.

* Remove debugging code accidentally left behind.

* Analysis of patterns-matching in the nullable walker requires valid (>0) slots.

* Skip a flaky test

* Patch after merge.

* Make ctor private to force use of factory methods

* Correct a typo.

* Fixup after merge.
@sharwell sharwell added the Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented label Mar 30, 2019
@sharwell sharwell modified the milestones: 16.1, 16.1.P2 Mar 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Feature - Nullable Reference Types Nullable Reference Types Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
None yet
Development

No branches or pull requests

3 participants