From: Marek Safar Date: Wed, 4 Jun 2014 09:24:34 +0000 (+0200) Subject: [mcs] null coalescing operator cannot reduce constants X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=92e321f26f21b690d3fb965f6e0e70584f643711;p=mono.git [mcs] null coalescing operator cannot reduce constants --- diff --git a/mcs/errors/cs0133-6.cs b/mcs/errors/cs0133-6.cs new file mode 100644 index 00000000000..a523169cdbd --- /dev/null +++ b/mcs/errors/cs0133-6.cs @@ -0,0 +1,10 @@ +// CS0133: The expression being assigned to `o' must be constant +// Line: 8 + +class X +{ + void Foo () + { + const object o = "" ?? null; + } +} \ No newline at end of file diff --git a/mcs/errors/cs0133-7.cs b/mcs/errors/cs0133-7.cs new file mode 100644 index 00000000000..10d82d9fe5e --- /dev/null +++ b/mcs/errors/cs0133-7.cs @@ -0,0 +1,10 @@ +// CS0133: The expression being assigned to `o' must be constant +// Line: 8 + +class X +{ + void Foo () + { + const object o = null ?? ""; + } +} \ No newline at end of file diff --git a/mcs/mcs/nullable.cs b/mcs/mcs/nullable.cs index 5f1822d0070..a0ace60b4e7 100644 --- a/mcs/mcs/nullable.cs +++ b/mcs/mcs/nullable.cs @@ -1158,26 +1158,25 @@ namespace Mono.CSharp.Nullable // Constant lc = left as Constant; if (lc != null && !lc.IsDefaultValue) - return ReducedExpression.Create (lc, this); + return ReducedExpression.Create (lc, this, false); // // Reduce (left ?? null) to left OR (null-constant ?? right) to right // - if (right.IsNull || lc != null) - return ReducedExpression.Create (lc != null ? right : left, this); + if (right.IsNull || lc != null) { + // + // Special case null ?? null + // + if (right.IsNull && ltype == right.Type) + return null; + + return ReducedExpression.Create (lc != null ? right : left, this, false); + } right = Convert.ImplicitConversion (ec, right, ltype, loc); type = ltype; return this; } - - // - // Special case null ?? null - // - if (ltype == right.Type) { - type = ltype; - return this; - } } else { return null; } @@ -1190,7 +1189,7 @@ namespace Mono.CSharp.Nullable // Reduce (null ?? right) to right // if (left.IsNull) - return ReducedExpression.Create (right, this).Resolve (ec); + return ReducedExpression.Create (right, this, false).Resolve (ec); left = Convert.ImplicitConversion (ec, unwrap ?? left, rtype, loc); type = rtype;