{
try {
return TryReduceConstant (ec, target_type);
- }
- catch (OverflowException) {
+ } catch (OverflowException) {
if (ec.ConstantCheckState && Type.BuiltinType != BuiltinTypeSpec.Type.Decimal) {
ec.Report.Error (221, loc,
"Constant value `{0}' cannot be converted to a `{1}' (use `unchecked' syntax to override)",
Constant TryReduceConstant (ResolveContext ec, TypeSpec target_type)
{
- if (Type == target_type)
+ if (Type == target_type) {
+ //
+ // Reducing literal value produces a new constant. Syntactically 10 is not same as (int)10
+ //
+ if (IsLiteral)
+ return CreateConstantFromValue (target_type, GetValue (), loc);
+
return this;
+ }
Constant c;
if (target_type.IsEnum) {
eclass = ExprClass.Value;
Constant c = expr as Constant;
- Expression res = c != null ? c.TryReduce (ec, type) : null;
-
- if (res == null) {
- res = Convert.ExplicitConversion (ec, expr, type, loc);
+ if (c != null) {
+ c = c.TryReduce (ec, type);
+ if (c != null)
+ return c;
}
+ var res = Convert.ExplicitConversion (ec, expr, type, loc);
if (res == expr)
return EmptyCast.Create (res, type);