Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Nov 7, 2023
1 parent e364d18 commit c003fb0
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 78 deletions.
4 changes: 3 additions & 1 deletion contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Example:

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;

namespace System.Runtime.CompilerServices;

Expand All @@ -158,6 +159,7 @@ namespace System.Runtime.CompilerServices;
/// The specification for module initializers in the .NET runtime can be found here:
/// https://github.com/dotnet/runtime/blob/master/docs/design/specs/Ecma-335-Augments.md#module-initializer
/// </remarks>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.moduleinitializerattribute?view=net-7.0")]
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(
Expand All @@ -173,7 +175,7 @@ sealed class ModuleInitializerAttribute :

#endif
```
<sup><a href='/src/Polyfill/ModuleInitializerAttribute.cs#L1-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-ModuleInitializerAttribute.cs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Polyfill/ModuleInitializerAttribute.cs#L1-L45' title='Snippet source file'>snippet source</a> | <a href='#snippet-ModuleInitializerAttribute.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void CancellationTokenUnsafeRegister()
{
var source = new CancellationTokenSource();
var token = source.Token;
token.UnsafeRegister((state) => {}, null);
token.UnsafeRegister(state => {}, null);
token.UnsafeRegister((state, token) => {}, null);
}

Expand Down
14 changes: 14 additions & 0 deletions src/Polyfill/CallerArgumentExpressionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;

namespace System.Runtime.CompilerServices;

/// <summary>
/// Indicates that a parameter captures the expression passed for another parameter as a string.
/// </summary>
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(AttributeTargets.Parameter)]
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerargumentexpressionattribute")]
#if PolyPublic
public
#endif
sealed class CallerArgumentExpressionAttribute :
Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="CallerArgumentExpressionAttribute"/> class.
/// </summary>
/// <param name="parameterName">
/// The name of the parameter whose expression should be captured as a string.
/// </param>
public CallerArgumentExpressionAttribute(string parameterName) =>
ParameterName = parameterName;

/// <summary>
/// Gets the name of the parameter whose expression should be captured as a string.
/// </summary>
public string ParameterName { get; }
}

Expand Down
3 changes: 3 additions & 0 deletions src/Polyfill/CompilerFeatureRequiredAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;

namespace System.Runtime.CompilerServices;

Expand All @@ -16,6 +17,7 @@ namespace System.Runtime.CompilerServices;
validOn: AttributeTargets.All,
AllowMultiple = true,
Inherited = false)]
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.compilerfeaturerequiredattribute")]
#if PolyPublic
public
#endif
Expand All @@ -25,6 +27,7 @@ sealed class CompilerFeatureRequiredAttribute :
/// <summary>
/// Initialize a new instance of <see cref="CompilerFeatureRequiredAttribute"/>
/// </summary>
/// <param name="featureName">The name of the required compiler feature.</param>
public CompilerFeatureRequiredAttribute(string featureName) =>
FeatureName = featureName;

Expand Down
2 changes: 2 additions & 0 deletions src/Polyfill/DisableRuntimeMarshallingAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;

namespace System.Runtime.CompilerServices;

Expand All @@ -27,6 +28,7 @@ namespace System.Runtime.CompilerServices;
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(AttributeTargets.Assembly)]
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.disableruntimemarshallingattribute")]
#if PolyPublic
public
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/Polyfill/InterpolatedStringHandlerArgumentAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;

namespace System.Runtime.CompilerServices;

Expand All @@ -13,6 +14,7 @@ namespace System.Runtime.CompilerServices;
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(AttributeTargets.Parameter)]
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.interpolatedstringhandlerargumentattribute")]
#if PolyPublic
public
#endif
Expand Down
6 changes: 5 additions & 1 deletion src/Polyfill/InterpolatedStringHandlerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;

namespace System.Runtime.CompilerServices;

using Targets = AttributeTargets;

/// <summary>Indicates the attributed type is to be used as an interpolated string handler.</summary>
/// <summary>
/// Indicates the attributed type is to be used as an interpolated string handler.
/// </summary>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.interpolatedstringhandlerargumentattribute")]
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(
Expand Down
2 changes: 2 additions & 0 deletions src/Polyfill/ModuleInitializerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;

namespace System.Runtime.CompilerServices;

Expand All @@ -27,6 +28,7 @@ namespace System.Runtime.CompilerServices;
/// The specification for module initializers in the .NET runtime can be found here:
/// https://github.com/dotnet/runtime/blob/master/docs/design/specs/Ecma-335-Augments.md#module-initializer
/// </remarks>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.moduleinitializerattribute?view=net-7.0")]
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(
Expand Down
3 changes: 2 additions & 1 deletion src/Polyfill/PolyfillExtensions_IEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ static IEnumerable<TSource[]> ChunkIterator<TSource>(IEnumerable<TSource> source
{
// For all but the first chunk, the array will already be correctly sized.
// We can just store into it until either it's full or MoveNext returns false.
TSource[] local = array; // avoid bounds checks by using cached local (`array` is lifted to iterator object as a field)
// avoid bounds checks by using cached local (`array` is lifted to iterator object as a field)
TSource[] local = array;
for (; (uint)i < (uint)local.Length && e.MoveNext(); i++)
{
local[i] = e.Current;
Expand Down
16 changes: 8 additions & 8 deletions src/Polyfill/PolyfillExtensions_Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public static async Task WaitAsync(
this Task target,
TimeSpan timeout)
{
var cancellationSource = new CancellationTokenSource();
var cancelSource = new CancellationTokenSource();
try
{
await target.WaitAsync(timeout, cancellationSource.Token);
await target.WaitAsync(timeout, cancelSource.Token);
}
finally
{
cancellationSource.Cancel();
cancellationSource.Dispose();
cancelSource.Cancel();
cancelSource.Dispose();
}
}

Expand Down Expand Up @@ -60,15 +60,15 @@ public static async Task<TResult> WaitAsync<TResult>(
this Task<TResult> target,
TimeSpan timeout)
{
var cancellationSource = new CancellationTokenSource();
var cancelSource = new CancellationTokenSource();
try
{
return await target.WaitAsync(timeout, cancellationSource.Token);
return await target.WaitAsync(timeout, cancelSource.Token);
}
finally
{
cancellationSource.Cancel();
cancellationSource.Dispose();
cancelSource.Cancel();
cancelSource.Dispose();
}
}

Expand Down
62 changes: 34 additions & 28 deletions src/Tests/PolyfillExtensionsTests_CancellationToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ public void CancellationToken_Register_Exceptions()
CancellationToken token = default;

#nullable disable
Assert.Throws<ArgumentNullException>(() => token.Register((Action<object, CancellationToken>)null, null));
Assert.Throws<ArgumentNullException>(() => token.Register((Action<object, CancellationToken>) null, null));

Assert.Throws<ArgumentNullException>(() => token.UnsafeRegister((Action<object>)null, null));
Assert.Throws<ArgumentNullException>(() => token.UnsafeRegister((Action<object, CancellationToken>)null, null));
// ReSharper disable once RedundantCast
Assert.Throws<ArgumentNullException>(() => token.UnsafeRegister((Action<object>) null, null));
Assert.Throws<ArgumentNullException>(() => token.UnsafeRegister((Action<object, CancellationToken>) null, null));
#nullable enable
}

Expand All @@ -18,52 +19,57 @@ public void CancellationToken_Register_Exceptions()
[TestCase(true)]
public static void CancellationToken_Register_ExecutionContextFlowsIfExpected(bool callbackWithToken)
{
var cts = new CancellationTokenSource();
var cancelSource = new CancellationTokenSource();

const int Iters = 5;
int invoked = 0;
const int iterations = 5;
var invoked = 0;

AsyncLocal<int> al = new AsyncLocal<int>();
for (int i = 1; i <= Iters; i++)
var asyncLocal = new AsyncLocal<int>();
for (var i = 1; i <= iterations; i++)
{
bool flowExecutionContext = i % 2 == 0;
var flowExecutionContext = i % 2 == 0;

al.Value = i;
asyncLocal.Value = i;
Action<object?> callback = s =>
{
invoked++;
Assert.AreEqual(flowExecutionContext ? (int)s! : 0, al.Value);
Assert.AreEqual(flowExecutionContext ? (int) s! : 0, asyncLocal.Value);
};

CancellationToken ct = cts.Token;
var token = cancelSource.Token;
if (flowExecutionContext && callbackWithToken)
{
ct.Register((s, t) =>
{
Assert.AreEqual(ct, t);
callback(s);
}, i);
token.Register(
(s, t) =>
{
Assert.AreEqual(token, t);
callback(s);
},
i);
}
else if (flowExecutionContext)
{
ct.Register(callback, i);
token.Register(callback, i);
}
else if (callbackWithToken)
{
ct.UnsafeRegister((s, t) =>
{
Assert.AreEqual(ct, t);
callback(s);
}, i);
token.UnsafeRegister(
(s, t) =>
{
Assert.AreEqual(token, t);
callback(s);
},
i);
}
else
{
ct.UnsafeRegister(callback, i);
token.UnsafeRegister(callback, i);
}
}
al.Value = 0;

cts.Cancel();
Assert.AreEqual(Iters, invoked);
asyncLocal.Value = 0;

cancelSource.Cancel();
Assert.AreEqual(iterations, invoked);
}
}
}
Loading

0 comments on commit c003fb0

Please sign in to comment.