Skip to content

Commit

Permalink
Reapply "Fix missing analyzer warning for generics in containing type (
Browse files Browse the repository at this point in the history
…dotnet#109392)" (dotnet#109520)

This reverts commit 10af5e0.
  • Loading branch information
sbomer committed Nov 4, 2024
1 parent 10af5e0 commit 3b24432
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ internal static class GenericArgumentDataFlow
{
public static void ProcessGenericArgumentDataFlow (Location location, INamedTypeSymbol type, Action<Diagnostic> reportDiagnostic)
{
ProcessGenericArgumentDataFlow (location, type.TypeArguments, type.TypeParameters, reportDiagnostic);
while (type is { IsGenericType: true }) {
ProcessGenericArgumentDataFlow (location, type.TypeArguments, type.TypeParameters, reportDiagnostic);
type = type.ContainingType;
}
}

public static void ProcessGenericArgumentDataFlow (Location location, IMethodSymbol method, Action<Diagnostic> reportDiagnostic)
Expand Down Expand Up @@ -55,7 +58,7 @@ static void ProcessGenericArgumentDataFlow (

public static bool RequiresGenericArgumentDataFlow (INamedTypeSymbol type)
{
if (type.IsGenericType) {
while (type is { IsGenericType: true }) {
if (RequiresGenericArgumentDataFlow (type.TypeParameters))
return true;

Expand All @@ -64,6 +67,8 @@ public static bool RequiresGenericArgumentDataFlow (INamedTypeSymbol type)
&& RequiresGenericArgumentDataFlow (namedTypeSymbol))
return true;
}

type = type.ContainingType;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public Task RequiresInLibraryAssembly ()
return RunTest ();
}

[Fact]
public Task RequiresInRootAllAssembly ()
{
return RunTest ();
}

[Fact]
public Task RequiresOnAttribute ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public Task MethodByRefParameterDataFlow ()
return RunTest (allowMissingWarnings: true);
}

[Fact]
public Task ModifierDataFlow ()
{
return RunTest (allowMissingWarnings: true);
}

[Fact]
public Task StaticInterfaceMethodDataflow ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static void Main ()

TestNoWarningsInRUCMethod<TestType> ();
TestNoWarningsInRUCType<TestType, TestType> ();
TestGenericParameterFlowsToNestedType.Test ();
}

static void TestSingleGenericParameterOnType ()
Expand Down Expand Up @@ -849,6 +850,59 @@ static void TestNoWarningsInRUCType<T, U> ()
rucType.VirtualMethodRequiresPublicMethods<T> ();
}

class TestGenericParameterFlowsToNestedType
{
class Generic<T> {
[ExpectedWarning ("IL2091")]
public T CallNestedMethod () => GenericRequires<T>.Nested.Method ();

[ExpectedWarning ("IL2091")]
public T AccessNestedField () => GenericRequires<T>.Nested.Field;

[ExpectedWarning ("IL2091")]
public T AccessNestedProperty () => GenericRequires<T>.Nested.Property;

[ExpectedWarning ("IL2091")]
public void AccessNestedEvent () => GenericRequires<T>.Nested.Event += null;

[ExpectedWarning ("IL2091")]
public void UseNestedTypeArgument () {
new Generic<GenericRequires<T>.Nested> ();
}

[ExpectedWarning ("IL2091")]
public class DerivedFromNestedType : GenericRequires<T>.Nested
{
[ExpectedWarning ("IL2091")]
public DerivedFromNestedType () {
}
}
}

class GenericRequires<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T> {
public class Nested {
public static T? Method () => default;

public static T? Field = default;

public static T? Property { get; set; } = default;

public static event Action<T>? Event;
}
}

public static void Test ()
{
var instance = new Generic<string> ();
instance.CallNestedMethod ();
instance.AccessNestedField ();
instance.AccessNestedProperty ();
instance.AccessNestedEvent ();
instance.UseNestedTypeArgument ();
new Generic<string>.DerivedFromNestedType ();
}
}

[RequiresUnreferencedCode ("message")]
public class RUCTypeRequiresPublicFields<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T>
Expand Down

0 comments on commit 3b24432

Please sign in to comment.