Skip to content

Commit 89a776f

Browse files
committed
Add TrimmingTest and add one more test case in the NativeAOT test app
1 parent 654731b commit 89a776f

File tree

3 files changed

+132
-5
lines changed

3 files changed

+132
-5
lines changed

src/libraries/System.Runtime.InteropServices/tests/TrimmingTests/System.Runtime.InteropServices.TrimmingTests.proj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</TestConsoleAppSourceFiles>
2020

2121
<TestConsoleAppSourceFiles Include="TypeMap.cs" NativeAotOnly="true">
22+
<EnabledProperties>IlcGenerateMstatFile;IlcGenerateDgmlFile</EnabledProperties>
2223
</TestConsoleAppSourceFiles>
2324
</ItemGroup>
2425

src/libraries/System.Runtime.InteropServices/tests/TrimmingTests/TypeMap.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,41 @@
5858
return 4;
5959
}
6060

61+
if (GetTypeWithoutTrimAnalysis("TrimTarget") is not null)
62+
{
63+
Console.WriteLine("TrimTarget should not be preserved if the only place that would preserve it is a check that is optimized away.");
64+
return 5;
65+
}
66+
6167
if (usedTypeMap.TryGetValue("TrimTargetIsUnreferenced", out _))
6268
{
6369
Console.WriteLine("TrimTargetIsUnreferenced should not be found in used type map.");
64-
return 5;
70+
return 6;
6571
}
6672

6773
IReadOnlyDictionary<Type, Type> usedProxyTypeMap = TypeMapping.GetOrCreateProxyTypeMapping<UsedTypeMap>();
6874
if (!usedProxyTypeMap.TryGetValue(typeof(SourceClass), out Type proxyType))
6975
{
7076
Console.WriteLine("SourceClass not found in used proxy type map.");
71-
return 6;
77+
return 7;
7278
}
7379

7480
if (proxyType != GetTypeWithoutTrimAnalysis("ProxyType"))
7581
{
7682
Console.WriteLine("SourceClass proxy type does not match expected type.");
77-
return 7;
83+
return 8;
7884
}
7985

8086
if (GetTypeWithoutTrimAnalysis("UnusedTargetType") is not null)
8187
{
8288
Console.WriteLine("UnusedTargetType should not be preserved if the external type map is not used and it is not referenced otherwise even if the entry's trim target is kept.");
83-
return 8;
89+
return 9;
8490
}
8591

8692
if (GetTypeWithoutTrimAnalysis("UnusedProxyType") is not null)
8793
{
8894
Console.WriteLine("UnusedProxyType should not be preserved if the proxy type map is not used and it is not referenced otherwise even if the entry's source type is kept.");
89-
return 9;
95+
return 10;
9096
}
9197

9298
return 100;
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
7+
using System.Runtime.CompilerServices;
8+
using System.Runtime.InteropServices;
9+
using Mono.Linker.Tests.Cases.Expectations.Assertions;
10+
using Mono.Linker.Tests.Cases.Reflection;
11+
12+
[assembly: TypeMap<UsedTypeMap> ("TrimTargetIsTarget", typeof (TargetAndTrimTarget), typeof (TargetAndTrimTarget))]
13+
[assembly: TypeMap<UsedTypeMap> ("TrimTargetIsUnrelated", typeof (TargetType), typeof (TrimTarget))]
14+
[assembly: TypeMap<UsedTypeMap> ("TrimTargetIsUnreferenced", typeof (UnreferencedTargetType), typeof (UnreferencedTrimTarget))]
15+
[assembly: TypeMapAssociation<UsedTypeMap> (typeof (SourceClass), typeof (ProxyType))]
16+
17+
[assembly: TypeMap<UnusedTypeMap> ("UnusedName", typeof (UnusedTargetType), typeof (TrimTarget))]
18+
[assembly: TypeMapAssociation<UsedTypeMap> (typeof (UnusedSourceClass), typeof (UnusedProxyType))]
19+
20+
namespace Mono.Linker.Tests.Cases.Reflection
21+
{
22+
[Kept]
23+
[IgnoreTestCase("Trimmer support is currently not implemented", IgnoredBy = Tool.Trimmer)]
24+
class TypeMap
25+
{
26+
[Kept]
27+
public static void Main(string[] args)
28+
{
29+
object t = Activator.CreateInstance (Type.GetType (args[1]));
30+
if (t is TargetAndTrimTarget) {
31+
Console.WriteLine ("Type deriving from TargetAndTrimTarget instantiated.");
32+
} else if (t is TrimTarget) {
33+
Console.WriteLine ("Type deriving from TrimTarget instantiated.");
34+
}
35+
36+
Console.WriteLine ("Hash code of SourceClass instance: " + new SourceClass ().GetHashCode ());
37+
38+
Console.WriteLine (TypeMapping.GetOrCreateExternalTypeMapping<UsedTypeMap> ());
39+
Console.WriteLine (TypeMapping.GetOrCreateProxyTypeMapping<UsedTypeMap> ());
40+
}
41+
}
42+
43+
[Kept(By = Tool.Trimmer)]
44+
class UsedTypeMap;
45+
46+
[Kept]
47+
class TargetAndTrimTarget;
48+
49+
[Kept]
50+
class TargetType;
51+
52+
[Kept]
53+
class TrimTarget;
54+
55+
class UnreferencedTargetType;
56+
57+
class UnreferencedTrimTarget;
58+
59+
[Kept]
60+
[KeptMember(".ctor()")]
61+
class SourceClass;
62+
63+
[Kept]
64+
class ProxyType;
65+
66+
class UnusedTypeMap;
67+
class UnusedTargetType;
68+
class UnusedSourceClass;
69+
class UnusedProxyType;
70+
}
71+
72+
// Polyfill for the type map types until we use an LKG runtime that has it.
73+
namespace System.Runtime.InteropServices
74+
{
75+
[Kept (By = Tool.Trimmer)]
76+
[KeptBaseType (typeof (Attribute), By = Tool.Trimmer)]
77+
[KeptAttributeAttribute (typeof (AttributeUsageAttribute), By = Tool.Trimmer)]
78+
[AttributeUsage (AttributeTargets.Assembly, AllowMultiple = true)]
79+
public sealed class TypeMapAttribute<TTypeMapGroup> : Attribute
80+
{
81+
[Kept (By = Tool.Trimmer)]
82+
public TypeMapAttribute (string value, Type target) { }
83+
84+
[Kept (By = Tool.Trimmer)]
85+
[KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute), By = Tool.Trimmer)]
86+
[RequiresUnreferencedCode ("Interop types may be removed by trimming")]
87+
public TypeMapAttribute (string value, Type target, Type trimTarget) { }
88+
}
89+
90+
[Kept (By = Tool.Trimmer)]
91+
[KeptBaseType (typeof (Attribute), By = Tool.Trimmer)]
92+
[KeptAttributeAttribute (typeof (AttributeUsageAttribute), By = Tool.Trimmer)]
93+
[AttributeUsage (AttributeTargets.Assembly, AllowMultiple = true)]
94+
public sealed class TypeMapAssociationAttribute<TTypeMapGroup> : Attribute
95+
{
96+
[Kept (By = Tool.Trimmer)]
97+
public TypeMapAssociationAttribute (Type source, Type proxy) { }
98+
}
99+
100+
[Kept(By = Tool.Trimmer)]
101+
public static class TypeMapping
102+
{
103+
[Kept(By = Tool.Trimmer)]
104+
[KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute), By = Tool.Trimmer)]
105+
[RequiresUnreferencedCode ("Interop types may be removed by trimming")]
106+
public static IReadOnlyDictionary<string, Type> GetOrCreateExternalTypeMapping<TTypeMapGroup> ()
107+
{
108+
throw new NotImplementedException ();
109+
}
110+
111+
[Kept(By = Tool.Trimmer)]
112+
[KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute), By = Tool.Trimmer)]
113+
[RequiresUnreferencedCode ("Interop types may be removed by trimming")]
114+
public static IReadOnlyDictionary<Type, Type> GetOrCreateProxyTypeMapping<TTypeMapGroup> ()
115+
{
116+
throw new NotImplementedException ();
117+
}
118+
}
119+
}
120+

0 commit comments

Comments
 (0)