Bug 15392 - MCS shortcutting of "false" and "true" causes bad code to be accepted (and other undesirable side effects)
Summary: MCS shortcutting of "false" and "true" causes bad code to be accepted (and ot...
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-10-14 17:54 UTC by Brett van Swelm
Modified: 2013-11-27 10:13 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Brett van Swelm 2013-10-14 17:54:15 UTC
With "D:\Applications\Mono-2.11.4\bin" and
"C:\Windows\Microsoft.NET\Framework\v4.0.30319" in the path on my Windows 7
machine:

===== Test.cs =====
public class Test
{
    static void Main()
    {
        if(false && bar()) {

        }
    }
}
===== end =====

$ mcs Test.cs
Test.cs(5,12): warning CS0429: Unreachable expression code detected
Test.cs(5,28): warning CS0162: Unreachable code detected
Compilation succeeded - 2 warning(s)

$ csc Test.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.17929
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

Test.cs(5,21): error CS0103: The name 'bar' does not exist in the current context

Ideally this code would not compile with MCS.

This actually causes more subtle problems with overloaded operators. Consider this example:

===== D.cs =====
using System;

class D
{
    public string x;
    public static D operator &(D x, D y)
    {
        Console.WriteLine("and");
        return y;
    }
    public static implicit operator D(bool b)
    {
        Console.WriteLine("implicit");
        return new D("from implicit");
    }
    public static bool operator true(D d)
    {
        Console.WriteLine("true");
        return true;
    }
    public static bool operator false(D d)
    {
        Console.WriteLine("false");
        return false;
    }
    public D(string x)
    {
        this.x = x;
        Console.WriteLine("ctor " + x);
    }
    public override string ToString() { return x; }
    static void Main()
    {
        D d = false && new D("from main");
        Console.WriteLine(d);
    }
}
===== end =====

$ mcs D.cs
D.cs(34,15): warning CS0429: Unreachable expression code detected
Compilation succeeded - 1 warning(s)

$ ./D.exe 
implicit
ctor from implicit
from implicit

$ csc D.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.17929
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

$ ./D.exe 
implicit
ctor from implicit
false
ctor from main
and
from main

Thanks,

Brett van Swelm | Senior Engineer
Coverity | 185 Berry Street | Suite 6500, Lobby 3 | San Francisco, CA 94107
The Leader in Development Testing
Read our profile in Forbes, Coverity Gets Code Right 25% Faster
Comment 1 Marek Safar 2013-11-27 10:13:20 UTC
Fixed in master