This project is read-only.

Roslyn doesn't respect additional static implicit operators on decimal in Script#


This is a cross-post of this Connect issue:

Script# defines implicit conversions between Number and decimal. The Roslyn compiler in preview complains that decimal is not a valid Number.

See the attached project for repro.


I did some digging and found that this is caused by an optimization that ignores user defined implicit conversions only on decimal (UserDefinedConversions.cs):
// The decimal type does not contribute its user-defined conversions to the mix; though its 
            // conversions are actually implemented via user-defined operators, we logically treat it as 
            // though those conversions were built-in.  

            if (type.IsClassType() || type.IsStructType() && type.SpecialType != SpecialType.System_Decimal)
                var namedType = (NamedTypeSymbol)type;
                if (!excludeExisting || !HasIdentityConversionToAny(namedType, result))
This seems like an odd optimize but since it is limited to ignoring only the user-defined conversions on decimal, the workaround is to cast the decimal to a float and then the implicit conversion to Number is properly picked up.

file attachments


pgavlin wrote Jan 14, 2015 at 8:14 PM

This was fixed with commit d24e504.