Skip to content

Commit

Permalink
Use DynamicDependencyAttribute (#37780)
Browse files Browse the repository at this point in the history
* Use DynamicDependencyAttribute

* PR feedback

- Use typeof in more places
- Add comments about DynamicallyAccessedMembers
- Add comments about cases where we work around linker limitations
- Also move AssemblyBuilder`s DynamicDependency closer to where it
is actually needed.

* Add issue links

* Include internal attribute for OOB packages

On down-level platforms where DynamicDependencyAttribute doesn't exist
in corelib, this includes it in the OOB assemblies.
  • Loading branch information
sbomer authored Jun 16, 2020
1 parent bd7b3c5 commit ecdb7f3
Show file tree
Hide file tree
Showing 36 changed files with 122 additions and 217 deletions.
2 changes: 1 addition & 1 deletion eng/DefaultGenApiDocIds.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ T:System.ComponentModel.EditorAttribute
T:System.ComponentModel.ToolboxItemAttribute
T:System.ComponentModel.TypeDescriptionProviderAttribute
T:System.Configuration.ConfigurationPropertyAttribute
T:System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute
T:System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute
T:System.Diagnostics.CodeAnalysis.MemberNotNullAttribute
T:System.Diagnostics.CodeAnalysis.SuppressMessageAttribute
Expand All @@ -27,7 +28,6 @@ T:System.Runtime.CompilerServices.IteratorStateMachineAttribute
T:System.Runtime.CompilerServices.MethodImpl
T:System.Runtime.CompilerServices.NullableAttribute
T:System.Runtime.CompilerServices.NullableContextAttribute
T:System.Runtime.CompilerServices.PreserveDependencyAttribute
T:System.Runtime.CompilerServices.TypeForwardedFromAttribute
T:System.Runtime.ConstrainedExecution.PrePrepareMethodAttribute
T:System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
Expand Down
2 changes: 1 addition & 1 deletion eng/illink.targets
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<ILLinkArgs>$(ILLinkArgs) --skip-unresolved true</ILLinkArgs>
<!-- keep interface implementations -->
<ILLinkArgs>$(ILLinkArgs) --disable-opt unusedinterfaces</ILLinkArgs>
<!-- keep PreserveDependencyAttribute unless a project explicitly disables it -->
<!-- keep DynamicDependencyAttribute unless a project explicitly disables it -->
<ILLinkArgs Condition="'$(ILLinkKeepDepAttributes)' != 'false'">$(ILLinkArgs) --keep-dep-attributes true</ILLinkArgs>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;

namespace System.Collections.Generic
{
Expand All @@ -20,7 +21,7 @@ internal partial class ArraySortHelper<T>

public static IArraySortHelper<T> Default => s_defaultArraySortHelper;

[PreserveDependency(".ctor", "System.Collections.Generic.GenericArraySortHelper`1")]
[DynamicDependency("#ctor", typeof(GenericArraySortHelper<>))]
private static IArraySortHelper<T> CreateArraySortHelper()
{
IArraySortHelper<T> defaultArraySortHelper;
Expand Down Expand Up @@ -55,7 +56,7 @@ internal partial class ArraySortHelper<TKey, TValue>

public static IArraySortHelper<TKey, TValue> Default => s_defaultArraySortHelper;

[PreserveDependency(".ctor", "System.Collections.Generic.GenericArraySortHelper`2")]
[DynamicDependency("#ctor", typeof(GenericArraySortHelper<,>))]
private static IArraySortHelper<TKey, TValue> CreateArraySortHelper()
{
IArraySortHelper<TKey, TValue> defaultArraySortHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using System.Threading;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;

namespace System.Diagnostics
{
Expand Down Expand Up @@ -84,12 +84,13 @@ public StackFrameHelper(Thread? target)
// done by GetStackFramesInternal (on Windows for old PDB format).
//

// TODO: Remove these DynamicDependencyAttributes when https://github.com/mono/linker/issues/943 is fixed.
// This is necessary because linker can't add new assemblies to the closure when recognizing Type.GetType
// so the code below is actually recognized by linker, but fails to resolve the type since the System.Diagnostics.StackTrace
// is not always part of the closure linker works on.
// PreserveDependencyAttribute on the other hand can pull in additional assemblies.
[PreserveDependency("GetSourceLineInfo", "System.Diagnostics.StackTraceSymbols", "System.Diagnostics.StackTrace")]
[PreserveDependency(".ctor()", "System.Diagnostics.StackTraceSymbols", "System.Diagnostics.StackTrace")]
// DynamicDependencyAttribute on the other hand can pull in additional assemblies.
[DynamicDependency("GetSourceLineInfo", "System.Diagnostics.StackTraceSymbols", "System.Diagnostics.StackTrace")]
[DynamicDependency("#ctor()", "System.Diagnostics.StackTraceSymbols", "System.Diagnostics.StackTrace")]
internal void InitializeSourceInfo(int iSkip, bool fNeedFileInfo, Exception? exception)
{
StackTrace.GetStackFramesInternal(this, iSkip, fNeedFileInfo, exception);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@
<Compile Include="System\ComponentModel\Design\Serialization\RootDesignerSerializerAttribute.cs" />
<Compile Include="System\ComponentModel\ComponentResourceManager.cs" />
<Compile Include="System\Security\Authentication\ExtendedProtection\ExtendedProtectionPolicyTypeConverter.cs" />
<Compile Include="$(CommonPath)System\Runtime\CompilerServices\PreserveDependencyAttribute.cs"
Link="Common\System\Runtime\CompilerServices\PreserveDependencyAttribute.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Collections" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

namespace System.ComponentModel
Expand Down Expand Up @@ -87,7 +86,8 @@ private TypeDescriptor()
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static Type InterfaceType
{
[PreserveDependency(".ctor", "System.ComponentModel.TypeDescriptor/TypeDescriptorInterface")]
// TODO: replace this with DynamicallyAccessedMembersAttribute (https://github.com/dotnet/runtime/issues/37837)
[DynamicDependency("#ctor", typeof(TypeDescriptorInterface))]
get => typeof(TypeDescriptorInterface);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@
<Compile Include="System\Data\SQLTypes\SQLBytes.cs" />
<Compile Include="System\Data\ProviderBase\DataReaderContainer.cs" />
<Compile Include="System\Data\ProviderBase\SchemaMapping.cs" />
<Compile Include="$(CommonPath)System\Runtime\CompilerServices\PreserveDependencyAttribute.cs"
Link="Common\System\Runtime\CompilerServices\PreserveDependencyAttribute.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Collections" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Diagnostics;
using System.Text;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.SqlTypes
{
Expand Down Expand Up @@ -128,7 +128,7 @@ private static Func<Stream, XmlReaderSettings, XmlParserContext, XmlReader> Crea

private static MethodInfo CreateSqlReaderMethodInfo
{
[PreserveDependency("CreateSqlReader", "System.Xml.XmlReader", "System.Private.Xml")]
[DynamicDependency("CreateSqlReader", typeof(System.Xml.XmlReader))]
get
{
if (s_createSqlReaderMethodInfo == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
<Compile Include="System\Diagnostics\DiagnosticSource.cs" />
<Compile Include="System\Diagnostics\DiagnosticListener.cs" />
<Compile Include="System\Diagnostics\DiagnosticSourceEventSource.cs" />
<Compile Include="$(CommonPath)System\Runtime\CompilerServices\PreserveDependencyAttribute.cs"
Link="Common\System\Runtime\CompilerServices\PreserveDependencyAttribute.cs" />
<None Include="DiagnosticSourceUsersGuide.md" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.1'">
<ItemGroup Condition="'$(TargetFramework)' != '$(NetCoreAppCurrent)'">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicDependencyAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard1.1'">
<Compile Include="$(CommonPath)System\HexConverter.cs"
Link="Common\System\HexConverter.cs" />
<Compile Include="System\Diagnostics\Activity.cs" />
Expand All @@ -46,10 +48,10 @@
<Reference Include="System.Runtime.CompilerServices.Unsafe" />
<None Include="ActivityUserGuide.md" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'net45' And '$(TargetFramework)' != 'netstandard1.1'">
<ItemGroup Condition="'$(TargetFramework)' != 'net45' And '$(TargetFramework)' != 'netstandard1.1'">
<Compile Include="System\Diagnostics\Activity.Current.net46.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<Compile Include="System\Diagnostics\Activity.Current.net45.cs" />
<TargetingPackReference Include="System" />
<TargetingPackReference Include="System.Runtime.Remoting" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -891,9 +892,9 @@ public PropertyFetch(Type? type)
/// <summary>
/// Create a property fetcher for a propertyName
/// </summary>
[PreserveDependency(".ctor(System.Type)", "System.Diagnostics.DiagnosticSourceEventSource/TransformSpec/PropertySpec/PropertyFetch/EnumeratePropertyFetch`1")]
[PreserveDependency(".ctor(System.Type, System.Reflection.PropertyInfo)", "System.Diagnostics.DiagnosticSourceEventSource/TransformSpec/PropertySpec/PropertyFetch/RefTypedFetchProperty`2")]
[PreserveDependency(".ctor(System.Type, System.Reflection.PropertyInfo)", "System.Diagnostics.DiagnosticSourceEventSource/TransformSpec/PropertySpec/PropertyFetch/ValueTypedFetchProperty`2")]
[DynamicDependency("#ctor(System.Type)", typeof(EnumeratePropertyFetch<>))]
[DynamicDependency("#ctor(System.Type,System.Reflection.PropertyInfo)", typeof(RefTypedFetchProperty<,>))]
[DynamicDependency("#ctor(System.Type,System.Reflection.PropertyInfo)", typeof(ValueTypedFetchProperty<,>))]
public static PropertyFetch FetcherForProperty(Type? type, string propertyName)
{
if (propertyName == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
<ItemGroup>
<Compile Include="$(CommonPath)System\Collections\Generic\ArrayBuilder.cs"
Link="Common\System\Collections\Generic\ArrayBuilder.cs" />
<Compile Include="$(CommonPath)System\Runtime\CompilerServices\PreserveDependencyAttribute.cs"
Link="Common\System\Runtime\CompilerServices\PreserveDependencyAttribute.cs" />
<Compile Include="System\Dynamic\Utils\CacheDict.cs" />
<Compile Include="System\Dynamic\Utils\ContractUtils.cs" />
<Compile Include="System\Dynamic\Utils\ExpressionUtils.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Dynamic;
using System.Dynamic.Utils;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace System.Linq.Expressions.Interpreter
Expand Down Expand Up @@ -160,16 +160,17 @@ private static CallInstruction FastCreate<T0, T1>(MethodInfo target, ParameterIn
#endif

#if FEATURE_DLG_INVOKE
[PreserveDependency(".ctor", "System.Linq.Expressions.Interpreter.ActionCallInstruction")]
[PreserveDependency(".ctor", "System.Linq.Expressions.Interpreter.ActionCallInstruction`1")]
[PreserveDependency(".ctor", "System.Linq.Expressions.Interpreter.ActionCallInstruction`2")]
[PreserveDependency(".ctor", "System.Linq.Expressions.Interpreter.ActionCallInstruction`3")]
[PreserveDependency(".ctor", "System.Linq.Expressions.Interpreter.ActionCallInstruction`4")]
[PreserveDependency(".ctor", "System.Linq.Expressions.Interpreter.FuncCallInstruction`1")]
[PreserveDependency(".ctor", "System.Linq.Expressions.Interpreter.FuncCallInstruction`2")]
[PreserveDependency(".ctor", "System.Linq.Expressions.Interpreter.FuncCallInstruction`3")]
[PreserveDependency(".ctor", "System.Linq.Expressions.Interpreter.FuncCallInstruction`4")]
[PreserveDependency(".ctor", "System.Linq.Expressions.Interpreter.FuncCallInstruction`5")]
// TODO: replace these with DynamicallyAccessedMembersAttribute (https://github.com/dotnet/runtime/issues/37837)
[DynamicDependency("#ctor", typeof(ActionCallInstruction))]
[DynamicDependency("#ctor", typeof(ActionCallInstruction<>))]
[DynamicDependency("#ctor", typeof(ActionCallInstruction<,>))]
[DynamicDependency("#ctor", typeof(ActionCallInstruction<,,>))]
[DynamicDependency("#ctor", typeof(ActionCallInstruction<,,,>))]
[DynamicDependency("#ctor", typeof(FuncCallInstruction<>))]
[DynamicDependency("#ctor", typeof(FuncCallInstruction<,>))]
[DynamicDependency("#ctor", typeof(FuncCallInstruction<,,>))]
[DynamicDependency("#ctor", typeof(FuncCallInstruction<,,,>))]
[DynamicDependency("#ctor", typeof(FuncCallInstruction<,,,,>))]
private static Type GetHelperType(MethodInfo info, Type[] arrTypes)
{
Type t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Dynamic.Utils;
using System.Linq.Expressions;
Expand Down Expand Up @@ -275,47 +276,7 @@ internal void MoveRule(int i)
}

#if FEATURE_COMPILE
// TODO https://github.com/mono/linker/issues/799: Consolidate these attributes when possible.
[PreserveDependency("NoMatch1`2", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatch2`3", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatch3`4", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatch4`5", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatch5`6", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatch6`7", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatch7`8", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatch8`9", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatch9`10", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatch10`11", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatchVoid1`1", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatchVoid2`2", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatchVoid3`3", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatchVoid4`4", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatchVoid5`5", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatchVoid6`6", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatchVoid7`7", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatchVoid8`8", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatchVoid9`9", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("NoMatchVoid10`10", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecute1`2", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecute2`3", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecute3`4", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecute4`5", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecute5`6", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecute6`7", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecute7`8", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecute8`9", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecute9`10", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecute10`11", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecuteVoid1`1", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecuteVoid2`2", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecuteVoid3`3", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecuteVoid4`4", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecuteVoid5`5", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecuteVoid6`6", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecuteVoid7`7", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecuteVoid8`8", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecuteVoid9`9", "System.Dynamic.UpdateDelegates")]
[PreserveDependency("UpdateAndExecuteVoid10`10", "System.Dynamic.UpdateDelegates")]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(UpdateDelegates))]
#endif
internal T MakeUpdateDelegate()
{
Expand Down
Loading

0 comments on commit ecdb7f3

Please sign in to comment.