Mark more structs as readonly (dotnet/coreclr#19557)
authorBen Adams <thundercat@illyriad.co.uk>
Mon, 20 Aug 2018 16:25:47 +0000 (17:25 +0100)
committerJan Kotas <jkotas@microsoft.com>
Mon, 20 Aug 2018 16:25:47 +0000 (09:25 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/ac09773ad09f374053ae1cb11336b96d01da49a1

src/coreclr/src/System.Private.CoreLib/src/System/Array.cs
src/coreclr/src/System.Private.CoreLib/src/System/ByReference.cs
src/coreclr/src/System.Private.CoreLib/src/System/RtType.cs
src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/HandleRef.cs
src/libraries/System.Private.CoreLib/src/System/Runtime/Serialization/SerializationInfoEnumerator.cs
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.BinarySearch.cs
src/libraries/System.Private.CoreLib/src/System/Threading/AsyncLocal.cs
src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs

index 630ccfc..5cb7f1d 100644 (file)
@@ -1889,11 +1889,11 @@ namespace System
 
 
         // Private value type used by the Sort methods.
-        private struct SorterObjectArray
+        private readonly struct SorterObjectArray
         {
-            private object[] keys;
-            private object[] items;
-            private IComparer comparer;
+            private readonly object[] keys;
+            private readonly object[] items;
+            private readonly IComparer comparer;
 
             internal SorterObjectArray(object[] keys, object[] items, IComparer comparer)
             {
@@ -2095,11 +2095,11 @@ namespace System
         // Private value used by the Sort methods for instances of Array.
         // This is slower than the one for Object[], since we can't use the JIT helpers
         // to access the elements.  We must use GetValue & SetValue.
-        private struct SorterGenericArray
+        private readonly struct SorterGenericArray
         {
-            private Array keys;
-            private Array items;
-            private IComparer comparer;
+            private readonly Array keys;
+            private readonly Array items;
+            private readonly IComparer comparer;
 
             internal SorterGenericArray(Array keys, Array items, IComparer comparer)
             {
index 3e1fa8a..298b4bb 100644 (file)
@@ -11,9 +11,9 @@ namespace System
     // around lack of first class support for byref fields in C# and IL. The JIT and 
     // type loader has special handling for it that turns it into a thin wrapper around ref T.
     [NonVersionable]
-    internal ref struct ByReference<T>
+    internal readonly ref struct ByReference<T>
     {
-        private IntPtr _value;
+        private readonly IntPtr _value;
 
         public ByReference(ref T value)
         {
index cf08651..0e5cf81 100644 (file)
@@ -186,11 +186,11 @@ namespace System
                 NestedType
             }
 
-            private struct Filter
+            private readonly struct Filter
             {
-                private MdUtf8String m_name;
-                private MemberListType m_listType;
-                private uint m_nameHash;
+                private readonly MdUtf8String m_name;
+                private readonly MemberListType m_listType;
+                private readonly uint m_nameHash;
 
                 public unsafe Filter(byte* pUtf8Name, int cUtf8Name, MemberListType listType)
                 {
@@ -4888,7 +4888,7 @@ namespace System
     }
 
     #region Library
-    internal unsafe struct MdUtf8String
+    internal readonly unsafe struct MdUtf8String
     {
         [MethodImplAttribute(MethodImplOptions.InternalCall)]
         private static extern unsafe bool EqualsCaseSensitive(void* szLhs, void* szRhs, int cSz);
@@ -4917,8 +4917,8 @@ namespace System
             return len;
         }
 
-        private void* m_pStringHeap;        // This is the raw UTF8 string.
-        private int m_StringHeapByteLength;
+        private readonly void* m_pStringHeap;        // This is the raw UTF8 string.
+        private readonly int m_StringHeapByteLength;
 
         internal MdUtf8String(void* pStringHeap)
         {
index 3f642e8..1bf8174 100644 (file)
@@ -804,7 +804,7 @@ namespace System.Globalization
                     }
                 }
 
-                result.parsedTimeSpan._ticks = ticks;
+                result.parsedTimeSpan = new TimeSpan(ticks);
                 return true;
             }
 
@@ -932,7 +932,7 @@ namespace System.Globalization
                     }
                 }
 
-                result.parsedTimeSpan._ticks = ticks;
+                result.parsedTimeSpan = new TimeSpan(ticks);
                 return true;
             }
 
@@ -1058,7 +1058,7 @@ namespace System.Globalization
                     }
                 }
 
-                result.parsedTimeSpan._ticks = ticks;
+                result.parsedTimeSpan = new TimeSpan(ticks);
                 return true;
             }
 
@@ -1130,7 +1130,7 @@ namespace System.Globalization
                     }
                 }
 
-                result.parsedTimeSpan._ticks = ticks;
+                result.parsedTimeSpan = new TimeSpan(ticks);
                 return true;
             }
 
@@ -1200,7 +1200,7 @@ namespace System.Globalization
                     }
                 }
 
-                result.parsedTimeSpan._ticks = ticks;
+                result.parsedTimeSpan = new TimeSpan(ticks);
                 return true;
             }
 
@@ -1399,7 +1399,7 @@ namespace System.Globalization
                     ticks = -ticks;
                 }
 
-                result.parsedTimeSpan._ticks = ticks;
+                result.parsedTimeSpan = new TimeSpan(ticks);
                 return true;
             }
             else
@@ -1494,7 +1494,7 @@ namespace System.Globalization
 
             internal bool TryParse(ReadOnlySpan<char> input, ref TimeSpanResult result)
             {
-                result.parsedTimeSpan._ticks = 0;
+                result.parsedTimeSpan = default;
 
                 _str = input;
                 _len = input.Length;
@@ -1563,7 +1563,7 @@ namespace System.Globalization
                     return result.SetBadTimeSpanFailure();
                 }
 
-                result.parsedTimeSpan._ticks = time;
+                result.parsedTimeSpan = new TimeSpan(time);
                 return true;
             }
 
index 64d0553..c81a701 100644 (file)
@@ -4,12 +4,12 @@
 
 namespace System.Runtime.InteropServices
 {
-    public struct HandleRef
+    public readonly struct HandleRef
     {
         // ! Do not add or rearrange fields as the EE depends on this layout.
         //------------------------------------------------------------------
-        private object _wrapper;
-        private IntPtr _handle;
+        private readonly object _wrapper;
+        private readonly IntPtr _handle;
         //------------------------------------------------------------------
 
         public HandleRef(object wrapper, IntPtr handle)
index 6399510..ba84e65 100644 (file)
@@ -7,11 +7,11 @@ using System.Diagnostics;
 
 namespace System.Runtime.Serialization
 {
-    public struct SerializationEntry
+    public readonly struct SerializationEntry
     {
-        private string _name;
-        private object _value;
-        private Type _type;
+        private readonly string _name;
+        private readonly object _value;
+        private readonly Type _type;
 
         internal SerializationEntry(string entryName, object entryValue, Type entryType)
         {
index a81a5d3..2aec704 100644 (file)
@@ -62,11 +62,11 @@ namespace System
         }
 
         // Helper to allow sharing all code via IComparable<T> inlineable
-        internal struct ComparerComparable<T, TComparer> : IComparable<T>
+        internal readonly struct ComparerComparable<T, TComparer> : IComparable<T>
             where TComparer : IComparer<T>
         {
-            readonly T _value;
-            readonly TComparer _comparer;
+            private readonly T _value;
+            private readonly TComparer _comparer;
 
             public ComparerComparable(T value, TComparer comparer)
             {
index 84bbd17..12d6502 100644 (file)
@@ -86,19 +86,18 @@ namespace System.Threading
         void OnValueChanged(object previousValue, object currentValue, bool contextChanged);
     }
 
-    public struct AsyncLocalValueChangedArgs<T>
+    public readonly struct AsyncLocalValueChangedArgs<T>
     {
-        public T PreviousValue { get; private set; }
-        public T CurrentValue { get; private set; }
+        public T PreviousValue { get; }
+        public T CurrentValue { get; }
 
         //
         // If the value changed because we changed to a different ExecutionContext, this is true.  If it changed
         // because someone set the Value property, this is false.
         //
-        public bool ThreadContextChanged { get; private set; }
+        public bool ThreadContextChanged { get; }
 
         internal AsyncLocalValueChangedArgs(T previousValue, T currentValue, bool contextChanged)
-            : this()
         {
             PreviousValue = previousValue;
             CurrentValue = currentValue;
index 1b94c9f..10bdb33 100644 (file)
@@ -28,7 +28,7 @@ namespace System
     // an appropriate custom ILMarshaler to keep WInRT interop scenarios enabled.
     //
     [Serializable]
-    public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable, ISpanFormattable
+    public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable, ISpanFormattable
     {
         public const long TicksPerMillisecond = 10000;
         private const double MillisecondsPerTick = 1.0 / TicksPerMillisecond;
@@ -65,7 +65,7 @@ namespace System
 
         // internal so that DateTime doesn't have to call an extra get
         // method for some arithmetic operations.
-        internal long _ticks; // Do not rename (binary serialization)
+        internal readonly long _ticks; // Do not rename (binary serialization)
 
         public TimeSpan(long ticks)
         {