Skip to content

Commit f5469fe

Browse files
authored
Warn on trimming with COM hosting (built-in COM support) enabled (#72493)
1 parent 778137b commit f5469fe

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

eng/testing/linker/SupportFiles/Directory.Build.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<SkipConfigureTrimming>true</SkipConfigureTrimming>
44
<PublishTrimmed>true</PublishTrimmed>
55
<SkipImportRepoLinkerTargets>true</SkipImportRepoLinkerTargets>
6-
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
76
<TrimmerDefaultAction>link</TrimmerDefaultAction>
87
<TrimMode>link</TrimMode>
98
<TrimmerRemoveSymbols>false</TrimmerRemoveSymbols>

src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
<property name="Target">M:System.StartupHookProvider.ProcessStartupHooks()</property>
99
<property name="Justification">This warning is left in the product so developers get an ILLink warning when trimming an app with System.StartupHookProvider.IsSupported=true.</property>
1010
</attribute>
11+
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
12+
<argument>ILLink</argument>
13+
<argument>IL2026</argument>
14+
<property name="Scope">member</property>
15+
<property name="Target">M:Internal.Runtime.InteropServices.ComActivator.GetClassFactoryForTypeInternal(Internal.Runtime.InteropServices.ComActivationContextInternal*)</property>
16+
<property name="Justification">This warning is left in the product so developers get an ILLink warning when trimming an app with System.Runtime.InteropServices.BuiltInComInterop.IsSupported.IsSupported=true.</property>
17+
</attribute>
1118
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
1219
<argument>ILLink</argument>
1320
<argument>IL2026</argument>

src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ void CreateInstanceLic(
5959

6060
internal partial struct ComActivationContext
6161
{
62-
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
6362
public static unsafe ComActivationContext Create(ref ComActivationContextInternal cxtInt)
6463
{
6564
if (!Marshal.IsBuiltInComSupported)
@@ -217,7 +216,6 @@ private static void ClassRegistrationScenarioForType(ComActivationContext cxt, b
217216
/// Internal entry point for unmanaged COM activation API from native code
218217
/// </summary>
219218
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
220-
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
221219
[UnmanagedCallersOnly]
222220
private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInternal* pCxtInt)
223221
{
@@ -243,7 +241,9 @@ private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInt
243241
try
244242
{
245243
var cxt = ComActivationContext.Create(ref cxtInt);
244+
#pragma warning disable IL2026 // suppressed in ILLink.Suppressions.LibraryBuild.xml
246245
object cf = GetClassFactoryForType(cxt);
246+
#pragma warning restore IL2026
247247
IntPtr nativeIUnknown = Marshal.GetIUnknownForObject(cf);
248248
Marshal.WriteIntPtr(cxtInt.ClassFactoryDest, nativeIUnknown);
249249
}
@@ -259,7 +259,6 @@ private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInt
259259
/// Internal entry point for registering a managed COM server API from native code
260260
/// </summary>
261261
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
262-
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
263262
[UnmanagedCallersOnly]
264263
private static unsafe int RegisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
265264
{
@@ -291,20 +290,24 @@ private static unsafe int RegisterClassForTypeInternal(ComActivationContextInter
291290
try
292291
{
293292
var cxt = ComActivationContext.Create(ref cxtInt);
294-
ClassRegistrationScenarioForType(cxt, register: true);
293+
ClassRegistrationScenarioForTypeLocal(cxt, register: true);
295294
}
296295
catch (Exception e)
297296
{
298297
return e.HResult;
299298
}
300299

301300
return 0;
301+
302+
// Use a local function for a targeted suppression of the requires unreferenced code warning
303+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
304+
Justification = "The same feature switch applies to GetClassFactoryForTypeInternal and this function. We rely on the warning from GetClassFactoryForTypeInternal.")]
305+
static void ClassRegistrationScenarioForTypeLocal(ComActivationContext cxt, bool register) => ClassRegistrationScenarioForType(cxt, register);
302306
}
303307

304308
/// <summary>
305309
/// Internal entry point for unregistering a managed COM server API from native code
306310
/// </summary>
307-
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
308311
[UnmanagedCallersOnly]
309312
private static unsafe int UnregisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
310313
{
@@ -336,14 +339,19 @@ private static unsafe int UnregisterClassForTypeInternal(ComActivationContextInt
336339
try
337340
{
338341
var cxt = ComActivationContext.Create(ref cxtInt);
339-
ClassRegistrationScenarioForType(cxt, register: false);
342+
ClassRegistrationScenarioForTypeLocal(cxt, register: false);
340343
}
341344
catch (Exception e)
342345
{
343346
return e.HResult;
344347
}
345348

346349
return 0;
350+
351+
// Use a local function for a targeted suppression of the requires unreferenced code warning
352+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
353+
Justification = "The same feature switch applies to GetClassFactoryForTypeInternal and this function. We rely on the warning from GetClassFactoryForTypeInternal.")]
354+
static void ClassRegistrationScenarioForTypeLocal(ComActivationContext cxt, bool register) => ClassRegistrationScenarioForType(cxt, register);
347355
}
348356

349357
private static bool IsLoggingEnabled()

0 commit comments

Comments
 (0)