Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,65 @@
// 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.Diagnostics.CodeAnalysis;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.DataFlow
{
[SkipKeptItemsValidation]
[ExpectedNoWarnings]
class MethodByRefParameterDataFlow
{
public static void Main ()
{
Type typeWithMethods = _fieldWithMethods;

TestAssignStaticToAnnotatedRefParameter (ref typeWithMethods);
TestAssignParameterToAnnotatedRefParameter (ref typeWithMethods, typeof (TestType));
}

[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static Type _fieldWithMethods = null;

[ExpectedWarning ("IL2026", "Message for --TestType.Requires--")]

// https://github.com/dotnet/linker/issues/2158
// The type.GetMethods call generates a warning because we're not able to correctly track the value of the "this".
// (there's a ldind.ref insruction here which we currently don't handle and the "this" becomes unknown)
[ExpectedWarning ("IL2065")]
static void TestAssignStaticToAnnotatedRefParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type type)
{
type = typeof (TestTypeWithRequires);
type.GetMethods (); // Should not warn
}

// The warning message is REALLY confusing (basically wrong) since it talks about "calling the method with wrong argument"
// which is definitely not the case here.
[ExpectedWarning ("IL2067", "typeWithFields")]

// https://github.com/dotnet/linker/issues/2158
// The type.GetMethods call generates a warning because we're not able to correctly track the value of the "this".
// (there's a ldind.ref insruction here which we currently don't handle and the "this" becomes unknown)
[ExpectedWarning ("IL2065")]
static void TestAssignParameterToAnnotatedRefParameter (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type type,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type typeWithFields)
{
type = typeWithFields; // Should warn
type.GetMethods (); // Should not warn
}

class TestTypeWithRequires
{
[RequiresUnreferencedCode ("Message for --TestType.Requires--")]
public static void Requires () { }
}

class TestType
{
}
}
}
49 changes: 49 additions & 0 deletions test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefReturnDataFlow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.Diagnostics.CodeAnalysis;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.DataFlow
{
[SkipKeptItemsValidation]
[ExpectedNoWarnings]
public class MethodByRefReturnDataFlow
{
public static void Main ()
{
ReturnAnnotatedTypeReferenceAsUnannotated ();
AssignToAnnotatedTypeReference ();
}

[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static Type _annotatedField;

// This should warn, as assiging to the return ref Type will assign value to the annotated field
// but the annotation is not propagated
// https://github.com/dotnet/linker/issues/2158
// [ExpectedWarning("IL????")]
static ref Type ReturnAnnotatedTypeReferenceAsUnannotated () { return ref _annotatedField; }

[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static ref Type ReturnAnnotatedTypeReferenceAsAnnotated () { return ref _annotatedField; }

// https://github.com/dotnet/linker/issues/2158
// [ExpectedWarning("IL2026", "Message for --TestType.Requires--")]
static void AssignToAnnotatedTypeReference ()
{
ref Type typeShouldHaveAllMethods = ref ReturnAnnotatedTypeReferenceAsAnnotated ();
typeShouldHaveAllMethods = typeof (TestTypeWithRequires); // This should apply the annotation -> cause IL2026 due to RUC method
_annotatedField.GetMethods (); // Doesn't warn, but now contains typeof(TestType) - no warning here is correct
}

public class TestTypeWithRequires
{
[RequiresUnreferencedCode ("Message for --TestType.Requires--")]
public static void Requires () { }
}
}
}
12 changes: 12 additions & 0 deletions test/Mono.Linker.Tests.Cases/DataFlow/MethodParametersDataFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static void Main ()
instance.WriteToParameterOnInstanceMethod (null);
instance.LongWriteToParameterOnInstanceMethod (0, 0, 0, 0, null);
instance.UnsupportedParameterType (null);

TestParameterOverwrite (typeof (TestType));
}

// Validate the error message when annotated parameter is passed to another annotated parameter
Expand Down Expand Up @@ -223,6 +225,16 @@ private static void RequirePublicParameterlessConstructorAndNothing (
{
}

[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static Type _fieldWithMethods;

[ExpectedWarning ("IL2077", nameof (_fieldWithMethods))]
static void TestParameterOverwrite ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type type)
{
type = _fieldWithMethods;
type.GetFields ();
}

class TestType
{
public TestType () { }
Expand Down