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

Warn on RUC annotated ctors invoked through new() constraint in analyzer #2254

Merged
merged 5 commits into from
Sep 8, 2021
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
13 changes: 13 additions & 0 deletions src/ILLink.RoslynAnalyzer/ISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ internal static bool HasAttribute (this ISymbol symbol, string attributeName)
return false;
}

internal static bool TryGetAttribute (this ISymbol member, string attributeName, [NotNullWhen (returnValue: true)] out AttributeData? attribute)
{
attribute = null;
foreach (var attr in member.GetAttributes ()) {
if (attr.AttributeClass is { } attrClass && attrClass.HasName (attributeName)) {
attribute = attr;
return true;
}
}

return false;
}

internal static bool TryGetOverriddenMember (this ISymbol? symbol, [NotNullWhen (returnValue: true)] out ISymbol? overridenMember)
{
overridenMember = symbol switch {
Expand Down
2 changes: 1 addition & 1 deletion src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private void ReportMismatchInAttributesDiagnostic (SymbolAnalysisContext symbolA

protected abstract string GetMessageFromAttribute (AttributeData requiresAttribute);

private string GetUrlFromAttribute (AttributeData? requiresAttribute)
public static string GetUrlFromAttribute (AttributeData? requiresAttribute)
{
var url = requiresAttribute?.NamedArguments.FirstOrDefault (na => na.Key == "Url").Value.Value?.ToString ();
return MessageFormat.FormatRequiresAttributeUrlArg (url);
Expand Down
44 changes: 42 additions & 2 deletions src/ILLink.RoslynAnalyzer/RequiresUnreferencedCodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using ILLink.Shared;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

namespace ILLink.RoslynAnalyzer
{
Expand All @@ -31,6 +32,38 @@ public sealed class RequiresUnreferencedCodeAnalyzer : RequiresAnalyzerBase
operationContext.Operation.Syntax.GetLocation ()));
};

static readonly Action<OperationAnalysisContext> s_constructorConstraint = operationContext => {
var invocationOperation = (IInvocationOperation) operationContext.Operation;
if (FindContainingSymbol (operationContext, DiagnosticTargets.All) is not ISymbol containingSymbol ||
containingSymbol.HasAttribute (RequiresUnreferencedCodeAttribute))
return;

var targetMethod = invocationOperation.TargetMethod;
if (!targetMethod.IsGenericMethod)
return;

for (int i = 0; i < targetMethod.TypeParameters.Length; i++) {
var typeParameter = targetMethod.TypeParameters[i];
var typeArgument = targetMethod.TypeArguments[i];
if (!typeParameter.HasConstructorConstraint)
continue;

var typeArgCtors = ((INamedTypeSymbol) typeArgument).InstanceConstructors;
foreach (var instanceCtor in typeArgCtors) {
if (instanceCtor.Arity > 0)
continue;

if (instanceCtor.TryGetAttribute (RequiresUnreferencedCodeAttribute, out var requiresUnreferencedCodeAttribute)) {
operationContext.ReportDiagnostic (Diagnostic.Create (s_requiresUnreferencedCodeRule,
operationContext.Operation.Syntax.GetLocation (),
containingSymbol.GetDisplayName (),
(string) requiresUnreferencedCodeAttribute.ConstructorArguments[0].Value!,
GetUrlFromAttribute (requiresUnreferencedCodeAttribute)));
}
}
}
};

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create (s_dynamicTypeInvocationRule, s_requiresUnreferencedCodeRule, s_requiresUnreferencedCodeAttributeMismatch);

Expand All @@ -47,8 +80,15 @@ public sealed class RequiresUnreferencedCodeAnalyzer : RequiresAnalyzerBase
protected override bool IsAnalyzerEnabled (AnalyzerOptions options, Compilation compilation) =>
options.IsMSBuildPropertyValueTrue (MSBuildPropertyOptionNames.EnableTrimAnalyzer, compilation);

private protected override ImmutableArray<(Action<OperationAnalysisContext> Action, OperationKind[] OperationKind)> ExtraOperationActions =>
ImmutableArray.Create ((s_dynamicTypeInvocation, new OperationKind[] { OperationKind.DynamicInvocation }));
private protected override ImmutableArray<(Action<OperationAnalysisContext> Action, OperationKind[] OperationKind)> ExtraOperationActions {
get {
var diagsBuilder = ImmutableArray.CreateBuilder<(Action<OperationAnalysisContext>, OperationKind[])> ();
diagsBuilder.Add ((s_dynamicTypeInvocation, new OperationKind[] { OperationKind.DynamicInvocation }));
diagsBuilder.Add ((s_constructorConstraint, new OperationKind[] { OperationKind.Invocation }));

return diagsBuilder.ToImmutable ();
}
}

protected override bool VerifyAttributeArguments (AttributeData attribute) =>
attribute.ConstructorArguments.Length >= 1 && attribute.ConstructorArguments[0] is { Type: { SpecialType: SpecialType.System_String } } ctorArg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,7 @@ public NewConstrainTestType () { }

static void GenericMethod<T> () where T : new() { }

// https://github.com/mono/linker/issues/2117
[ExpectedWarning ("IL2026", "--NewConstrainTestType.ctor--", GlobalAnalysisOnly = true)]
[ExpectedWarning ("IL2026", "--NewConstrainTestType.ctor--")]
mateoatr marked this conversation as resolved.
Show resolved Hide resolved
public static void Test ()
{
GenericMethod<NewConstrainTestType> ();
Expand Down