I recently played with the C # compiler on TryRoslyn, and I ran into a strange problem when checking for inequality turned into more than one. Here is the replay code:
using System; public class C { public void M() { if (Foo() != 0 || Foo() != 0) { Console.WriteLine("Hi!"); } } private int Foo() => 0; }
and here is the code generated by the decompiler:
using System; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Security.Permissions; [assembly: AssemblyVersion("0.0.0.0")] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)] [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [module: UnverifiableCode] public class C { public void M() { bool flag = this.Foo() != 0 || this.Foo() > 0;
Here is a link to play. Why is Roslyn doing this; this is mistake?
Some observations that I made after playing with the code for a while:
This only happens with the last boolean expression in the condition. For example, if you add another operator || , this will happen only with the last call to Foo() .
This also happens with only 0; if you replace it with 1 or some other number, this will not happen.
James ko
source share