Bugzilla – Bug 316136
Compiler crash with static string a; string b = a + "x"
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by jhill@arcfocus.com 2004-09-29 00:04:52 MST ---- Description of Problem: The following test case will compile fine on csc, but mcs will fail with an InvalidCastException. If you remove either of the constructors, the compilation will succeed. Steps to reproduce the problem: 1. Create X.cs file with the following code: class X { static string a = "static string"; string b = a + "string"; X () {} X (int x) {} static void Main () {} } 2. Attempt to compile: mcs X.cs Actual Results: Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type. in <0x00070> Mono.CSharp.StringConcat:Emit (Mono.CSharp.EmitContext) in <0x000fa> Mono.CSharp.FieldExpr:EmitAssign (Mono.CSharp.EmitContext,Mono.CSharp.Expression,bool,bool) in <0x001c7> Mono.CSharp.Assign:Emit (Mono.CSharp.EmitContext,bool) in <0x00012> Mono.CSharp.Assign:EmitStatement (Mono.CSharp.EmitContext) in <0x0019b> Mono.CSharp.TypeContainer:EmitFieldInitializers (Mono.CSharp.EmitContext) in <0x00286> Mono.CSharp.Constructor:Emit () in <0x00344> Mono.CSharp.TypeContainer:EmitType () in <0x005a7> Mono.CSharp.RootContext:EmitCode () in <0x00b7f> Mono.CSharp.Driver:MainDriver (string[]) in <0x00012> Mono.CSharp.Driver:Main (string[]) Expected Results: Compilation succeeded How often does this happen? Always. Additional Information: When mcs fails, it will not output a file name or number, making this issue difficult to track in larger projects. ---- Additional Comments From jluke@cfl.rr.com 2004-09-30 15:38:56 MST ---- in expression.cs around line 3142, we are casting to Expression from a value in an ArrayList which is actually a Argument without checking the type. A similar thing also happens in a few other places in that file. I will look into fixing this, but so far I seem to end up emitting invalid IL. ---- Additional Comments From jluke@cfl.rr.com 2004-09-30 16:38:27 MST ---- Created an attachment (id=166854) patch ---- Additional Comments From jluke@cfl.rr.com 2004-09-30 16:40:54 MST ---- The above seems to fix it without breaking anything, but I am not sure it is correct. It just uses the as keyword to cast and only tries to convert it from Expression to Argument if it is not null (because it is already an Argument at least in this case, maybe always?). ---- Additional Comments From miguel@ximian.com 2004-11-02 17:26:52 MST ---- The problem is that the StringConcat expression is mutating the object during the Emit phase. And it happens that we invoke the same expression to be emitted once for each constructor defined, so the correct fix is to make StringConcat not mutate its data, and allow for multiple emissions. Ben is looking at this. This is not easy, because in general, it means that we should ensure that Emit in every Expression is non mutating. ---- Additional Comments From miguel@ximian.com 2004-11-07 00:22:13 MST ---- I believe this got fixed for the 1.0.4 release, Ben? If not, its fixed oN CVS. ---- Additional Comments From miguel@ximian.com 2004-11-07 00:22:22 MST ---- I mean, fixed. Imported an attachment (id=166854)