[mcs] null coalescing operator cannot reduce constants
authorMarek Safar <marek.safar@gmail.com>
Wed, 4 Jun 2014 09:24:34 +0000 (11:24 +0200)
committerMarek Safar <marek.safar@gmail.com>
Wed, 4 Jun 2014 09:25:24 +0000 (11:25 +0200)
mcs/errors/cs0133-6.cs [new file with mode: 0644]
mcs/errors/cs0133-7.cs [new file with mode: 0644]
mcs/mcs/nullable.cs

diff --git a/mcs/errors/cs0133-6.cs b/mcs/errors/cs0133-6.cs
new file mode 100644 (file)
index 0000000..a523169
--- /dev/null
@@ -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 (file)
index 0000000..10d82d9
--- /dev/null
@@ -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
index 5f1822d00701b8a259f8e1746e5b4ae4954d470d..a0ace60b4e77d16bcebe7042c6d229efa411db15 100644 (file)
@@ -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;