Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress linker warnings properly #84272

Merged
merged 7 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;

namespace System.Reflection.Emit
{
Expand All @@ -18,6 +17,8 @@ internal sealed class MethodBuilderImpl : MethodBuilder
private readonly CallingConventions _callingConventions;
private readonly TypeBuilderImpl _declaringType;

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
Justification = "Do not expect System.Void type will be trimmed from core assembly")]
internal MethodBuilderImpl(string name, MethodAttributes attributes, CallingConventions callingConventions, Type? returnType,
Type[]? parameterTypes, ModuleBuilderImpl module, TypeBuilderImpl declaringType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ internal ModuleBuilderImpl(string name, Assembly coreAssembly)
_name = name;
}

[RequiresUnreferencedCode("Types might be removed")]
internal Type GetTypeFromCoreAssembly(string name)
{
Type? type;

// TODO: Use Enum as the key for perf
if (!_coreTypes.TryGetValue(name, out type))
{
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
type = _coreAssembly.GetType(name, throwOnError: true)!;
#pragma warning restore IL2026
_coreTypes.Add(name, type);
}
jkotas marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;

Expand All @@ -18,6 +19,8 @@ internal static BlobBuilder FieldSignatureEncoder(Type fieldType)
return fieldSignature;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Do not expect System.Void type will be trimmed from core assembly")]
internal static BlobBuilder MethodSignatureEncoder(ModuleBuilderImpl _module, Type[]? parameters, Type? returnType, bool isInstance)
{
// Encoding return type and parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ protected override MethodBuilder DefineMethodCore(string name, MethodAttributes
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) => throw new NotImplementedException();
protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) => throw new NotImplementedException();

protected override void SetParentCore([DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes)(-1))] Type? parent)
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2074:DynamicallyAccessedMembers",
Justification = "No need to propogate the attriubte for called method")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Do not expect System.Object type will be trimmed from core assembly")]
protected override void SetParentCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent)
{
if (parent != null)
{
Expand All @@ -86,9 +90,7 @@ protected override void SetParentCore([DynamicallyAccessedMembers((DynamicallyAc
{
if ((_attributes & TypeAttributes.Interface) != TypeAttributes.Interface)
{
#pragma warning disable IL2074 // Value stored in field does not satisfy 'DynamicallyAccessedMembersAttribute' requirements. The return value of the source method does not have matching annotations.
_typeParent = _module.GetTypeFromCoreAssembly("System.Object");
#pragma warning restore IL2074
}
else
{
Expand Down