Bugzilla – Bug 318661
[PATCH] CS0165: incorrect flow branching on member access to an unassigned class instance
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by gert.driesen@pandora.be 2005-08-05 00:50:40 MST ---- To reproduce this issue, compile the following code snippet: using System.Collections; public class EntryPoint { public static void Main() { ArrayList fields = new ArrayList(); Field fb; for (int i = 0; i < fields.Count; i++) { if (((Field) fields[i]).Name == "abc") { fb = (Field) fields[i]; break; } } if (fb.Name != "b") { } } public class Field { public string Name; } } Actual result: Successful compilation. Expected result: test.cs(14,7): error CS0165: Use of unassigned local variable 'fb' This breaks the mono build on win32, when compiling mbas: Creating ../build/deps/mbas.exe.response ... csc.exe /lib:../class/lib/default /r:System.dll /r:../class/lib/default/Mo no.Get Options.dll /r:../class/lib/default/Microsoft.VisualBasic.dll /nologo /opt imize -d:NET_1_1 -d:ONLY_1_1 /debug+ /debug:full -target:exe -out:mbas.exe mb- parser.c s @../build/deps/mbas.exe.response decl.cs(295,4): warning CS0168: The variable 'exp' is declared but never used expression.cs(3342,24): error CS0165: Use of unassigned local variable 'fb' expression.cs(3375,10): error CS0165: Use of unassigned local variable 'fb' expression.cs(3406,10): error CS0165: Use of unassigned local variable 'fb' make[1]: *** [mbas.exe] Error 1 make[1]: Leaving directory `/cygdrive/d/projects/mono/head/mcs/mbas' ---- Additional Comments From atsushi@ximian.com 2005-08-22 15:42:20 MST ---- similar case: ---- using System.Collections; public class EntryPoint { public static void Main() { ArrayList fields = new ArrayList(); Field fb; while (fields.Count > 0) { fb = (Field) fields[0]; } if (fb.Name != "b") { System.Console.WriteLine ("shouldn't compile here."); } } public class Field { public string Name; } } There are many possibilities to change code condition. Note that mcs/errors/cs0165*.cs are correctly rejected. ---- Additional Comments From atsushi@ximian.com 2005-08-22 18:14:46 MST ---- Created an attachment (id=168316) proposed fix ---- Additional Comments From miguel@ximian.com 2005-08-22 18:35:00 MST ---- Martin, can you review/approve? ---- Additional Comments From martin@ximian.com 2005-08-23 09:13:42 MST ---- The patch looks fine, does this pass all the tests (make bootstrap-world) ? If it does, please commit. ---- Additional Comments From atsushi@ximian.com 2005-08-23 10:27:32 MST ---- mhm, it passes all mcs tests/errors tests but broke corlib build. Needs further fix. ---- Additional Comments From atsushi@ximian.com 2005-08-23 11:50:48 MST ---- Created an attachment (id=168317) fixity fix ---- Additional Comments From atsushi@ximian.com 2005-08-23 11:56:16 MST ---- ok, am committing the patch. There is only one difference between the first and second patches. @@ -6875,7 +6875,7 @@ if (!ResolveBase (ec)) return null; - if ((variable_info != null) && !variable_info.IsAssigned (ec)) { + if ((variable_info != null) && !(type.IsValueType && ec.OmitStructFlowAnalysis) && !variable_info.IsAssigned (ec)) { Error (188, "The `this' object cannot be used before all of its fields are assigned to"); variable_info.SetAssigned (ec); return this; ---- Additional Comments From atsushi@ximian.com 2005-08-23 12:06:40 MST ---- fixed as r48739. Imported an attachment (id=168316) Imported an attachment (id=168317) Unknown operating system unknown. Setting to default OS "Other".