Search Unity

Internal compiler error caused by valid code

Discussion in 'Editor & General Support' started by cowlinator, Jan 17, 2014.

  1. cowlinator

    cowlinator

    Joined:
    Mar 15, 2012
    Posts:
    69
    Unity and/or mono generates an "internal compiler error" referring to a completely unrelated code file when compiling the following valid code:


    Code (csharp):
    1. using System.Collections;
    2.  
    3. public class Error
    4. {
    5.     const bool WAIT = false;
    6.  
    7.     IEnumerator error()
    8.     {
    9.         if (WAIT)
    10.             yield return null;
    11.  
    12.         yield break;
    13.     }
    14. }


    However, this code compiles correctly:

    Code (csharp):
    1. using System.Collections;
    2.  
    3. public class Error
    4. {
    5.     static bool WAIT = false;
    6.  
    7.     IEnumerator error()
    8.     {
    9.         if (WAIT)
    10.             yield return null;
    11.  
    12.         yield break;
    13.     }
    14. }
     
  2. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    973
    It looks like the compiler does not like a yield being unreachable code. The same thing happens with:

    Code (csharp):
    1.  
    2. using System.Collections;
    3.  
    4. public class Error
    5. {
    6.     IEnumerator error()
    7.     {
    8.         if (false)
    9.             yield return null;
    10.         yield break;
    11.     }
    12. }
    13.  
    The actual compiler exception is:


    Unhandled Exception: Mono.CSharp.InternalErrorException: Assets/CrashCompiler.cs(8,5): <error>c__Iterator0 ---> System.ArgumentException: Label not marked
    at System.Reflection.Emit.ILGenerator.label_fixup () [0x00000] in <filename unknown>:0
    at System.Reflection.Emit.MethodBuilder.fixup () [0x00000] in <filename unknown>:0
    at System.Reflection.Emit.TypeBuilder.CreateType () [0x00000] in <filename unknown>:0
    at Mono.CSharp.TypeContainer.CloseType () [0x00000] in <filename unknown>:0

    --- End of inner exception stack trace ---
    at Mono.CSharp.TypeContainer.CloseType () [0x00000] in <filename unknown>:0
    at Mono.CSharp.TypeContainer.CloseType () [0x00000] in <filename unknown>:0
    at Mono.CSharp.RootContext.CloseTypes () [0x00000] in <filename unknown>:0
    at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0
    at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0

    Sounds like bug report time to me!
     
  3. cowlinator

    cowlinator

    Joined:
    Mar 15, 2012
    Posts:
    69
    Yes, that's my observation as well, except the error i received was different: it was erroring on a different unrelated script in my project.
    It's almost as though the compiler gets the exception stacktrace from the most recent correctly compiled script or something.

    I have submitted a bug report.
     
  4. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    973
    The error appears in other unrelated scripts indeed, just having this compiled is enough to trigger it. I got the stack trace by having it only compile this one.

    A bit of googling reveals that it's been a Mono bug for a while, it's an active issue on the tracker.

    My guess is, the optimiser strips away the unreachable code and the IEnumerator maker tries to access the label on it, but I haven't actually checked how IEnumerators are compiled, so I could be wrong.
     
  5. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
    Just confirming this is still in v4.5. Compiler cashes on unreachable code that contains a yield.
     
  6. airship-brooks

    airship-brooks

    Joined:
    Aug 14, 2015
    Posts:
    20
    Still happens in Unity 5.1.2. Wasted my afternoon tracking it down.

    It's really annoying because it shows you an error in an unrelated place in your code; basically, Unity takes a benign warning and "promotes" it into an error and acts like that's your problem. In my case, it was an "unreachable code" warning in some other file.

    If the compiler crashes, how about just parsing that properly and telling the user so we can more easily google it and work around it?

    Fixing the crash would be nice, but fixing the error/warning parsing seems pretty crucial here.
     
  7. jeremyfp

    jeremyfp

    Joined:
    Oct 14, 2013
    Posts:
    8
    Still present in 5.2.1f1. Frustrating to say the least.
     
  8. xyzDave

    xyzDave

    Joined:
    Jun 19, 2014
    Posts:
    24
    Still present in 5.3.2p3
    Just caught me out until I googled it and found this thread.
    Appears to cause a lot of warnings to be treated as errors too.
     
  9. taxvi

    taxvi

    Joined:
    Feb 18, 2013
    Posts:
    30
    5.3.4p1 and still there
    changing the variable from const to static solved it for me too