Skip to content
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
Prev Previous commit
Next Next commit
PR feedback
  • Loading branch information
tlakollo committed Jul 21, 2022
commit 0f4bb8844c55eb29d7f90382ad0252e1d15a327e
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,21 @@ void CheckAndWarnOnReflectionAccess(in MessageOrigin origin, TypeSystemEntity en
}
}

var diagnosticContext = new DiagnosticContext(
if (entity.DoesMemberRequire(DiagnosticUtilities.RequiresUnreferencedCodeAttribute, out requiresAttribute) &&
!_logger.ShouldSuppressAnalysisWarningsForRequires(origin.MemberDefinition, DiagnosticUtilities.RequiresUnreferencedCodeAttribute))
{
var diagnosticContext = new DiagnosticContext(
origin,
_logger.ShouldSuppressAnalysisWarningsForRequires(origin.MemberDefinition, DiagnosticUtilities.RequiresUnreferencedCodeAttribute),
_logger.ShouldSuppressAnalysisWarningsForRequires(origin.MemberDefinition, DiagnosticUtilities.RequiresDynamicCodeAttribute),
_logger.ShouldSuppressAnalysisWarningsForRequires(origin.MemberDefinition, DiagnosticUtilities.RequiresAssemblyFilesAttribute),
_logger);
ReflectionMethodBodyScanner.CheckAndReportRequires(diagnosticContext, entity, DiagnosticUtilities.RequiresUnreferencedCodeAttribute);

string arg1 = MessageFormat.FormatRequiresAttributeMessageArg(DiagnosticUtilities.GetRequiresAttributeMessage(requiresAttribute.Value));
string arg2 = MessageFormat.FormatRequiresAttributeUrlArg(DiagnosticUtilities.GetRequiresAttributeUrl(requiresAttribute.Value));

diagnosticContext.AddDiagnostic(DiagnosticId.RequiresUnreferencedCode, entity.GetDisplayName(), arg1, arg2);
}

if (!Annotations.ShouldWarnWhenAccessedForReflection(entity))
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public MultiFileSharedCompilationModuleGroup(CompilerTypeSystemContext context,

public override bool ShouldProduceFullVTable(TypeDesc type)
{
return false;
return ConstructedEETypeNode.CreationAllowed(type);
}

public override bool ShouldPromoteToFullType(TypeDesc type)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics;

using ILCompiler.DependencyAnalysis;
using Internal.TypeSystem;
using Internal.TypeSystem.Ecma;

namespace ILCompiler
{

/// <summary>
/// Represents a non-leaf multifile compilation group where types contained in the group are always fully expanded.
/// </summary>
public class TestInfraMultiFileSharedCompilationModuleGroup : MultiFileCompilationModuleGroup
{
public TestInfraMultiFileSharedCompilationModuleGroup(CompilerTypeSystemContext context, IEnumerable<ModuleDesc> compilationModuleSet)
: base(context, compilationModuleSet)
{
}

public override bool ShouldProduceFullVTable(TypeDesc type)
{
return false;
}

public override bool ShouldPromoteToFullType(TypeDesc type)
{
return ShouldProduceFullVTable(type);
}

public override bool PresenceOfEETypeImpliesAllMethodsOnType(TypeDesc type)
{
return (type.HasInstantiation || type.IsArray) && ShouldProduceFullVTable(type) &&
type.ConvertToCanonForm(CanonicalFormKind.Specific).IsCanonicalSubtype(CanonicalFormKind.Any);
}

public override bool AllowInstanceMethodOptimization(MethodDesc method)
{
// Both the instance methods and the owning type are homed in a single compilation group
// so if we're able to generate the body, we would also generate the owning type here
// and nowhere else.
Debug.Assert(ContainsMethodBody(method, unboxingStub: false));
TypeDesc owningType = method.OwningType;
return owningType.IsDefType && !owningType.HasInstantiation && !method.HasInstantiation;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public sealed class UsageBasedMetadataManager : GeneratingMetadataManager

private readonly FeatureSwitchHashtable _featureSwitchHashtable;

private static (string AttributeName, DiagnosticId Id)[] _requiresAttributeMismatchNameAndId = new[]
{
(DiagnosticUtilities.RequiresUnreferencedCodeAttribute, DiagnosticId.RequiresUnreferencedCodeAttributeMismatch),
(DiagnosticUtilities.RequiresDynamicCodeAttribute, DiagnosticId.RequiresDynamicCodeAttributeMismatch),
(DiagnosticUtilities.RequiresAssemblyFilesAttribute, DiagnosticId.RequiresAssemblyFilesAttributeMismatch)
};

private readonly List<ModuleDesc> _modulesWithMetadata = new List<ModuleDesc>();
private readonly List<FieldDesc> _fieldsWithMetadata = new List<FieldDesc>();
private readonly List<MethodDesc> _methodsWithMetadata = new List<MethodDesc>();
Expand Down Expand Up @@ -784,26 +791,17 @@ public bool GeneratesAttributeMetadata(TypeDesc attributeType)
public override void NoteOverridingMethod(MethodDesc baseMethod, MethodDesc overridingMethod)
{
bool baseMethodTypeIsInterface = baseMethod.OwningType.IsInterface;
string overridingMethodName = overridingMethod.GetDisplayName();
string baseMethodName = baseMethod.GetDisplayName();
string[] requiresCheck = new[] { DiagnosticUtilities.RequiresUnreferencedCodeAttribute, DiagnosticUtilities.RequiresDynamicCodeAttribute, DiagnosticUtilities.RequiresAssemblyFilesAttribute };
foreach (var requiresAttribute in requiresCheck)
foreach (var requiresAttribute in _requiresAttributeMismatchNameAndId)
{
// We validate that the various dataflow/Requires* annotations are consistent across virtual method overrides
if (HasMismatchingAttributes(baseMethod, overridingMethod, requiresAttribute))
if (HasMismatchingAttributes(baseMethod, overridingMethod, requiresAttribute.AttributeName))
{
string message = MessageFormat.FormatRequiresAttributeMismatch(overridingMethod.DoesMethodRequire(requiresAttribute, out _),
baseMethodTypeIsInterface, requiresAttribute, overridingMethodName, baseMethodName);

DiagnosticId diagnosticId = requiresAttribute switch
{
DiagnosticUtilities.RequiresUnreferencedCodeAttribute => DiagnosticId.RequiresUnreferencedCodeAttributeMismatch,
DiagnosticUtilities.RequiresDynamicCodeAttribute => DiagnosticId.RequiresDynamicCodeAttributeMismatch,
DiagnosticUtilities.RequiresAssemblyFilesAttribute => DiagnosticId.RequiresAssemblyFilesAttributeMismatch,
_ => throw new NotImplementedException($"{requiresAttribute} is not a valid supported Requires attribute"),
};
string overridingMethodName = overridingMethod.GetDisplayName();
string baseMethodName = baseMethod.GetDisplayName();
string message = MessageFormat.FormatRequiresAttributeMismatch(overridingMethod.DoesMethodRequire(requiresAttribute.AttributeName, out _),
baseMethodTypeIsInterface, requiresAttribute.AttributeName, overridingMethodName, baseMethodName);

Logger.LogWarning(overridingMethod, diagnosticId, message);
Logger.LogWarning(overridingMethod, requiresAttribute.Id, message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@
<Compile Include="Compiler\IInliningPolicy.cs" />
<Compile Include="Compiler\ManifestResourceBlockingPolicy.cs" />
<Compile Include="Compiler\MethodImportationErrorProvider.cs" />
<Compile Include="Compiler\TestInfraMultiFileCompilationModuleGroup.cs" />
<Compile Include="Compiler\NoMetadataBlockingPolicy.cs" />
<Compile Include="Compiler\DependencyAnalysis\FrozenObjectNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\GCStaticsPreInitDataNode.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ public void Method1 () { }
public void Method2 () { }

// https://github.com/dotnet/linker/issues/2273
[ExpectedWarning ("IL2026", "--Method1--", ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2026", "--Method2--", ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2026", "--Method1--", ProducedBy = ProducedBy.Trimmer | ProducedBy.NativeAot)]
[ExpectedWarning ("IL2026", "--Method2--", ProducedBy = ProducedBy.Trimmer | ProducedBy.NativeAot)]
public static void Test ()
{
Type.GetType ("Mono.Linker.Tests.Cases.DataFlow." + nameof (GetTypeDataFlow) + "+" + nameof (TypeWithWarnings)).RequiresPublicMethods ();
Expand All @@ -187,7 +187,7 @@ class OverConstTypeName
public void Method1 () { }

// https://github.com/dotnet/linker/issues/2273
[ExpectedWarning ("IL2026", "--Method1--", ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2026", "--Method1--", ProducedBy = ProducedBy.Trimmer | ProducedBy.NativeAot)]
public static void Test ()
{
Type.GetType (s_ConstTypeName).RequiresPublicMethods ();
Expand Down
Loading