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

Flow analysis failure on "obj is Type == false" #31469

Closed
fiinix00 opened this issue Nov 30, 2018 · 4 comments
Closed

Flow analysis failure on "obj is Type == false" #31469

fiinix00 opened this issue Nov 30, 2018 · 4 comments

Comments

@fiinix00
Copy link

Version Used:

Multiple tries

  1. Roslyn 2.0 (VS2017) (https://dotnetfiddle.net)

  2. dotnet --version = 3.0.100-preview-009795
    Build Engine version 16.0.225-preview+g5ebeba52a1 for .NET Core

  3. Branch master (28 Oct 2018)
    https://sharplab.io/#v2:EYLgtghgzgLgpgJwD4AEAMACFBGA3AWACgiUBmLAJgwEEiBvIjJrcnANiwBYMAhAVwDmACgCUjZg0LNpGAPbAAVnADGMOYowBeDACIAEnAA2h2ToLEpMpgEsAZkPkKM1qFmyZYCLdtsRDUODFLK0krKxwATiEdAHkNFwwAO1k1HDQdEXMwjABfcSsjAPyZUOzmSKEAElj41zStDDpPHIzcDAB6dowAUQRZLwBhAGU0bDYAVhAMAFUAuVsMPkToKGsBRLgAEwwTZT8MADcIBGsIYEM4DAByTyvi6Tzg3KIcoA

Steps to Reproduce:

Compile of this code

using System;

public class A
{
    public static void Bug()
    {
        object obj = "Hello";

        if(obj is string str == false)
        {
            Console.WriteLine("Obj is not string");
        }
        else
        {
            Console.WriteLine($"Obj is string = {str}"); // Eror CS0165: Use of unassigned local variable 'str'
        }
    }
}

Expected Behavior:

Code should compile, and print "Obj is string = Hello";

Actual Behavior:

Compiler failure, flow analysis fails

@alrz
Copy link
Contributor

alrz commented Nov 30, 2018

I believe this is by design. Probably it's best to move this to dotnet/csharplang.

@fiinix00
Copy link
Author

Apparently there is a discussion on how to fix this kind of syntaxical chaos:
dotnet/csharplang#882

The odd thing is that this compiles correcly and 'str' is defined in the else scope instead.

if(!(obj is string str)) 
{ 
    str.Method(); //Use of unassigned local variable 'str' 
}
else 
{
    str.Method(); //Ok
}

Language reference
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is

  • States that: "If expr is true and is is used with an if statement, varname is assigned and has local scope within the if statement only."
  • Yet the example above goes against that

Stack overflow also has a interesting discussion on it.
Seems that more have come along this odd behavior:
https://stackoverflow.com/questions/47242221/c-sharp-7-compiler-error-pattern-matching

But yes, you are somewhat correct, it is a Roslyn yet a dotnet/csharplang problem at the same time.

  • I would not say it is by design(?) as my counter example sais otherwise

@gafter
Copy link
Member

gafter commented Nov 30, 2018

The language-specified definite assignment rules for ! make the latter example work, but there are no special language-specified definite assignment rules for == false that would make the original example work as desired. Roslyn implements the language specification, and therefore this is by design.

@gafter
Copy link
Member

gafter commented Nov 30, 2018

Issue moved to dotnet/csharplang #2035 via ZenHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants