Skip to content

Commit

Permalink
Trim analyzer: fix member reported for generic warning (#108528)
Browse files Browse the repository at this point in the history
Fixes an incorrect member reported for warning due to RUC member
kept by new constraint. Also adds a testcase for
#108507, but doesn't fix
that issue.
  • Loading branch information
sbomer authored Oct 4, 2024
1 parent fce7c7d commit 8d85ada
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public override void Initialize (AnalysisContext context)
VerifyAttributeArguments (requiresAttribute)) {
syntaxNodeAnalysisContext.ReportDiagnostic (Diagnostic.Create (RequiresDiagnosticRule,
syntaxNodeAnalysisContext.Node.GetLocation (),
containingSymbol.GetDisplayName (),
instanceCtor.GetDisplayName (),
(string) requiresAttribute.ConstructorArguments[0].Value!,
GetUrlFromAttribute (requiresAttribute)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1305,13 +1305,17 @@ public class RequiresAll<[DynamicallyAccessedMembers (DynamicallyAccessedMemberT
{
}

class RequiresNew<T> where T : new()
{
}

[RequiresUnreferencedCode ("--ClassWithRequires--")]
public class ClassWithRequires
{
public static RequiresAll<T> field;

// Instance fields get generic warnings but static fields don't.
[UnexpectedWarning ("IL2091", Tool.Trimmer, "https://github.com/dotnet/linker/issues/3142")]
[UnexpectedWarning ("IL2091", Tool.Trimmer, "https://github.com/dotnet/runtime/issues/108523")]
public RequiresAll<T> instanceField;

[RequiresOnCtor]
Expand Down Expand Up @@ -1351,20 +1355,36 @@ public class ClassWithAttribute
}

// This warning should ideally be suppressed by the RUC on the type:
[UnexpectedWarning ("IL2091", Tool.TrimmerAnalyzerAndNativeAot, "https://github.com/dotnet/linker/issues/3142")]
[UnexpectedWarning ("IL2091", Tool.TrimmerAnalyzerAndNativeAot, "https://github.com/dotnet/runtime/issues/108523")]
[RequiresUnreferencedCode ("--GenericClassWithWarningWithRequires--")]
public class GenericClassWithWarningWithRequires<U> : RequiresAll<U>
{
}

// This warning should ideally be suppressed by the RUC on the type:
[UnexpectedWarning ("IL2091", Tool.TrimmerAnalyzerAndNativeAot, "https://github.com/dotnet/linker/issues/3142")]
[UnexpectedWarning ("IL2091", Tool.TrimmerAnalyzerAndNativeAot, "https://github.com/dotnet/runtime/issues/108523")]
[RequiresUnreferencedCode ("--ClassWithWarningWithRequires--")]
public class ClassWithWarningWithRequires : RequiresAll<T>
{
}

[UnexpectedWarning ("IL2091", Tool.TrimmerAnalyzerAndNativeAot, "https://github.com/dotnet/linker/issues/3142")]
[ExpectedWarning ("IL2026", "ClassWithRequires()", "--ClassWithRequires--")]
class ClassWithWarningOnGenericArgumentConstructor : RequiresNew<ClassWithRequires>
{
// Analyzer misses warning for implicit call to the base constructor, because the new constraint is not checked in dataflow.
[ExpectedWarning ("IL2026", Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/108507")]
public ClassWithWarningOnGenericArgumentConstructor ()
{
}
}

[UnexpectedWarning ("IL2026", Tool.TrimmerAnalyzerAndNativeAot, "https://github.com/dotnet/runtime/issues/108507")]
[RequiresUnreferencedCode ("--ClassWithWarningOnGenericArgumentConstructorWithRequires--")]
class ClassWithWarningOnGenericArgumentConstructorWithRequires : RequiresNew<ClassWithRequires>
{
}

[UnexpectedWarning ("IL2091", Tool.TrimmerAnalyzerAndNativeAot, "https://github.com/dotnet/runtime/issues/108523")]
[RequiresUnreferencedCode ("--GenericAnnotatedWithWarningWithRequires--")]
public class GenericAnnotatedWithWarningWithRequires<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TFields> : RequiresAll<TFields>
{
Expand All @@ -1376,6 +1396,7 @@ public class GenericAnnotatedWithWarningWithRequires<[DynamicallyAccessedMembers
[ExpectedWarning ("IL2026", "--ClassWithRequires--", ".ClassWithRequires", "MethodWithAttribute")]
[ExpectedWarning ("IL2026", "--GenericClassWithWarningWithRequires--")]
[ExpectedWarning ("IL2026", "--ClassWithWarningWithRequires--")]
[ExpectedWarning ("IL2026", "--ClassWithWarningOnGenericArgumentConstructorWithRequires--")]
[ExpectedWarning ("IL2026", "--GenericAnnotatedWithWarningWithRequires--")]
[ExpectedWarning ("IL2091", Tool.Trimmer, "")]
public static void Test (ClassWithRequires inst = null)
Expand All @@ -1392,7 +1413,9 @@ public static void Test (ClassWithRequires inst = null)
var d = new ClassWithRequires.ClassWithAttribute ();
var g = new GenericClassWithWarningWithRequires<int> ();
var h = new ClassWithWarningWithRequires ();
var j = new GenericAnnotatedWithWarningWithRequires<int> ();
var j = new ClassWithWarningOnGenericArgumentConstructor ();
var k = new ClassWithWarningOnGenericArgumentConstructorWithRequires ();
var l = new GenericAnnotatedWithWarningWithRequires<int> ();
}
}

Expand Down

0 comments on commit 8d85ada

Please sign in to comment.