Skip to content

Commit

Permalink
Remove incorrect suppressions from AwaitableInfo (#56360)
Browse files Browse the repository at this point in the history
These warning suppressions are not correct. The reflection usage in these methods isn't compatible with trimming. Marking the methods correctly with RequiresUnreferencedCode.

See dotnet/runtime#103258 for more info.
  • Loading branch information
eerhardt authored Jun 22, 2024
1 parent 28481ab commit 96559b6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Shared/ObjectMethodExecutor/AwaitableInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Microsoft.Extensions.Internal;

internal readonly struct AwaitableInfo
{
internal const string RequiresUnreferencedCodeMessage = "Uses unbounded reflection to determine awaitability of types.";

private const BindingFlags Everything = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
private static readonly MethodInfo INotifyCompletion_OnCompleted = typeof(INotifyCompletion).GetMethod(nameof(INotifyCompletion.OnCompleted), Everything, new[] { typeof(Action) })!;
private static readonly MethodInfo ICriticalNotifyCompletion_UnsafeOnCompleted = typeof(ICriticalNotifyCompletion).GetMethod(nameof(ICriticalNotifyCompletion.UnsafeOnCompleted), Everything, new[] { typeof(Action) })!;
Expand Down Expand Up @@ -41,8 +43,7 @@ public AwaitableInfo(
GetAwaiterMethod = getAwaiterMethod;
}

[UnconditionalSuppressMessage("Trimmer", "IL2070", Justification = "Reflecting over the async Task types contract")]
[UnconditionalSuppressMessage("Trimmer", "IL2075", Justification = "Reflecting over the async Task types contract")]
[RequiresUnreferencedCode(RequiresUnreferencedCodeMessage)]
public static bool IsTypeAwaitable(
Type type,
out AwaitableInfo awaitableInfo)
Expand Down
1 change: 1 addition & 0 deletions src/Shared/ObjectMethodExecutor/CoercedAwaitableInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public CoercedAwaitableInfo(Expression coercerExpression, Type coercerResultType
AwaitableInfo = coercedAwaitableInfo;
}

[RequiresUnreferencedCode(AwaitableInfo.RequiresUnreferencedCodeMessage)]
[RequiresDynamicCode("Dynamically generates calls to FSharpAsync.")]
public static bool IsTypeAwaitable(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] Type type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal static class ObjectMethodExecutorFSharpSupport
/// by the coercer expression, if it was possible to build a coercer; otherwise, <see langword="null"/>.
/// </param>
/// <returns><see langword="true"/> if it was possible to build a coercer; otherwise, <see langword="false"/>.</returns>
[UnconditionalSuppressMessage("Trimmer", "IL2060", Justification = "Reflecting over the async FSharpAsync<> contract.")]
[RequiresUnreferencedCode("Reflecting over the async FSharpAsync<> contract.")]
public static bool TryBuildCoercerFromFSharpAsyncToAwaitable(
Type possibleFSharpAsyncType,
out Expression coerceToAwaitableExpression,
Expand Down Expand Up @@ -127,7 +127,7 @@ public static bool TryBuildCoercerFromFSharpAsyncToAwaitable(
/// otherwise, <see langword="null"/>.
/// </param>
/// <returns><see langword="true"/> if it was possible to build a coercer; otherwise, <see langword="false"/>.</returns>
[UnconditionalSuppressMessage("Trimmer", "IL2060", Justification = "Reflecting over FSharp.Core.Unit.")]
[RequiresUnreferencedCode("Reflecting over FSharp.Core.Unit.")]
public static bool TryBuildCoercerFromUnitAwaitableToVoidAwaitable(
Type genericAwaitableType,
out Expression coercerExpression,
Expand Down Expand Up @@ -168,12 +168,15 @@ static Expression MakeValueTaskOfUnitToValueTaskExpression(Type type)
}
}

[RequiresUnreferencedCode("Reflecting over the async FSharpAsync<> contract.")]
private static bool IsFSharpAsyncOpenGenericType(Type possibleFSharpAsyncType) =>
IsCoerceableFSharpType(possibleFSharpAsyncType, FSharpAsyncGenericTypeName);

[RequiresUnreferencedCode("Reflecting over the async FSharpAsync<> contract.")]
private static bool IsFSharpUnit(Type possibleFSharpUnitType) =>
IsCoerceableFSharpType(possibleFSharpUnitType, FSharpUnitTypeName);

[RequiresUnreferencedCode("Reflecting over the async FSharpAsync<> contract.")]
private static bool IsCoerceableFSharpType(Type possibleFSharpType, string coerceableFSharpTypeName)
{
var typeFullName = possibleFSharpType?.FullName;
Expand All @@ -199,9 +202,7 @@ private static bool IsCoerceableFSharpType(Type possibleFSharpType, string coerc
}
}

[UnconditionalSuppressMessage("Trimmer", "IL2026", Justification = "Reflecting over the async FSharpAsync<> contract")]
[UnconditionalSuppressMessage("Trimmer", "IL2055", Justification = "Reflecting over the async FSharpAsync<> contract")]
[UnconditionalSuppressMessage("Trimmer", "IL2072", Justification = "Reflecting over the async FSharpAsync<> contract")]
[RequiresUnreferencedCode("Reflecting over the async FSharpAsync<> contract.")]
private static bool TryPopulateFSharpValueCaches(Type possibleFSharpType)
{
var assembly = possibleFSharpType.Assembly;
Expand Down

0 comments on commit 96559b6

Please sign in to comment.