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 + dynamic #32364

Closed
vsfeedback opened this issue Jan 10, 2019 · 4 comments
Closed

Nullable + dynamic #32364

vsfeedback opened this issue Jan 10, 2019 · 4 comments
Labels
Area-Compilers Area-Language Design Developer Community The issue was originally reported on https://developercommunity.visualstudio.com New Language Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design
Milestone

Comments

@vsfeedback
Copy link

vsfeedback commented Jan 10, 2019

If there is defined class A which overloads + operator such that it returns always null then it is possible to write program throwing ArgumentNullException but has no warnings by dynamics.

This issue has been moved from https://developercommunity.visualstudio.com/content/problem/401974/nullable-dynamic.html
VSTS ticketId: 743594

These are the original issue comments:

libor.drozdek on 12/7/2018, 01:58 AM (34 days ago):

I have problems with formatting when I use "insert Code" so I will post the code here:

#nullable enable 
class Program
{
    private static void Main()
    {
        A obj = new A();
        var x = f(obj); 
        // Null reference exception but no warning
        x.ToString();
    }
    
    public static object f(dynamic a)
    {
        if (a == null)
            throw new System.Exception();

        return a + a;
    }
    
    public class A
    {
        public static A? operator +(A fst, A snd) => null;
    }
}

These are the original issue solutions:
(no solutions)


[jcouv update:] I referenced this issue in code. Please find those references before closing this issue.

@jcouv
Copy link
Member

jcouv commented Jan 17, 2019

There's a number of questions around dynamic and nullability analysis. We'll have to confirm with LDM, but my sense is that once you enter the world of dynamic you exit the world of static analysis, including static nullability analysis.
We can track top-level nullability, but nothing about members/methods.

A few scenarios that need discussion:

dynamic d = something;
int y = d.x.y; // ok
d.x = null; // ok
d.M(); // ok
d.M!(); // useless

d = null;
d.M(); // warn
int x = d.x; // warn

var z = d.y!; // unnecessary

@jaredpar
Copy link
Member

This behavior appears to be "By Design" today. The return type of an expression that has dynamic input is dynamic (non-null). Hence compiler is acting correctly here by not issuing a warning on the dereference.

@jcouv jcouv reopened this Jan 27, 2019
@jcouv
Copy link
Member

jcouv commented Jan 27, 2019

I'm re-opening this issue as I've used this to track questions relative to dynamic and nullable. (see updated I made to OP)

@gafter
Copy link
Member

gafter commented Apr 4, 2019

This behavior was confirmed by the LDM on 2019-04-03.

@gafter gafter added the Resolution-By Design The behavior reported in the issue matches the current design label Apr 4, 2019
@jaredpar jaredpar closed this as completed Apr 5, 2019
@sharwell sharwell added the Developer Community The issue was originally reported on https://developercommunity.visualstudio.com label Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Area-Language Design Developer Community The issue was originally reported on https://developercommunity.visualstudio.com New Language Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

5 participants