[svn] r6042: nemerle/trunk: lib/internal.n ncc/external/InternalTypes.n ncc/external/LibrariesLoader.n ncc...

malekith svnadmin at nemerle.org
Wed Dec 28 12:07:43 CET 2005


Log:
Mark immutable fields with a special attribute, forbid writes to them. Resolves #593.

Author: malekith
Date: Wed Dec 28 12:07:41 2005
New Revision: 6042

Added:
   nemerle/trunk/ncc/testsuite/negative/immutable-field.n
Modified:
   nemerle/trunk/lib/internal.n
   nemerle/trunk/ncc/external/InternalTypes.n
   nemerle/trunk/ncc/external/LibrariesLoader.n
   nemerle/trunk/ncc/generation/HierarchyEmitter.n
   nemerle/trunk/ncc/generation/Typer3.n
   nemerle/trunk/ncc/testsuite/positive/attributes.n
   nemerle/trunk/ncc/testsuite/positive/struct.n

Modified: nemerle/trunk/lib/internal.n
==============================================================================
--- nemerle/trunk/lib/internal.n	(original)
+++ nemerle/trunk/lib/internal.n	Wed Dec 28 12:07:41 2005
@@ -41,6 +41,13 @@
 
   
   /**
+   * Marks an immutable field
+   */
+  public class ImmutableAttribute : NemerleAttribute
+  {  }
+
+  
+  /**
    * Used to store the Nemerle types in genereated assembly metadata
    */    
   public class TypeAttribute : NemerleAttribute

Modified: nemerle/trunk/ncc/external/InternalTypes.n
==============================================================================
--- nemerle/trunk/ncc/external/InternalTypes.n	(original)
+++ nemerle/trunk/ncc/external/InternalTypes.n	Wed Dec 28 12:07:41 2005
@@ -126,6 +126,11 @@
     get { InternalType.VolatileModifier_tc.SystemType }
   }
   
+  public ImmutableAttribute : System.Type
+  {
+    get { InternalType.ImmutableAttribute_tc.SystemType }
+  }
+  
   public ConstantVariantOptionAttribute : System.Type
   {
     get { InternalType.ConstantVariantOptionAttribute_tc.SystemType }
@@ -370,6 +375,7 @@
   public mutable ContainsMacroAttribute_tc : TypeInfo;
   public mutable TypeAttribute_tc : TypeInfo;
   public mutable VariantAttribute_tc : TypeInfo;
+  public mutable ImmutableAttribute_tc : TypeInfo;
   public mutable TypeAliasAttribute_tc : TypeInfo;
   public mutable VariantOptionAttribute_tc : TypeInfo;
   public mutable VolatileModifier_tc : TypeInfo;
@@ -585,6 +591,7 @@
       InternalType.TypeAliasAttribute_tc = lookup ("Nemerle.Internal.TypeAliasAttribute"); 
       InternalType.VariantOptionAttribute_tc = lookup ("Nemerle.Internal.VariantOptionAttribute");
       InternalType.VolatileModifier_tc = lookup ("Nemerle.Internal.VolatileModifier");
+      InternalType.ImmutableAttribute_tc = lookup ("Nemerle.Internal.ImmutableAttribute");
       InternalType.ConstantVariantOptionAttribute_tc = lookup ("Nemerle.Internal.ConstantVariantOptionAttribute");
       InternalType.ExtensionPatternEncodingAttribute_tc = lookup ("Nemerle.Internal.ExtensionPatternEncodingAttribute");
       

Modified: nemerle/trunk/ncc/external/LibrariesLoader.n
==============================================================================
--- nemerle/trunk/ncc/external/LibrariesLoader.n	(original)
+++ nemerle/trunk/ncc/external/LibrariesLoader.n	Wed Dec 28 12:07:41 2005
@@ -1426,7 +1426,12 @@
 
       public IsMutable : bool
       {
-        get { !handle.IsInitOnly }
+        get
+        {
+          // shouldn't we use InternalType here?
+          !handle.IsInitOnly && 
+          !handle.IsDefined (typeof (Nemerle.Internal.ImmutableAttribute), false)
+        }
       }
 
       public IsVolatile : bool

Modified: nemerle/trunk/ncc/generation/HierarchyEmitter.n
==============================================================================
--- nemerle/trunk/ncc/generation/HierarchyEmitter.n	(original)
+++ nemerle/trunk/ncc/generation/HierarchyEmitter.n	Wed Dec 28 12:07:41 2005
@@ -1028,11 +1028,15 @@
         });
       }
       
-      when (IsVolatile)
-      {
+      when (IsVolatile) {
         def volatile_attr = AttributeCompiler.MakeEmittedAttribute (SystemType.VolatileModifier);
         field_builder.SetCustomAttribute (volatile_attr)
       }
+
+      unless (IsMutable) {
+        def imm_attr = AttributeCompiler.MakeEmittedAttribute (SystemType.ImmutableAttribute);
+        field_builder.SetCustomAttribute (imm_attr)
+      }
     }
   }
 

Modified: nemerle/trunk/ncc/generation/Typer3.n
==============================================================================
--- nemerle/trunk/ncc/generation/Typer3.n	(original)
+++ nemerle/trunk/ncc/generation/Typer3.n	Wed Dec 28 12:07:41 2005
@@ -1273,7 +1273,7 @@
           //Message.Debug ($"add field $name to $lambda_name : $clo_type --> $(fix_type (clo_type)) new_tp=$new_tp $local_function_type->$(local_function_type.typarms) $current_method->$(current_method.typarms)");
           def field =
             builder.DefineAndReturn (<[ decl: 
-              $(name : name) : $(fix_type (clo_type) : typed) 
+              mutable $(name : name) : $(fix_type (clo_type) : typed) 
             ]>) :> IField;
           clo_fields [hd.id] = field;
           (<[ parameter: $(name : name) : $(fix_type (clo_type) : typed) ]>,

Added: nemerle/trunk/ncc/testsuite/negative/immutable-field.n
==============================================================================
--- (empty file)
+++ nemerle/trunk/ncc/testsuite/negative/immutable-field.n	Wed Dec 28 12:07:41 2005
@@ -0,0 +1,2 @@
+def x = 1 :: [];
+x.tl = [2]; // E: needed a writable location for assignment target, got a reference to field `tl', which is read-only

Modified: nemerle/trunk/ncc/testsuite/positive/attributes.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/attributes.n	(original)
+++ nemerle/trunk/ncc/testsuite/positive/attributes.n	Wed Dec 28 12:07:41 2005
@@ -171,7 +171,7 @@
 
   class F {
     [XmlElement("foo")]
-    x : int;
+    mutable x : int;
   }
 }
 
@@ -203,7 +203,7 @@
 
 	public static Run () :  int
 	{
-                def x = typeof (FooEnum).GetField ("B").GetCustomAttributes (false) [0];
+                def x = typeof (FooEnum).GetField ("B").GetCustomAttributes (typeof (SimpleAttribute), false) [0];
                 Console.WriteLine ((x :> SimpleAttribute).n);
                 foreach (x in typeof (JOO.A).GetCustomAttributes (false))
                   match (x) {

Modified: nemerle/trunk/ncc/testsuite/positive/struct.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/struct.n	(original)
+++ nemerle/trunk/ncc/testsuite/positive/struct.n	Wed Dec 28 12:07:41 2005
@@ -128,6 +128,18 @@
   }
 }
 
+
+public struct MapCollection
+{
+    x : int;
+    
+    public foo () : void
+    {
+        def buffer = 0;
+        [].Iter (fun(map){ _ = buffer; });
+    }
+}
+
 public module Test
 {
   test_passing_around (s : TestStruct) : void



More information about the svn mailing list