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

Validate generic constraints in ILLink tests #103452

Merged
merged 5 commits into from
Jun 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ public Task MethodWithUnmanagedConstraint ()
}

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

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

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

[Fact]
public Task CorrectOverloadedMethodGetsStrippedInGenericClass ()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace Mono.Linker.Tests.Cases.Expectations.Assertions
{
[AttributeUsage (AttributeTargets.GenericParameter, Inherited = false)]
public class KeptAttributeOnConstraintAttribute : KeptAttribute
{
public KeptAttributeOnConstraintAttribute (Type constraintType, Type attributeType)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Reflection;

namespace Mono.Linker.Tests.Cases.Expectations.Assertions
{
[AttributeUsage (AttributeTargets.All, Inherited = false)]
public class KeptGenericParamAttributesAttribute : KeptAttribute
{
public KeptGenericParamAttributesAttribute (GenericParameterAttributes attributes)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Reflection;

sbomer marked this conversation as resolved.
Show resolved Hide resolved
sbomer marked this conversation as resolved.
Show resolved Hide resolved
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

Expand All @@ -19,7 +21,10 @@ public static void Main ()
/// </summary>
/// <typeparam name="T"></typeparam>
[Kept]
static void Method<T> () where T : unmanaged
static void Method<
[KeptGenericParamAttributes (GenericParameterAttributes.NotNullableValueTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint)]
T
> () where T : unmanaged
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#nullable enable

using System.Reflection;
using System.Runtime.CompilerServices;

using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.Attributes.OnlyKeepUsed
{
[SetupCSharpCompilerToUse ("csc")]
[SetupLinkerTrimMode ("link")]
public class NullableOnConstraintsKept
{
public static void Main ()
{
Test.Run ();
}

[Kept]
[KeptInterface (typeof (I))]
class Test : I
{
[Kept]
public static void Run ()
{
new C<Test> ();
Method<Test> ();
}

[Kept]
[KeptAttributeAttribute (typeof (NullableContextAttribute))]
static T? Method<
[KeptGenericParamAttributes (GenericParameterAttributes.ReferenceTypeConstraint)]
[KeptAttributeAttribute (typeof (NullableAttribute))]
T
> ()
where T : class, I?
{
return default;
}
}

[Kept]
interface I
{
}

[Kept]
[KeptMember (".ctor()")]
class C<
[KeptAttributeOnConstraint (typeof (I), typeof (NullableAttribute))]
T
>
where T : I?
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#nullable enable

using System.Reflection;
using System.Runtime.CompilerServices;

using Mono.Linker.Tests.Cases.Expectations.Assertions;
sbomer marked this conversation as resolved.
Show resolved Hide resolved
using Mono.Linker.Tests.Cases.Expectations.Metadata;

Expand All @@ -9,7 +12,7 @@ namespace Mono.Linker.Tests.Cases.Attributes.OnlyKeepUsed
[SetupLinkerArgument ("--used-attrs-only", "true")]
[SetupLinkerTrimMode ("link")]
[IgnoreDescriptors (false)]
public class NullableOnConstraints
public class NullableOnConstraintsRemoved
{
public static void Main ()
{
Expand All @@ -28,21 +31,24 @@ public static void Run ()
}

[Kept]
static T? Method<T> () where T : class, I?
static T? Method<
[KeptGenericParamAttributes (GenericParameterAttributes.ReferenceTypeConstraint)]
T
> () where T : class, I?
{
return default;
}
}
}

[Kept]
interface I
{
}
[Kept]
interface I
{
}

[Kept]
[KeptMember (".ctor()")]
class C<T> where T : I?
{
[Kept]
[KeptMember (".ctor()")]
class C<T> where T : I?
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;
using DAM = System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute;
Expand Down Expand Up @@ -131,14 +132,22 @@ static void UnderlyingTypeOfUnannotatedGenericParameterRequiresProperties<TNulla
}

[Kept]
static void NullableOfAnnotatedGenericParameterRequiresPublicProperties<[KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicProperties)] T> () where T : struct
static void NullableOfAnnotatedGenericParameterRequiresPublicProperties<
[KeptGenericParamAttributes (GenericParameterAttributes.NotNullableValueTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint)]
[KeptAttributeAttribute (typeof (DAM))]
[DAM (DAMT.PublicProperties)]
T
> () where T : struct
{
Nullable.GetUnderlyingType (typeof (Nullable<T>)).RequiresPublicProperties ();
}

[Kept]
[ExpectedWarning ("IL2087")]
static void NullableOfUnannotatedGenericParameterRequiresPublicProperties<T> () where T : struct
static void NullableOfUnannotatedGenericParameterRequiresPublicProperties<
[KeptGenericParamAttributes (GenericParameterAttributes.NotNullableValueTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint)]
T
> () where T : struct
{
Nullable.GetUnderlyingType (typeof (Nullable<T>)).RequiresPublicProperties ();
}
Expand Down Expand Up @@ -240,7 +249,12 @@ static void DamOnNullableKeepsUnderlyingMembers ()
}

[Kept]
static void UnderlyingTypeOfCreatedNullableOfAnnotatedTRequiresPublicProperties<[KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicProperties)] T> () where T : struct
static void UnderlyingTypeOfCreatedNullableOfAnnotatedTRequiresPublicProperties<
[KeptGenericParamAttributes (GenericParameterAttributes.NotNullableValueTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint)]
[KeptAttributeAttribute (typeof (DAM))]
[DAM (DAMT.PublicProperties)]
T
> () where T : struct
{
Type t = typeof (Nullable<T>);
t = Nullable.GetUnderlyingType (t);
Expand All @@ -262,13 +276,20 @@ struct StructWithFieldsReferencedThroughDamOnNullable

[Kept]
[ExpectedWarning ("IL2091")]
static void NullableOfUnannotatedGenericParamPassedAsGenericParamRequiresPublicFields<T> () where T : struct
static void NullableOfUnannotatedGenericParamPassedAsGenericParamRequiresPublicFields<
[KeptGenericParamAttributes (GenericParameterAttributes.NotNullableValueTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint)]
T
> () where T : struct
{
RequirePublicFieldsOnGenericParam<Nullable<T>> ();
}

[Kept]
static void NullableOfAnnotatedGenericParamPassedAsGenericParamRequiresPublicFields<[KeptAttributeAttribute (typeof (DAM))][DAM (DAMT.PublicFields)] T> () where T : struct
static void NullableOfAnnotatedGenericParamPassedAsGenericParamRequiresPublicFields<
[KeptGenericParamAttributes (GenericParameterAttributes.NotNullableValueTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint)]
[KeptAttributeAttribute (typeof (DAM))]
[DAM (DAMT.PublicFields)] T
> () where T : struct
{
RequirePublicFieldsOnGenericParam<Nullable<T>> ();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.Generics
{
class ByRefLike
{
static void Main ()
{
Test ();
}

[Kept]
static void Test ()
{
G<RefStruct> g = new ();
}

[Kept]
[KeptAttributeAttribute (typeof (IsByRefLikeAttribute))]
[KeptAttributeAttribute (typeof (ObsoleteAttribute))] // Signals this is unsupported to older compilers
[KeptAttributeAttribute (typeof (CompilerFeatureRequiredAttribute))]
ref struct RefStruct {
}

[Kept]
[KeptAttributeAttribute (typeof (IsByRefLikeAttribute))]
[KeptAttributeAttribute (typeof (ObsoleteAttribute))] // Signals this is unsupported to older compilers
[KeptAttributeAttribute (typeof (CompilerFeatureRequiredAttribute))]
ref struct G<
[KeptGenericParamAttributes (GenericParameterAttributes.AllowByRefLike)]
T
> where T : allows ref struct {
[Kept]
public T t;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Runtime.CompilerServices;
using Mono.Linker.Tests.Cases.Expectations.Assertions;

namespace Mono.Linker.Tests.Cases.Generics
Expand Down Expand Up @@ -36,7 +37,10 @@ public void Foo ()

[Kept]
[KeptMember (".ctor()")]
class WithConstraint<T> where T : new()
class WithConstraint<
[KeptGenericParamAttributes (GenericParameterAttributes.DefaultConstructorConstraint)]
T
> where T : new()
{
}

Expand Down Expand Up @@ -70,7 +74,10 @@ public void Foo ()

[Kept]
[KeptMember (".ctor()")]
class WithConstraint<T> where T : struct
class WithConstraint<
[KeptGenericParamAttributes (GenericParameterAttributes.NotNullableValueTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint)]
T
> where T : struct
{
}

Expand Down Expand Up @@ -104,7 +111,11 @@ public void Foo ()

[Kept]
[KeptMember (".ctor()")]
class WithConstraint<[KeptAttributeAttribute (typeof (IsUnmanagedAttribute))] T> where T : unmanaged
class WithConstraint<
[KeptAttributeAttribute (typeof (IsUnmanagedAttribute))]
[KeptGenericParamAttributes (GenericParameterAttributes.NotNullableValueTypeConstraint | GenericParameterAttributes.DefaultConstructorConstraint)]
T
> where T : unmanaged
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using System.Reflection;

using Mono.Linker.Tests.Cases.Expectations.Assertions;

namespace Mono.Linker.Tests.Cases.Generics
{
public class VariantCasting
{
[Kept]
interface IVariant<out T> { }
interface IVariant<
[KeptGenericParamAttributes (GenericParameterAttributes.Covariant)]
out T
> { }

[Kept]
interface IFoo { }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Reflection;

using Mono.Linker.Tests.Cases.Expectations.Assertions;
sbomer marked this conversation as resolved.
Show resolved Hide resolved

namespace Mono.Linker.Tests.Cases.Inheritance.Interfaces.OnValueType
Expand Down Expand Up @@ -33,7 +35,10 @@ public interface IBuilder
}

[Kept]
public interface IBuildable<T> : IBuildable where T : IBuilder, new()
public interface IBuildable<
[KeptGenericParamAttributes (GenericParameterAttributes.DefaultConstructorConstraint)]
T
> : IBuildable where T : IBuilder, new()
{
}

Expand Down
Loading
Loading