[Mono-bugs] [Bug 430804] New: Static readonly field optimization doesn' t account for changes via reflection

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Mon Sep 29 18:34:09 EDT 2008


https://bugzilla.novell.com/show_bug.cgi?id=430804


           Summary: Static readonly field optimization doesn't account for
                    changes via reflection
           Product: Mono: Compilers
           Version: 1.9
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: C#
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: jonbnews at hotmail.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---


It appears that static readonly fields are being optimized in a way that
doesn't account for them being changed via reflection.  Consider the following
example:

public class TheClass
{
   public class Foo
   {
      // Initialize to zero.
      public static readonly int s_int = 0;

      // Return the value of the int.
      public static int TheInt
      {
         get { return s_int; }
      }
   }

   public static void Main(params string[] args)
   {
      // Get the field via reflection.
      Type type = typeof (Foo);
      FieldInfo fi = type.GetField("s_int", BindingFlags.Static |
BindingFlags.Public);

      // Write out the initial value.
      Console.Out.WriteLine("TheInt: " + Foo.TheInt);
      // Set the value via reflection.
      fi.SetValue(null, 1);
      // Write out the value that we get via reflection.
      Console.Out.WriteLine("TheInt GetValue: " + fi.GetValue(null));
      // Write out the value we get by calling the property.
      Console.Out.WriteLine("TheInt: " + Foo.TheInt);
   }
}

Expected Output:
TheInt: 0
TheInt GetValue: 1
TheInt: 1

Actual Output:
TheInt: 0
TheInt GetValue: 1
TheInt: 0

We verify that the static value actually changed by retrieving it via
reflection after it's set.  But the property fails to return the new value. 
This suggests that the compiler or something has optimized this and it's
returning some cached value which is not the current value.  It's probably
thinking that since the field is declared "readonly" that it can't be changed
after it's been initialized.

I think this is a bug because if the reflection call lets us set the value,
then the compiler can't assume that the field can't change after
initialization.


-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list