Mark applicable structs as readonly (#14789)
authorStephen Toub <stoub@microsoft.com>
Fri, 3 Nov 2017 11:45:14 +0000 (07:45 -0400)
committerJan Kotas <jkotas@microsoft.com>
Fri, 3 Nov 2017 11:45:14 +0000 (04:45 -0700)
In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type.

18 files changed:
src/mscorlib/shared/System/Collections/Generic/KeyValuePair.cs
src/mscorlib/shared/System/DateTime.cs
src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs
src/mscorlib/shared/System/Globalization/DaylightTime.cs
src/mscorlib/shared/System/ParamsArray.cs
src/mscorlib/shared/System/Reflection/ParameterModifier.cs
src/mscorlib/shared/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs
src/mscorlib/shared/System/Runtime/Serialization/StreamingContext.cs
src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs
src/mscorlib/src/System/ArraySegment.cs
src/mscorlib/src/System/DateTime.CoreCLR.cs
src/mscorlib/src/System/Nullable.cs
src/mscorlib/src/System/Reflection/Emit/MethodBuilder.cs
src/mscorlib/src/System/Runtime/CompilerServices/TaskAwaiter.cs
src/mscorlib/src/System/Runtime/CompilerServices/YieldAwaitable.cs
src/mscorlib/src/System/Threading/CancellationToken.cs
src/mscorlib/src/System/Threading/CancellationTokenRegistration.cs
src/mscorlib/src/System/TimeZoneInfo.TransitionTime.cs

index aeafecd..82c786d 100644 (file)
@@ -47,10 +47,10 @@ namespace System.Collections.Generic
     // and IReadOnlyDictionary<TKey, TValue>.
     [Serializable]
     [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
-    public struct KeyValuePair<TKey, TValue>
+    public readonly struct KeyValuePair<TKey, TValue>
     {
-        private TKey key; // Do not rename (binary serialization)
-        private TValue value; // Do not rename (binary serialization)
+        private readonly TKey key; // Do not rename (binary serialization)
+        private readonly TValue value; // Do not rename (binary serialization)
 
         public KeyValuePair(TKey key, TValue value)
         {
index ad4d3a0..d3720b2 100644 (file)
@@ -53,7 +53,7 @@ namespace System
     [StructLayout(LayoutKind.Auto)]
     [Serializable]
     [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] 
-    public partial struct DateTime : IComparable, IFormattable, IConvertible, IComparable<DateTime>, IEquatable<DateTime>, ISerializable
+    public readonly partial struct DateTime : IComparable, IFormattable, IConvertible, IComparable<DateTime>, IEquatable<DateTime>, ISerializable
     {
         // Number of 100ns ticks per time unit
         private const long TicksPerMillisecond = 10000;
@@ -135,7 +135,7 @@ namespace System
         //               savings time hour and it is in daylight savings time. This allows distinction of these
         //               otherwise ambiguous local times and prevents data loss when round tripping from Local to
         //               UTC time.
-        private UInt64 _dateData;
+        private readonly UInt64 _dateData;
 
         // Constructs a DateTime from a tick count. The ticks
         // argument specifies the date as the number of 100-nanosecond intervals
index 854bc06..cff424d 100644 (file)
@@ -1,4 +1,4 @@
-using System;
+using System;
 using System.Reflection;
 using System.Runtime.InteropServices;
 using System.Diagnostics;
@@ -27,7 +27,7 @@ namespace System.Diagnostics.Tracing
 #else
     internal
 #endif
-    unsafe struct PropertyValue
+    unsafe readonly struct PropertyValue
     {
         /// <summary>
         /// Union of well-known value types, to avoid boxing those types.
index 10f074d..e6920b3 100644 (file)
@@ -33,7 +33,7 @@ namespace System.Globalization
     }
 
     // Value type version of DaylightTime
-    internal struct DaylightTimeStruct
+    internal readonly struct DaylightTimeStruct
     {
         public DaylightTimeStruct(DateTime start, DateTime end, TimeSpan delta)
         {
index 1c73bc5..043ee67 100644 (file)
@@ -4,7 +4,7 @@
 
 namespace System
 {
-    internal struct ParamsArray
+    internal readonly struct ParamsArray
     {
         // Sentinel fixed-length arrays eliminate the need for a "count" field keeping this
         // struct down to just 4 fields. These are only used for their "Length" property,
index 640fee2..0fb75ff 100644 (file)
@@ -4,7 +4,7 @@
 
 namespace System.Reflection
 {
-    public struct ParameterModifier
+    public readonly struct ParameterModifier
     {
         private readonly bool[] _byRef;
 
index f710678..b4efc73 100644 (file)
@@ -11,7 +11,7 @@ namespace System.Runtime.CompilerServices
     /// <summary>Provides an awaitable type that enables configured awaits on a <see cref="ValueTask{TResult}"/>.</summary>
     /// <typeparam name="TResult">The type of the result produced.</typeparam>
     [StructLayout(LayoutKind.Auto)]
-    public struct ConfiguredValueTaskAwaitable<TResult>
+    public readonly struct ConfiguredValueTaskAwaitable<TResult>
     {
         /// <summary>The wrapped <see cref="ValueTask{TResult}"/>.</summary>
         private readonly ValueTask<TResult> _value;
index 4fe90ca..cdcb1c3 100644 (file)
@@ -4,7 +4,7 @@
 
 namespace System.Runtime.Serialization
 {
-    public struct StreamingContext
+    public readonly struct StreamingContext
     {
         private readonly object _additionalContext;
         private readonly StreamingContextStates _state;
index 4ccf0f8..de9b016 100644 (file)
@@ -50,7 +50,7 @@ namespace System.Threading.Tasks
     /// </remarks>
     [AsyncMethodBuilder(typeof(AsyncValueTaskMethodBuilder<>))]
     [StructLayout(LayoutKind.Auto)]
-    public struct ValueTask<TResult> : IEquatable<ValueTask<TResult>>
+    public readonly struct ValueTask<TResult> : IEquatable<ValueTask<TResult>>
     {
         /// <summary>The task to be used if the operation completed asynchronously or if it completed synchronously but non-successfully.</summary>
         internal readonly Task<TResult> _task;
index a96db8f..251dee8 100644 (file)
@@ -26,7 +26,7 @@ namespace System
     // (ie, users could assign a new value to the old location).  
     [Serializable]
     [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
-    public struct ArraySegment<T> : IList<T>, IReadOnlyList<T>
+    public readonly struct ArraySegment<T> : IList<T>, IReadOnlyList<T>
     {
         // Do not replace the array allocation with Array.Empty. We don't want to have the overhead of
         // instantiating another generic type in addition to ArraySegment<T> for new type parameters.
index 4d36b99..84cda1d 100644 (file)
@@ -6,7 +6,7 @@ using System.Runtime.CompilerServices;
 
 namespace System
 {
-    public partial struct DateTime
+    public readonly partial struct DateTime
     {
         public static DateTime UtcNow
         {
index f9ee9ac..159453d 100644 (file)
@@ -23,10 +23,10 @@ namespace System
     [Serializable]
     [System.Runtime.Versioning.NonVersionable] // This only applies to field layout
     [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
-    public struct Nullable<T> where T : struct
+    public readonly struct Nullable<T> where T : struct
     {
-        private bool hasValue; // Do not rename (binary serialization)
-        internal T value; // Do not rename (binary serialization)
+        private readonly bool hasValue; // Do not rename (binary serialization)
+        internal readonly T value; // Do not rename (binary serialization)
 
         [System.Runtime.Versioning.NonVersionable]
         public Nullable(T value)
index 928b2e3..daa6d76 100644 (file)
@@ -1103,7 +1103,7 @@ namespace System.Reflection.Emit
     /// Describes exception handler in a method body.
     /// </summary>
     [StructLayout(LayoutKind.Sequential)]
-    internal struct ExceptionHandler : IEquatable<ExceptionHandler>
+    internal readonly struct ExceptionHandler : IEquatable<ExceptionHandler>
     {
         // Keep in sync with unmanged structure. 
         internal readonly int m_exceptionClass;
index 5b2ac28..5fb4371 100644 (file)
@@ -55,7 +55,7 @@ namespace System.Runtime.CompilerServices
 {
     /// <summary>Provides an awaiter for awaiting a <see cref="System.Threading.Tasks.Task"/>.</summary>
     /// <remarks>This type is intended for compiler use only.</remarks>
-    public struct TaskAwaiter : ICriticalNotifyCompletion, ITaskAwaiter
+    public readonly struct TaskAwaiter : ICriticalNotifyCompletion, ITaskAwaiter
     {
         // WARNING: Unsafe.As is used to access the generic TaskAwaiter<> as TaskAwaiter.
         // Its layout must remain the same.
@@ -320,7 +320,7 @@ namespace System.Runtime.CompilerServices
 
     /// <summary>Provides an awaiter for awaiting a <see cref="System.Threading.Tasks.Task{TResult}"/>.</summary>
     /// <remarks>This type is intended for compiler use only.</remarks>
-    public struct TaskAwaiter<TResult> : ICriticalNotifyCompletion, ITaskAwaiter
+    public readonly struct TaskAwaiter<TResult> : ICriticalNotifyCompletion, ITaskAwaiter
     {
         // WARNING: Unsafe.As is used to access TaskAwaiter<> as the non-generic TaskAwaiter.
         // Its layout must remain the same.
@@ -409,7 +409,7 @@ namespace System.Runtime.CompilerServices
 
     /// <summary>Provides an awaitable object that allows for configured awaits on <see cref="System.Threading.Tasks.Task"/>.</summary>
     /// <remarks>This type is intended for compiler use only.</remarks>
-    public struct ConfiguredTaskAwaitable
+    public readonly struct ConfiguredTaskAwaitable
     {
         /// <summary>The task being awaited.</summary>
         private readonly ConfiguredTaskAwaitable.ConfiguredTaskAwaiter m_configuredTaskAwaiter;
@@ -434,7 +434,7 @@ namespace System.Runtime.CompilerServices
 
         /// <summary>Provides an awaiter for a <see cref="ConfiguredTaskAwaitable"/>.</summary>
         /// <remarks>This type is intended for compiler use only.</remarks>
-        public struct ConfiguredTaskAwaiter : ICriticalNotifyCompletion, IConfiguredTaskAwaiter
+        public readonly struct ConfiguredTaskAwaiter : ICriticalNotifyCompletion, IConfiguredTaskAwaiter
         {
             // WARNING: Unsafe.As is used to access the generic ConfiguredTaskAwaiter as this.
             // Its layout must remain the same.
@@ -500,7 +500,7 @@ namespace System.Runtime.CompilerServices
 
     /// <summary>Provides an awaitable object that allows for configured awaits on <see cref="System.Threading.Tasks.Task{TResult}"/>.</summary>
     /// <remarks>This type is intended for compiler use only.</remarks>
-    public struct ConfiguredTaskAwaitable<TResult>
+    public readonly struct ConfiguredTaskAwaitable<TResult>
     {
         /// <summary>The underlying awaitable on whose logic this awaitable relies.</summary>
         private readonly ConfiguredTaskAwaitable<TResult>.ConfiguredTaskAwaiter m_configuredTaskAwaiter;
@@ -524,7 +524,7 @@ namespace System.Runtime.CompilerServices
 
         /// <summary>Provides an awaiter for a <see cref="ConfiguredTaskAwaitable{TResult}"/>.</summary>
         /// <remarks>This type is intended for compiler use only.</remarks>
-        public struct ConfiguredTaskAwaiter : ICriticalNotifyCompletion, IConfiguredTaskAwaiter
+        public readonly struct ConfiguredTaskAwaiter : ICriticalNotifyCompletion, IConfiguredTaskAwaiter
         {
             // WARNING: Unsafe.As is used to access this as the non-generic ConfiguredTaskAwaiter.
             // Its layout must remain the same.
index 8a7450b..e2e4374 100644 (file)
@@ -37,7 +37,7 @@ namespace System.Runtime.CompilerServices
 
     /// <summary>Provides an awaitable context for switching into a target environment.</summary>
     /// <remarks>This type is intended for compiler use only.</remarks>
-    public struct YieldAwaitable
+    public readonly struct YieldAwaitable
     {
         /// <summary>Gets an awaiter for this <see cref="YieldAwaitable"/>.</summary>
         /// <returns>An awaiter for this awaitable.</returns>
@@ -46,7 +46,7 @@ namespace System.Runtime.CompilerServices
 
         /// <summary>Provides an awaiter that switches into a target environment.</summary>
         /// <remarks>This type is intended for compiler use only.</remarks>
-        public struct YieldAwaiter : ICriticalNotifyCompletion
+        public readonly struct YieldAwaiter : ICriticalNotifyCompletion
         {
             /// <summary>Gets whether a yield is not required.</summary>
             /// <remarks>This property is intended for compiler user rather than use directly in code.</remarks>
index 4d86881..86d5dcd 100644 (file)
@@ -27,7 +27,7 @@ namespace System.Threading
     /// </para>
     /// </remarks>
     [DebuggerDisplay("IsCancellationRequested = {IsCancellationRequested}")]
-    public struct CancellationToken
+    public readonly struct CancellationToken
     {
         private readonly static Action<object> s_actionToActionObjShunt = obj => ((Action)obj)();
 
index b3571d8..2b56bf7 100644 (file)
@@ -10,7 +10,7 @@ namespace System.Threading
     /// <remarks>
     /// To unregister a callback, dispose the corresponding Registration instance.
     /// </remarks>
-    public struct CancellationTokenRegistration : IEquatable<CancellationTokenRegistration>, IDisposable
+    public readonly struct CancellationTokenRegistration : IEquatable<CancellationTokenRegistration>, IDisposable
     {
         private readonly long _id;
         private readonly CancellationTokenSource.CallbackNode _node;
index 81c0957..b937942 100644 (file)
@@ -9,7 +9,7 @@ namespace System
     public sealed partial class TimeZoneInfo
     {
         [Serializable]
-        public struct TransitionTime : IEquatable<TransitionTime>, ISerializable, IDeserializationCallback
+        public readonly struct TransitionTime : IEquatable<TransitionTime>, ISerializable, IDeserializationCallback
         {
             private readonly DateTime _timeOfDay;
             private readonly byte _month;