Bug 30267

Summary: Constant value 'invalid decimal constant' cannot be converted to a 'double'
Product: [Mono] Compilers Reporter: Jon Douglas [MSFT] <jon.douglas>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+monodroid, mono-bugs+mono, udhams
Priority: Normal    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:

Description Jon Douglas [MSFT] 2015-05-20 12:49:24 UTC
*Description

When converting the decimal.MinValue to a double with a simple cast, the following error is given:

    Constant value 'invalid decimal constant' cannot be converted to a 'double'

Given the following example:

    var smallestDecimal = (double)decimal.MinValue;

This statement will fail and thus not allow us to compile. This gives me suspicions that (double)decimal.MinusOne; will also run into this problem. However we can compile just fine with the opposite:

    var largestDecimal = (double)decimal.MaxValue;

When compiling this code in a C# Console Project, there is no problem with these lines of code, this is only within Xamarin.Android(mscorlib) that has been discovered.

*Test Case

            var largestDecimal = (double)decimal.MaxValue; // works 7.92281625142643E+28
            var smallestDecimal = (double)decimal.MinValue; // fails
            var minusOneDecimal = (double)decimal.MinusOne; //fails with same error message

*Build Output

1>C:\Users\Jon\Downloads\DecimalMinValueBugShowCase\DecimalMinValueBugShowCase\AndroidBugProject\MainActivity.cs(38,35,38,59): error CS0031: Constant value 'invalid decimal constant' cannot be converted to a 'double'
1>  The command exited with code 1. (TaskId:78)
1>Done executing task "Csc" -- FAILED. (TaskId:78)

*Workaround

One could set the variable manually like the following:

var smallestDecimal = (double)-79228162514264337593543950335m;

Which yields the expected result of:

-7.92281625142643E+28

*Version Information

Microsoft Visual Studio Ultimate 2013
Version 12.0.31101.00 Update 4
Microsoft .NET Framework
Version 4.6.00057

Installed Version: Ultimate

Architecture and Modeling Tools   06181-004-0451031-02320
Microsoft Architecture and Modeling Tools
    
UML® and Unified Modeling Language™ are trademarks or registered trademarks of the Object Management Group, Inc. in the United States and other countries.

LightSwitch for Visual Studio 2013   06181-004-0451031-02320
Microsoft LightSwitch for Visual Studio 2013

Team Explorer for Visual Studio 2013   06181-004-0451031-02320
Microsoft Team Explorer for Visual Studio 2013

Visual Basic 2013   06181-004-0451031-02320
Microsoft Visual Basic 2013

Visual C# 2013   06181-004-0451031-02320
Microsoft Visual C# 2013

Visual C++ 2013   06181-004-0451031-02320
Microsoft Visual C++ 2013

Visual F# 2013   06181-004-0451031-02320
Microsoft Visual F# 2013

Visual Studio 2013 Code Analysis Spell Checker   06181-004-0451031-02320
Microsoft® Visual Studio® 2013 Code Analysis Spell Checker

Portions of International CorrectSpell™ spelling correction system © 1993 by Lernout & Hauspie Speech Products N.V. All rights reserved.

The American Heritage® Dictionary of the English Language, Third Edition Copyright © 1992 Houghton Mifflin Company. Electronic version licensed from Lernout & Hauspie Speech Products N.V. All rights reserved.

Windows Phone SDK 8.0 - ENU   06181-004-0451031-02320
Windows Phone SDK 8.0 - ENU

ApiPortVS   1.0
API portability analyzer.

ASP.NET and Web Tools   12.4.51016.0
Microsoft Web Developer Tools contains the following components:
Support for creating and opening ASP.NET web projects
Browser Link: A communication channel between Visual Studio and browsers
Editor extensions for HTML, CSS, and JavaScript
Page Inspector: Inspection tool for ASP.NET web projects
Scaffolding: A framework for building and running code generators
Server Explorer extensions for Microsoft Azure Websites
Web publishing: Extensions for publishing ASP.NET web projects to hosting providers, on-premises servers, or Microsoft Azure

ASP.NET Web Frameworks and Tools 2012.2   4.1.21001.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.2.21010.0
For additional information, visit http://www.asp.net/

Color Theme Designer   1.0
Designer for creating new color themes

Common Azure Tools   1.3
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Entity Framework Power Tools   1.0
Adds useful design-time DbContext features to the Visual Studio Solution Explorer context menu. 

When right-clicking on a file containing a derived DbContext class, the following context menu functions are supported:

1) View Entity Data Model - Displays the underlying Code First model in the Entity Framework designer.
2) View Entity Data Model XML - Displays the EDMX XML representing the underlying Code First model.
3) Generate Views - Generates pre-compiled views used by the EF runtime to improve start-up performance. Adds the generated views file to the containing project.

File Nesting   1.0
Automatically nest files based on file name and enables developers to nest and unnest any file manually

JetBrains ReSharper 8.2   Full Edition build 8.2.0.2160 on 2014-03-21T01:53:08
JetBrains ReSharper 8.2 package for Microsoft Visual Studio. For more information about ReSharper, visit http://www.jetbrains.com/resharper/. Copyright © 2003–2015 JetBrains, Inc.

Microsoft Advertising SDK for Windows Phone   
Microsoft Advertising SDK for Windows Phone
Build 

Microsoft Azure Mobile Services Tools   1.3
Microsoft Azure Mobile Services Tools

Microsoft XNA Game Studio   4.0
Microsoft XNA Game Studio 4.0
Build 4.0.30901.0

NuGet Package Manager   2.8.50926.663
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PowerShell Tools   1.3
Provides file classification services using PowerShell

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

Release Management for Visual Studio Package   1.0
Release Management for Visual Studio

SAPReferenceManager   1.0
Manages References to Shared Projects

SQL Server Data Tools   12.0.41012.0
Microsoft SQL Server Data Tools

Windows Azure Tools   2.3
Windows Azure Tools for Microsoft Visual Studio 2013 - v2.3.20320.1602

Windows Phone 8.1 SDK Integration   1.0
This package integrates the tools for the Windows Phone 8.1 SDK into the menus and controls of Visual Studio.

Xamarin   3.11.446.0 (db2a71b)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   5.1.1.0 (3518c4ceb984ffea1f5d972ea9c09eb27738d565)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.Forms Intellisense   1.0
Provides intellisense for Xamarin.Forms in the XML editor.

Xamarin.iOS   8.10.0.0 (7741cc495ab0baf04ff0405d0604bc27f0ecae2e)
Visual Studio extension to enable development for Xamarin.iOS.

Xamarin.iOS Unified Migration   1.0
Automated migration for Xamarin iOS Classic projects to Unified

Xamarin.TestCloud.Integration   1.0
Early preview of Xamarin Test Cloud integration

XNA Game Studio 4.0   1.0
Information about my package
Comment 1 Jonathan Pryor 2015-05-20 17:42:05 UTC
@Jon.Douglas: What compiler are they using? Error CS0031 is a *C# Compiler Error*, not a runtime error.

I'm not able to repro this on OS X with mcs.
Comment 2 Jonathan Pryor 2015-05-21 14:01:47 UTC
This is arguably a CSC bug.

I'll punt to @msafar on whose "fault" it is.

The problem: .NET CSC doesn't like Mono mcs-generated output when it comes to values containing decimal.MinValue. (And since Xamarin.Android's mscorlib.dll is built with mcs, therein lies the problem.)

Consider these two files, dmv-lib.cs and dmv-app.cs:

  // dmv-lib.cs
  using System;

  namespace MyLib {
    public class A {
      public const decimal MinDecimal = -79228162514264337593543950335m;
    }
  }

  // dmv-app.cs
  using System;

  class App {
    public static void Main ()
    {
      Console.WriteLine ("MinValue={0}", MyLib.A.MinDecimal);
      var a = (double) MyLib.A.MinDecimal;
    }
  }

If we stay purely within compilers, things work:

  # OS X
  $ mcs /t:library /out:dmv-lib-mcs.dll dmv-lib.cs
  $ mcs /out:dmv-mcs.exe dmv-app.cs -r:dmv-lib-mcs.dll

  # Windows
  > csc /t:library /out:dmv-lib-csc.dll dmv-lib.cs
  > csc /out:dmv-csc.exe dmv-app.cs -r:dmv-lib-csc.dll

Use the mcs-generated library with CSC, and then things break:

  > csc /out:dmv-csc.exe dmv.cs -r:dmv-lib-mcs.dll /nologo
  dmv.cs(8,15): error CS0031: Constant value 'invalid decimal constant'
  cannot be converted to a 'double'

Why? Because the [DecimalConstantAttribute] values differ.

mcs:

>   .class public auto ansi beforefieldinit A
>   	extends [mscorlib]System.Object
>   {
>     .field  public static initonly  valuetype [mscorlib]System.Decimal MinDecimal
>     .custom instance void class [mscorlib]System.Runtime.CompilerServices.DecimalConstantAttribute::'.ctor'(unsigned int8, unsigned int8, unsigned int32, unsigned int32, unsigned int32) =  (
> 		01 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF   // ................
> 		00 00                                           ) // ..

CSC:

>   .class public auto ansi beforefieldinit A
>   	extends [mscorlib]System.Object
>   {
>     .field  public static initonly  valuetype [mscorlib]System.Decimal MinDecimal
>     .custom instance void class [mscorlib]System.Runtime.CompilerServices.DecimalConstantAttribute::'.ctor'(unsigned int8, unsigned int8, unsigned int32, unsigned int32, unsigned int32) =  (
> 		01 00 00 80 FF FF FF FF FF FF FF FF FF FF FF FF   // ................
> 		00 00                                           ) // ..

Notice that the 4th byte differs: mcs emits 0xFF while CSC emits 0x80.

This does NOT appear to be a runtime issue: dmv-mcs.exe can be run on .NET and produces correct output:

  > .\dmv-mcs.exe
  MinValue=-79228162514264337593543950335

It's "just" CSC's reading of the DecimalConstantAttribute value that is "wrong."

I'm inclined to say that this is a CSC bug -- otherwise why the mcs-generated binary run correctly on .NET? -- but that may be irrelevant. If we can't use csc to compile, we have a problem.
Comment 3 Marek Safar 2015-05-22 14:12:36 UTC
Fixed in master