From: Stephen Toub Date: Fri, 3 Nov 2017 11:45:14 +0000 (-0400) Subject: Mark applicable structs as readonly (#14789) X-Git-Tag: accepted/tizen/base/20180629.140029~693 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=278c8f3f04dc120c07face13221b70b1116f4552;p=platform%2Fupstream%2Fcoreclr.git Mark applicable structs as readonly (#14789) In a few cases (e.g. nullable), I added readonly to fields in order to allow readonly on the type. --- diff --git a/src/mscorlib/shared/System/Collections/Generic/KeyValuePair.cs b/src/mscorlib/shared/System/Collections/Generic/KeyValuePair.cs index aeafecd..82c786d 100644 --- a/src/mscorlib/shared/System/Collections/Generic/KeyValuePair.cs +++ b/src/mscorlib/shared/System/Collections/Generic/KeyValuePair.cs @@ -47,10 +47,10 @@ namespace System.Collections.Generic // and IReadOnlyDictionary. [Serializable] [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct KeyValuePair + public readonly struct KeyValuePair { - 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) { diff --git a/src/mscorlib/shared/System/DateTime.cs b/src/mscorlib/shared/System/DateTime.cs index ad4d3a0..d3720b2 100644 --- a/src/mscorlib/shared/System/DateTime.cs +++ b/src/mscorlib/shared/System/DateTime.cs @@ -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, IEquatable, ISerializable + public readonly partial struct DateTime : IComparable, IFormattable, IConvertible, IComparable, IEquatable, 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 diff --git a/src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs b/src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs index 854bc06..cff424d 100644 --- a/src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs +++ b/src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs @@ -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 { /// /// Union of well-known value types, to avoid boxing those types. diff --git a/src/mscorlib/shared/System/Globalization/DaylightTime.cs b/src/mscorlib/shared/System/Globalization/DaylightTime.cs index 10f074d..e6920b3 100644 --- a/src/mscorlib/shared/System/Globalization/DaylightTime.cs +++ b/src/mscorlib/shared/System/Globalization/DaylightTime.cs @@ -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) { diff --git a/src/mscorlib/shared/System/ParamsArray.cs b/src/mscorlib/shared/System/ParamsArray.cs index 1c73bc5..043ee67 100644 --- a/src/mscorlib/shared/System/ParamsArray.cs +++ b/src/mscorlib/shared/System/ParamsArray.cs @@ -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, diff --git a/src/mscorlib/shared/System/Reflection/ParameterModifier.cs b/src/mscorlib/shared/System/Reflection/ParameterModifier.cs index 640fee2..0fb75ff 100644 --- a/src/mscorlib/shared/System/Reflection/ParameterModifier.cs +++ b/src/mscorlib/shared/System/Reflection/ParameterModifier.cs @@ -4,7 +4,7 @@ namespace System.Reflection { - public struct ParameterModifier + public readonly struct ParameterModifier { private readonly bool[] _byRef; diff --git a/src/mscorlib/shared/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs b/src/mscorlib/shared/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs index f710678..b4efc73 100644 --- a/src/mscorlib/shared/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs +++ b/src/mscorlib/shared/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs @@ -11,7 +11,7 @@ namespace System.Runtime.CompilerServices /// Provides an awaitable type that enables configured awaits on a . /// The type of the result produced. [StructLayout(LayoutKind.Auto)] - public struct ConfiguredValueTaskAwaitable + public readonly struct ConfiguredValueTaskAwaitable { /// The wrapped . private readonly ValueTask _value; diff --git a/src/mscorlib/shared/System/Runtime/Serialization/StreamingContext.cs b/src/mscorlib/shared/System/Runtime/Serialization/StreamingContext.cs index 4fe90ca..cdcb1c3 100644 --- a/src/mscorlib/shared/System/Runtime/Serialization/StreamingContext.cs +++ b/src/mscorlib/shared/System/Runtime/Serialization/StreamingContext.cs @@ -4,7 +4,7 @@ namespace System.Runtime.Serialization { - public struct StreamingContext + public readonly struct StreamingContext { private readonly object _additionalContext; private readonly StreamingContextStates _state; diff --git a/src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs b/src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs index 4ccf0f8..de9b016 100644 --- a/src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs +++ b/src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs @@ -50,7 +50,7 @@ namespace System.Threading.Tasks /// [AsyncMethodBuilder(typeof(AsyncValueTaskMethodBuilder<>))] [StructLayout(LayoutKind.Auto)] - public struct ValueTask : IEquatable> + public readonly struct ValueTask : IEquatable> { /// The task to be used if the operation completed asynchronously or if it completed synchronously but non-successfully. internal readonly Task _task; diff --git a/src/mscorlib/src/System/ArraySegment.cs b/src/mscorlib/src/System/ArraySegment.cs index a96db8f..251dee8 100644 --- a/src/mscorlib/src/System/ArraySegment.cs +++ b/src/mscorlib/src/System/ArraySegment.cs @@ -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 : IList, IReadOnlyList + public readonly struct ArraySegment : IList, IReadOnlyList { // 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 for new type parameters. diff --git a/src/mscorlib/src/System/DateTime.CoreCLR.cs b/src/mscorlib/src/System/DateTime.CoreCLR.cs index 4d36b99..84cda1d 100644 --- a/src/mscorlib/src/System/DateTime.CoreCLR.cs +++ b/src/mscorlib/src/System/DateTime.CoreCLR.cs @@ -6,7 +6,7 @@ using System.Runtime.CompilerServices; namespace System { - public partial struct DateTime + public readonly partial struct DateTime { public static DateTime UtcNow { diff --git a/src/mscorlib/src/System/Nullable.cs b/src/mscorlib/src/System/Nullable.cs index f9ee9ac..159453d 100644 --- a/src/mscorlib/src/System/Nullable.cs +++ b/src/mscorlib/src/System/Nullable.cs @@ -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 where T : struct + public readonly struct Nullable 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) diff --git a/src/mscorlib/src/System/Reflection/Emit/MethodBuilder.cs b/src/mscorlib/src/System/Reflection/Emit/MethodBuilder.cs index 928b2e3..daa6d76 100644 --- a/src/mscorlib/src/System/Reflection/Emit/MethodBuilder.cs +++ b/src/mscorlib/src/System/Reflection/Emit/MethodBuilder.cs @@ -1103,7 +1103,7 @@ namespace System.Reflection.Emit /// Describes exception handler in a method body. /// [StructLayout(LayoutKind.Sequential)] - internal struct ExceptionHandler : IEquatable + internal readonly struct ExceptionHandler : IEquatable { // Keep in sync with unmanged structure. internal readonly int m_exceptionClass; diff --git a/src/mscorlib/src/System/Runtime/CompilerServices/TaskAwaiter.cs b/src/mscorlib/src/System/Runtime/CompilerServices/TaskAwaiter.cs index 5b2ac28..5fb4371 100644 --- a/src/mscorlib/src/System/Runtime/CompilerServices/TaskAwaiter.cs +++ b/src/mscorlib/src/System/Runtime/CompilerServices/TaskAwaiter.cs @@ -55,7 +55,7 @@ namespace System.Runtime.CompilerServices { /// Provides an awaiter for awaiting a . /// This type is intended for compiler use only. - 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 /// Provides an awaiter for awaiting a . /// This type is intended for compiler use only. - public struct TaskAwaiter : ICriticalNotifyCompletion, ITaskAwaiter + public readonly struct TaskAwaiter : 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 /// Provides an awaitable object that allows for configured awaits on . /// This type is intended for compiler use only. - public struct ConfiguredTaskAwaitable + public readonly struct ConfiguredTaskAwaitable { /// The task being awaited. private readonly ConfiguredTaskAwaitable.ConfiguredTaskAwaiter m_configuredTaskAwaiter; @@ -434,7 +434,7 @@ namespace System.Runtime.CompilerServices /// Provides an awaiter for a . /// This type is intended for compiler use only. - 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 /// Provides an awaitable object that allows for configured awaits on . /// This type is intended for compiler use only. - public struct ConfiguredTaskAwaitable + public readonly struct ConfiguredTaskAwaitable { /// The underlying awaitable on whose logic this awaitable relies. private readonly ConfiguredTaskAwaitable.ConfiguredTaskAwaiter m_configuredTaskAwaiter; @@ -524,7 +524,7 @@ namespace System.Runtime.CompilerServices /// Provides an awaiter for a . /// This type is intended for compiler use only. - 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. diff --git a/src/mscorlib/src/System/Runtime/CompilerServices/YieldAwaitable.cs b/src/mscorlib/src/System/Runtime/CompilerServices/YieldAwaitable.cs index 8a7450b..e2e4374 100644 --- a/src/mscorlib/src/System/Runtime/CompilerServices/YieldAwaitable.cs +++ b/src/mscorlib/src/System/Runtime/CompilerServices/YieldAwaitable.cs @@ -37,7 +37,7 @@ namespace System.Runtime.CompilerServices /// Provides an awaitable context for switching into a target environment. /// This type is intended for compiler use only. - public struct YieldAwaitable + public readonly struct YieldAwaitable { /// Gets an awaiter for this . /// An awaiter for this awaitable. @@ -46,7 +46,7 @@ namespace System.Runtime.CompilerServices /// Provides an awaiter that switches into a target environment. /// This type is intended for compiler use only. - public struct YieldAwaiter : ICriticalNotifyCompletion + public readonly struct YieldAwaiter : ICriticalNotifyCompletion { /// Gets whether a yield is not required. /// This property is intended for compiler user rather than use directly in code. diff --git a/src/mscorlib/src/System/Threading/CancellationToken.cs b/src/mscorlib/src/System/Threading/CancellationToken.cs index 4d86881..86d5dcd 100644 --- a/src/mscorlib/src/System/Threading/CancellationToken.cs +++ b/src/mscorlib/src/System/Threading/CancellationToken.cs @@ -27,7 +27,7 @@ namespace System.Threading /// /// [DebuggerDisplay("IsCancellationRequested = {IsCancellationRequested}")] - public struct CancellationToken + public readonly struct CancellationToken { private readonly static Action s_actionToActionObjShunt = obj => ((Action)obj)(); diff --git a/src/mscorlib/src/System/Threading/CancellationTokenRegistration.cs b/src/mscorlib/src/System/Threading/CancellationTokenRegistration.cs index b3571d8..2b56bf7 100644 --- a/src/mscorlib/src/System/Threading/CancellationTokenRegistration.cs +++ b/src/mscorlib/src/System/Threading/CancellationTokenRegistration.cs @@ -10,7 +10,7 @@ namespace System.Threading /// /// To unregister a callback, dispose the corresponding Registration instance. /// - public struct CancellationTokenRegistration : IEquatable, IDisposable + public readonly struct CancellationTokenRegistration : IEquatable, IDisposable { private readonly long _id; private readonly CancellationTokenSource.CallbackNode _node; diff --git a/src/mscorlib/src/System/TimeZoneInfo.TransitionTime.cs b/src/mscorlib/src/System/TimeZoneInfo.TransitionTime.cs index 81c0957..b937942 100644 --- a/src/mscorlib/src/System/TimeZoneInfo.TransitionTime.cs +++ b/src/mscorlib/src/System/TimeZoneInfo.TransitionTime.cs @@ -9,7 +9,7 @@ namespace System public sealed partial class TimeZoneInfo { [Serializable] - public struct TransitionTime : IEquatable, ISerializable, IDeserializationCallback + public readonly struct TransitionTime : IEquatable, ISerializable, IDeserializationCallback { private readonly DateTime _timeOfDay; private readonly byte _month;