[mcs] Check for initially unassigned struct instance variables of unassigned structs.
authorMarek Safar <marek.safar@gmail.com>
Tue, 21 Jan 2014 15:31:50 +0000 (16:31 +0100)
committerMarek Safar <marek.safar@gmail.com>
Tue, 21 Jan 2014 15:33:01 +0000 (16:33 +0100)
mcs/errors/cs0165-44.cs [new file with mode: 0644]
mcs/errors/known-issues-net_4_5
mcs/mcs/ecore.cs
mcs/tests/test-829.cs
mcs/tests/ver-il-net_4_5.xml

diff --git a/mcs/errors/cs0165-44.cs b/mcs/errors/cs0165-44.cs
new file mode 100644 (file)
index 0000000..d4b6988
--- /dev/null
@@ -0,0 +1,21 @@
+// CS0165: Use of unassigned local variable `x'
+// Line: 19
+
+struct S
+{
+       public object O;
+}
+
+class X
+{
+       public S s;
+}
+
+class C
+{
+       public static void Main ()
+       {
+               X x;
+               x.s.O = 2;
+       }
+}
\ No newline at end of file
index a59413c220f1fcdaeab75897d7b814491bbf71ab..1e07f7c667ee0b12c339540088bf027700452b7b 100644 (file)
@@ -17,8 +17,3 @@ cs0080.cs
 # Operators
 cs0457-2.cs
 cs0457.cs
-
-cs1060.cs NO ERROR
-cs1060-2.cs NO ERROR
-cs1060-3.cs NO ERROR
-cs1060-4.cs
index 36b2ee930d1aa79f48d54e6cbcda5cab2eb8ac51..3bad645c309f5b2fad893b8d33c69a1f40a7f893 100644 (file)
@@ -5814,25 +5814,36 @@ namespace Mono.CSharp {
                        }
 
                        var fe = InstanceExpression as FieldExpr;
-                       if (fe != null || lvalue_instance) {
-                               if (fe == null)
-                                       return;
+                       if (fe != null) {
+                               Expression instance;
 
-                               /*
-                               while (fe.InstanceExpression is FieldExpr) {
-                                       fe = (FieldExpr) fe.InstanceExpression;
-                                       if (!fe.Spec.DeclaringType.IsStruct)
-                                               continue;
+                               do {
+                                       instance = fe.InstanceExpression;
+                                       var fe_instance = instance as FieldExpr;
+                                       if ((fe_instance != null && !fe_instance.IsStatic) || instance is LocalVariableReference) {
+                                               if (TypeSpec.IsReferenceType (fe.Type) && instance.Type.IsStruct) {
+                                                       var var = InstanceExpression as IVariableReference;
+                                                       if (var != null && var.VariableInfo == null) {
+                                                               var var_inst = instance as IVariableReference;
+                                                               if (var_inst == null || (var_inst.VariableInfo != null && !fc.IsDefinitelyAssigned (var_inst.VariableInfo)))
+                                                                       fc.Report.Warning (1060, 1, fe.loc, "Use of possibly unassigned field `{0}'", fe.Name);
+                                                       }
+                                               }
 
-                                       if (fe.VariableInfo != null && fc.IsStructFieldDefinitelyAssigned (fe.VariableInfo, fe.Name)) {
-                                               fc.Report.Warning (1060, 1, fe.loc, "Use of possibly unassigned field `{0}'", fe.Name);
+                                               if (fe_instance != null) {
+                                                       fe = fe_instance;
+                                                       continue;
+                                               }
                                        }
-                               }
 
-                               fe.InstanceExpression.FlowAnalysis (fc);
-                               */
+                                       break;
+                               } while (true);
+
+                               if (instance != null && TypeSpec.IsReferenceType (instance.Type))
+                                       instance.FlowAnalysis (fc);
                        } else {
-                               InstanceExpression.FlowAnalysis (fc);
+                               if (TypeSpec.IsReferenceType (InstanceExpression.Type))
+                                       InstanceExpression.FlowAnalysis (fc);
                        }
                }
 
index 6a7e2c8a9c5cc6fa968b227c45511f5317a1d609..d5c83a033f9cbe9bbf5ddc45cc7d0b817684ddc5 100644 (file)
@@ -23,6 +23,18 @@ class C
                        s.F = 1.0f;
                }
        }
+
+       static void Test2 (bool b)
+       {
+               S s;
+               if (b) {
+                       s.s2 = new S2 ();
+                       s.F = 1.0f;
+               } else {
+                       s.s2.f1 = 2.1f;
+                       s.F = 1.0f;
+               }
+       }
        
        public static int Main ()
        {
index c76dd77f5422ad0fe06bafa44503e6c60d06f2f3..3e2ae6cfb5180fa70b7ce8e75e273b30b9485b14 100644 (file)
       <method name="Void .ctor()" attrs="6278">\r
         <size>7</size>\r
       </method>\r
+      <method name="Void Test2(Boolean)" attrs="145">\r
+        <size>74</size>\r
+      </method>\r
     </type>\r
   </test>\r
   <test name="test-83.cs">\r