Bug 26892

Summary: mcs generates CS0254 error for cast in fixed statement
Product: [Mono] Compilers Reporter: Alexander Köplinger <alex.koeplinger>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Tags: Is this bug a regression?: ---
Last known good build:

Description Alexander Köplinger 2015-02-09 22:05:24 UTC
The following code generates a CS0254 on mcs, but works fine on csc and roslyn:

> using System;
> using System.Collections.Generic;

> public class Program
> {
>         public unsafe static void Main()
>         {
>                 var list = new List<object>();
>                 fixed (char *c = (string)list[0]) { }
>         }
> }


> $ mcs /unsafe test.cs 
> test.cs(9,20): error CS0254: The right hand side of a fixed statement assignment may not be a cast expression
> Compilation failed: 1 error(s), 0 warnings
Comment 1 Marek Safar 2015-02-10 03:31:56 UTC
Fixed in master
Comment 2 Alexander Köplinger 2015-02-10 17:12:18 UTC
I'm now seeing a compiler crash in the code this test case originally came from.

New test case:

> using System;
> 
> public class Program
> {
>         public unsafe static int Main()
>         {
>                 var list = new object[] { "" };
> 
>                 if (list[0] is string)
>                 {
>                         fixed (char* p = (string)list[0])
>                                 return 0;
>                 }
>                 else
>                 {
>                         fixed (byte* p = (byte[])list[0])
>                                 return 1;
>                 }
>         }
> }


Compiler crash:

> $ MONO_OPTIONS=--debug mcs /unsafe test.cs 
> 
> [ERROR] FATAL UNHANDLED EXCEPTION: Mono.CSharp.InternalErrorException: test.cs(5,34): Program.Main() ---> Mono.CSharp.InternalErrorException: test.cs(6,9): ---> Mono.CSharp.InternalErrorException: Missing Resolve call
>   at Mono.CSharp.ShimExpression.Emit (Mono.CSharp.EmitContext ec) [0x00000] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/ecore.cs:2477 
>   at Mono.CSharp.Expression.EmitBranchable (Mono.CSharp.EmitContext ec, Label target, Boolean on_true) [0x00000] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/ecore.cs:618 
>   at Mono.CSharp.Binary.EmitBranchable (Mono.CSharp.EmitContext ec, Label target, Boolean on_true) [0x0010b] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/expression.cs:5302 
>   at Mono.CSharp.Binary.EmitBranchable (Mono.CSharp.EmitContext ec, Label target, Boolean on_true) [0x00207] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/expression.cs:5340 
>   at Mono.CSharp.Conditional.Emit (Mono.CSharp.EmitContext ec) [0x0000e] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/expression.cs:6250 
>   at Mono.CSharp.Fixed+ExpressionEmitter.Emit (Mono.CSharp.EmitContext ec) [0x00000] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:6245 
>   at Mono.CSharp.Fixed.DoEmit (Mono.CSharp.EmitContext ec) [0x00011] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:6449 
>   at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x0000d] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:52 
>   at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00007] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:2933 
>   at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x0004a] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:3231 
>   at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00007] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:2933 
>   at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x0004a] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:3231 
>   at Mono.CSharp.If.DoEmit (Mono.CSharp.EmitContext ec) [0x000a2] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:257 
>   at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x0000d] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:52 
>   at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00007] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:2933 
>   at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x0004a] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:3231 
>   at Mono.CSharp.ParametersBlock.Emit (Mono.CSharp.EmitContext ec) [0x00036] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:3779 
>   at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00045] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:4389 
>   --- End of inner exception stack trace ---
>   at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x000d3] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/statement.cs:4418 
>   at Mono.CSharp.MethodData.Emit (Mono.CSharp.TypeDefinition parent) [0x00092] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/method.cs:2191 
>   at Mono.CSharp.MethodOrOperator.Emit () [0x001d0] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/method.cs:731 
>   at Mono.CSharp.Method.Emit () [0x0012c] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/method.cs:1374 
>   --- End of inner exception stack trace ---
>   at Mono.CSharp.Method.Emit () [0x00139] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/method.cs:1376 
>   at Mono.CSharp.TypeDefinition.Emit () [0x0026e] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/class.cs:2233 
>   at Mono.CSharp.ClassOrStruct.Emit () [0x00025] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/class.cs:2733 
>   at Mono.CSharp.Class.Emit () [0x00000] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/class.cs:2871 
>   at Mono.CSharp.TypeDefinition.EmitContainer () [0x0000f] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/class.cs:2266 
>   at Mono.CSharp.TypeContainer.EmitContainer () [0x00012] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/class.cs:335 
>   at Mono.CSharp.NamespaceContainer.EmitContainer () [0x00007] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/namespace.cs:876 
>   at Mono.CSharp.TypeContainer.EmitContainer () [0x00012] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/class.cs:335 
>   at Mono.CSharp.ModuleContainer.EmitContainer () [0x0009c] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/module.cs:574 
>   at Mono.CSharp.AssemblyDefinition.Emit () [0x00098] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/assembly.cs:484 
>   at Mono.CSharp.AssemblyDefinitionStatic.Emit () [0x00126] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/ikvm.cs:215 
>   at Mono.CSharp.Driver.Compile () [0x002dc] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/driver.cs:384 
>   at Mono.CSharp.Driver.Main (System.String[] args) [0x00052] in /tmp/buildd/mono-snapshot-20150210100505-20150210100505/mcs/mcs/driver.cs:187
Comment 3 Marek Safar 2015-02-11 03:22:05 UTC
Thanks for reporting, fixed in master as well.