[Mono-dev] Optimization 'precomp' violates ECMA-335

Martin Däumler mdae at cs.tu-chemnitz.de
Thu Jan 19 16:37:55 UTC 2012


I tried out several optimizations of Mono 2.6.1 and Mono
So I used "-O=precomp". The class 'Test' of the appended test case
has an (explicit) class constructor. That is, in class Test's IL-code,
the field "beforefieldinit" is not set. ECMA-335 §8.9.5 states that
the class constructor is executed at first access to any static member
of that class or at first creation of one instance of that class, as
I understood it. When I execute the program with Mono 2.6.1 and the
optimization "-O=precomp", class Test's class constructor is executed
before first access. Even worse, Mono crashes while

So, what have do be done in order to don't violate the specification?
How is it handled in AOT-mode?

With kind regards,
Martin Däumler

using System;

class Test
     public static string x;

     static Test ()
       Console.WriteLine("In type initializer");
       x = "In type initializer";

     public static string EchoAndReturn (string s)
         Console.WriteLine (s);
         return s;

class Driver
     public static void Main()
         Console.WriteLine("Starting Main");
         // Invoke a static method on Test
         Console.WriteLine("After echo");
         // Reference a static field in Test
         string y = Test.x;
         // Use the value just to avoid compiler cleverness
         if (y != null)
             Console.WriteLine("After field access");

More information about the Mono-devel-list mailing list