Skip to content

Commit 442a380

Browse files
sbomerJamesNKMackinnonBuck
authored
Enable trim analyzer (#41683)
Co-authored-by: James Newton-King <[email protected]> Co-authored-by: Mackinnon Buck <[email protected]>
1 parent 4aa4ec3 commit 442a380

File tree

22 files changed

+57
-12
lines changed

22 files changed

+57
-12
lines changed

eng/Workarounds.props

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,4 @@
4242
<ErrorOnDuplicatePublishOutputFiles>false</ErrorOnDuplicatePublishOutputFiles>
4343
</PropertyGroup>
4444

45-
<!-- Trim analyzers are automatically enabled by IsTrimming=true. They currently break when run on aspnetcore.
46-
Remove when trimming analyzers are fixed: https://github.com/dotnet/aspnetcore/issues/41016 -->
47-
<PropertyGroup>
48-
<EnableTrimAnalyzer Condition=" '$(EnableTrimAnalyzer)' == ''">false</EnableTrimAnalyzer>
49-
</PropertyGroup>
50-
5145
</Project>

src/Components/Components/src/BindConverter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,7 @@ private static class FormatterDelegateCache
18261826
}
18271827
}
18281828

1829+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "We expect unknown underlying types are configured by application code to be retained.")]
18291830
private static BindFormatter<T> MakeTypeConverterFormatter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>()
18301831
{
18311832
var typeConverter = TypeDescriptor.GetConverter(typeof(T));
@@ -2028,6 +2029,7 @@ bool ConvertToArray(object? obj, CultureInfo? culture, out T[]? value)
20282029
}
20292030
}
20302031

2032+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "We expect unknown underlying types are configured by application code to be retained.")]
20312033
private static BindParser<T> MakeTypeConverterConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>()
20322034
{
20332035
var typeConverter = TypeDescriptor.GetConverter(typeof(T));

src/Components/Components/src/CascadingParameterState.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Reflection;
77
using Microsoft.AspNetCore.Components.Reflection;
88
using Microsoft.AspNetCore.Components.Rendering;
9+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
910

1011
namespace Microsoft.AspNetCore.Components;
1112

@@ -75,7 +76,7 @@ public static IReadOnlyList<CascadingParameterState> FindCascadingParameters(Com
7576
}
7677

7778
private static ReflectedCascadingParameterInfo[] GetReflectedCascadingParameterInfos(
78-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] Type componentType)
79+
[DynamicallyAccessedMembers(Component)] Type componentType)
7980
{
8081
if (!_cachedInfos.TryGetValue(componentType, out var infos))
8182
{
@@ -87,7 +88,7 @@ private static ReflectedCascadingParameterInfo[] GetReflectedCascadingParameterI
8788
}
8889

8990
private static ReflectedCascadingParameterInfo[] CreateReflectedCascadingParameterInfos(
90-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] Type componentType)
91+
[DynamicallyAccessedMembers(Component)] Type componentType)
9192
{
9293
List<ReflectedCascadingParameterInfo>? result = null;
9394
var candidateProps = ComponentProperties.GetCandidateBindableProperties(componentType);

src/Components/Components/src/DynamicComponent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void Attach(RenderHandle renderHandle)
5959
}
6060

6161
/// <inheritdoc />
62+
[UnconditionalSuppressMessage("Trimming", "IL2072", Justification = "We expect that types used with DynamicComponent will be defined in assemblies that don't get trimmed.")]
6263
public Task SetParametersAsync(ParameterView parameters)
6364
{
6465
// This manual parameter assignment logic will be marginally faster than calling

src/Components/Components/src/LayoutAttribute.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
using static Microsoft.AspNetCore.Internal.LinkerFlags;
6+
47
namespace Microsoft.AspNetCore.Components;
58

69
/// <summary>
@@ -13,7 +16,7 @@ public sealed class LayoutAttribute : Attribute
1316
/// Constructs an instance of <see cref="LayoutAttribute"/>.
1417
/// </summary>
1518
/// <param name="layoutType">The type of the layout.</param>
16-
public LayoutAttribute(Type layoutType)
19+
public LayoutAttribute([DynamicallyAccessedMembers(Component)] Type layoutType)
1720
{
1821
LayoutType = layoutType ?? throw new ArgumentNullException(nameof(layoutType));
1922

src/Components/Components/src/LayoutView.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public Task SetParametersAsync(ParameterView parameters)
4646
return Task.CompletedTask;
4747
}
4848

49+
[UnconditionalSuppressMessage("Trimming", "IL2072", Justification = "Layout components are preserved because the LayoutAttribute constructor parameter is correctly annotated.")]
4950
private void Render()
5051
{
5152
// In the middle goes the supplied content

src/Components/Components/src/RouteView.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#nullable disable warnings
55

6+
using System.Diagnostics.CodeAnalysis;
67
using System.Reflection;
78
using Microsoft.AspNetCore.Components.Rendering;
89
using Microsoft.AspNetCore.Components.Routing;
@@ -72,6 +73,7 @@ public Task SetParametersAsync(ParameterView parameters)
7273
/// Renders the component.
7374
/// </summary>
7475
/// <param name="builder">The <see cref="RenderTreeBuilder"/>.</param>
76+
[UnconditionalSuppressMessage("Trimming", "IL2111", Justification = "Layout components are preserved because the LayoutAttribute constructor parameter is correctly annotated.")]
7577
protected virtual void Render(RenderTreeBuilder builder)
7678
{
7779
var pageLayoutType = RouteData.PageType.GetCustomAttribute<LayoutAttribute>()?.LayoutType

src/Components/Components/src/Routing/RouteTableFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Concurrent;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Reflection;
67
using Microsoft.AspNetCore.Components.Routing;
78

@@ -30,6 +31,7 @@ public static RouteTable Create(RouteKey routeKey)
3031

3132
public static void ClearCaches() => Cache.Clear();
3233

34+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Application code does not get trimmed, and the framework does not define routable components.")]
3335
private static List<Type> GetRouteableComponents(RouteKey routeKey)
3436
{
3537
var routeableComponents = new List<Type>();

src/Components/Forms/src/EditContextDataAnnotationsExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public DataAnnotationsEventSubscriptions(EditContext editContext, IServiceProvid
8383
}
8484
}
8585

86+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Model types are expected to be defined in assemblies that do not get trimmed.")]
8687
private void OnFieldChanged(object? sender, FieldChangedEventArgs eventArgs)
8788
{
8889
var fieldIdentifier = eventArgs.FieldIdentifier;
@@ -108,6 +109,7 @@ private void OnFieldChanged(object? sender, FieldChangedEventArgs eventArgs)
108109
}
109110
}
110111

112+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Model types are expected to be defined in assemblies that do not get trimmed.")]
111113
private void OnValidationRequested(object? sender, ValidationRequestedEventArgs e)
112114
{
113115
var validationContext = new ValidationContext(_editContext.Model, _serviceProvider, items: null);
@@ -152,6 +154,7 @@ public void Dispose()
152154
}
153155
}
154156

157+
[UnconditionalSuppressMessage("Trimming", "IL2080", Justification = "Model types are expected to be defined in assemblies that do not get trimmed.")]
155158
private static bool TryGetValidatableProperty(in FieldIdentifier fieldIdentifier, [NotNullWhen(true)] out PropertyInfo? propertyInfo)
156159
{
157160
var cacheKey = (ModelType: fieldIdentifier.Model.GetType(), fieldIdentifier.FieldName);

src/Components/Shared/src/ComponentParametersTypeCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal sealed class ComponentParametersTypeCache
2424
}
2525
}
2626

27-
[RequiresUnreferencedCode("This type attempts to load component parameters that may be trimmed.")]
27+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "We expect application code is configured to preserve component parameters.")]
2828
private static Type? ResolveType(Key key, Assembly[] assemblies)
2929
{
3030
Assembly? assembly = null;

0 commit comments

Comments
 (0)