roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

C# Compiler should interpret negated is operator correctly

Open agaace opened this issue 6 years ago • 4 comments

The is operator can be negated in an intuitive way but it causes behavior opposite of intended, confusing to say the least - it's a Schrodinger's expression: my variable both is and isn't null. Was this really a design "feature" (WHY??) or is this a bug?

Type o = typeof(string);
bool x = o is null;
bool y = o !is null;
// No compiler error; x and y are equal.. WHY??

Version Used: Whatever's shipped in VS 2019 Community 16.5.4. Code tested in a .NET Core 3.1 project. Also confirmed in Roslyn master branch (march 9, 2020), per SharpLab.

Steps to Reproduce:

using System;
public class C {
    public static void Main() {
        Type o = typeof(string);
        Console.WriteLine(o is null ? "y" : "n");
        Console.WriteLine(o !is null ? "y" : "n");
        Console.WriteLine(!(o is null) ? "y" : "n");
    }
}

Here's a functioning snippet: https://sharplab.io/#v2:EYLgtghgzgLgpgJwD4AEBMBGAsAKBQZgAJ1CBhQgb10JuLTUutuYBUBPABzkIHtCBeQjE5weAMwAUKDAAYAlAG4mzGgHpV0gJwSAhACU4YxHAB2AYzgBRAI4BXCABsoEngBpCJ2w4dzCAfkIAIjZAwhAgk0DFZRV1LQkJAHlgACs4Mxg5Ph1BT29/IJCwiKilHBVaOIxtbNyvBwLg0PDAyOjyiuJql0IASygPesailrayzq6awh1+wfyApuLW0pjmeJ0e2byfYeaS9uYAX1xDoA=

WHY??

Expected Behavior:

Type o = typeof(string);
Console.WriteLine(o is null ? "y" : "n");    // This prints "n"
Console.WriteLine(o !is null ? "y" : "n");   // This either fails to compile or prints "y"
Console.WriteLine(!(o is null) ? "y" : "n"); // This prints "y"

Actual Behavior:

Type o = typeof(string);
Console.WriteLine(o is null ? "y" : "n");    // This prints "n"
Console.WriteLine(o !is null ? "y" : "n");   // This prints "n" - --> WHY??
Console.WriteLine(!(o is null) ? "y" : "n"); // This prints "y"

agaace avatar May 04 '20 08:05 agaace

Closing out. This is by design. There is no "negated is operator". When you write: bool y = o !is null; what you're writing is (o!) is null. ! is the null warning suppression operator which has no effect at runtime at all. So (o!) is null is equivalent to (o) is null which is o is null.

CyrusNajmabadi avatar May 04 '20 09:05 CyrusNajmabadi

Wow, this design is so unintuitive and confusing and I haven't been able to find ANY documentation on this behavior at all. I think it at least warrants:

  1. mentioning this case in the is operator documentation here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is,
  2. a compiler warning.

Also, a negation operator would be super handy, even implemented as pure syntactic sugar as in: x isnot null simply compiling to !(x is null) because the brackets in the negative case pretty much defeat the purpose of using this syntactic sugar anyway. At this point, I much prefer !ReferenceEquals(x, null) syntactically to !(x is null) - it's easier to read.

agaace avatar May 05 '20 05:05 agaace

mentioning this case in the is operator documentation here:

Sure. File a request on that page to have it updated.

a compiler warning.

that would be a breaking change and cannot be taken. You can write your own analyzer to catch this if you want though.

Wow, this design is so unintuitive and confusing and I haven't been able to find ANY documentation on this behavior at all.

In general such things aren't really documented... each is operating exactly as intended. o! is well documented as to what it means, and how it has no runtime impact. ... is ... is also well documented. Both are combining exactly as specified. There is no documentation on this because there is no !is operator. Those were just two tokens that happened to be next to each other that you thought had some new meaning.

CyrusNajmabadi avatar May 05 '20 05:05 CyrusNajmabadi

Also, a negation operator would be super handy, even implemented as pure syntactic sugar as in: x isnot null

That is coming with the next version of C# patterns. you can say x is not null. in this case, null is a pattern not P is a negation of a pattern, and x is P is testing x against that pattern.

CyrusNajmabadi avatar May 05 '20 05:05 CyrusNajmabadi