Bug 8387 - Mono MCS doesn't allow implicit double to enum conversion, but MS CSC does
Summary: Mono MCS doesn't allow implicit double to enum conversion, but MS CSC does
Status: RESOLVED FEATURE
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2012-11-13 19:18 UTC by Brett van Swelm
Modified: 2012-11-14 02:48 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 FEATURE

Description Brett van Swelm 2012-11-13 19:18:36 UTC
Mono MCS doesn't allow implicit double to enum conversion, but MS CSC does.

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:

===== ConvertDouble =====
using System;

public class Outer {
    public Outer(Inner inner) {}
    public enum Inner { ONE, TWO }
}

public class ConvertDouble {
    public static void Main(String[] args) {
        new Outer(0.0);
    }
}
===== end =====

Section "6.1.3 Implicit enumeration conversions" of the C# language
specification states "An implicit enumeration conversion permits the
decimal-integer-literal 0 to be converted to any enum-type", which seems to
exclude implicit conversions to enums from floating point numbers. It seems
like MCS is correct and CSC is too permissive, however if compatibility
with CSC is a goal this may be worth fixing.

$ csc ConvertDouble.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.

$ mcs ConvertDouble.cs
ConvertDouble.cs(10,9): error CS1502: The best overloaded method match for
`Outer.Outer(Outer.Inner)' has some invalid arguments
ConvertDouble.cs(4,12): (Location of the symbol related to previous error)
ConvertDouble.cs(10,19): error CS1503: Argument `#1' cannot convert `double'
expression to type `Outer.Inner'
Compilation failed: 2 error(s), 0 warnings

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 Brett van Swelm 2012-11-13 19:22:59 UTC
Some more examples of differences (accepted by CSC but not MCS):

    new Outer(0.0f); // "float" literal
    new Outer(0.0m); // "decimal" literal
    new Outer(0.0e47); // "double" using scientific notation
    new Outer(0.0 / 42.0);
Comment 2 Marek Safar 2012-11-14 02:48:59 UTC
This is one of the cases where we are standard compatible but not csc compatible. Please fix your code to work with mcs