[ObjCRuntime] Suppress trimmer warnings for Class.ResolveToken. (#20278)

Contributes towards #10405.
This commit is contained in:
Rolf Bjarne Kvinge 2024-03-12 08:51:05 +01:00 коммит произвёл GitHub
Родитель e1e2a1e207
Коммит 90a4288eb3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 40 добавлений и 24 удалений

Просмотреть файл

@ -508,20 +508,51 @@ namespace ObjCRuntime {
static MemberInfo? ResolveToken (Assembly assembly, Module? module, uint token)
{
#if !NET
return ResolveTokenNonManagedStatic (assembly, module, token);
#else
if (!Runtime.IsManagedStaticRegistrar)
return ResolveTokenNonManagedStatic (assembly, module, token);
// Finally resolve the token.
var token_type = token & 0xFF000000;
switch (token & 0xFF000000) {
case 0x02000000: // TypeDef
var type = RegistrarHelper.LookupRegisteredType (assembly, token & 0x00FFFFFF);
#if LOG_TYPELOAD
Runtime.NSLog ($"ResolveToken (0x{token:X}) => Type: {type.FullName}");
#endif
return type;
case 0x06000000: // Method
throw ErrorHelper.CreateError (8054, Errors.MX8054 /* Can't resolve metadata tokens for methods when using the managed static registrar (token: 0x{0}). */, token.ToString ("x"));
default:
throw ErrorHelper.CreateError (8021, $"Unknown implicit token type: 0x{token_type:X}.");
}
#endif // !NET
}
#if NET
// This method should never be called when using the managed static registrar, so assert that never happens by throwing an exception in that case.
// This method doesn't necessarily work with NativeAOT, but this is covered by the exception, because the managed static registrar is required for NativeAOT.
//
// IL2026: Using member 'System.Reflection.Module.ResolveMethod(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens.
// IL2026: Using member 'System.Reflection.Module.ResolveType(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens.
[UnconditionalSuppressMessage("", "IL2026", Justification = "The APIs this method tries to access are marked by other means, so this is linker-safe.")]
#endif
static MemberInfo? ResolveTokenNonManagedStatic (Assembly assembly, Module? module, uint token)
{
#if NET
// This method should never be called when using the managed static registrar, so assert that never happens by throwing an exception in that case.
// This also takes care of NativeAOT, because the managed static registrar is required when using NativeAOT.
if (Runtime.IsManagedStaticRegistrar)
throw new System.Diagnostics.UnreachableException ();
#endif
// Finally resolve the token.
var token_type = token & 0xFF000000;
switch (token & 0xFF000000) {
case 0x02000000: // TypeDef
Type type;
#if NET
if (Runtime.IsManagedStaticRegistrar) {
type = RegistrarHelper.LookupRegisteredType (assembly, token & 0x00FFFFFF);
#if LOG_TYPELOAD
Runtime.NSLog ($"ResolveToken (0x{token:X}) => Type: {type.FullName}");
#endif
return type;
}
#endif // NET
if (module is null) {
throw ErrorHelper.CreateError (8053, Errors.MX8053 /* Could not resolve the module in the assembly {0}. */, assembly.FullName);
} else {
@ -532,9 +563,6 @@ namespace ObjCRuntime {
#endif
return type;
case 0x06000000: // Method
if (Runtime.IsManagedStaticRegistrar)
throw ErrorHelper.CreateError (8054, Errors.MX8054 /* Can't resolve metadata tokens for methods when using the managed static registrar (token: 0x{0}). */, token.ToString ("x"));
if (module is null)
throw ErrorHelper.CreateError (8053, Errors.MX8053 /* Could not resolve the module in the assembly {0}. */, assembly.FullName);

Просмотреть файл

@ -23,8 +23,6 @@ namespace Xamarin.Tests {
case ApplePlatform.iOS:
case ApplePlatform.TVOS:
expectedWarnings = new ExpectedBuildMessage [] {
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 541 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveMethod(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 528 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveType(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 2434 */, "ObjCRuntime.Runtime.FindClosedMethod(Type, MethodBase): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'closed_type' of method 'ObjCRuntime.Runtime.FindClosedMethod(Type, MethodBase)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 1279 */, "ObjCRuntime.Runtime.FindPropertyInfo(MethodInfo): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Reflection.MemberInfo.DeclaringType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 1673 */, "ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
@ -33,8 +31,6 @@ namespace Xamarin.Tests {
break;
case ApplePlatform.MacOSX:
expectedWarnings = new ExpectedBuildMessage [] {
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 541 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveMethod(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 528 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveType(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 2434 */, "ObjCRuntime.Runtime.FindClosedMethod(Type, MethodBase): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'closed_type' of method 'ObjCRuntime.Runtime.FindClosedMethod(Type, MethodBase)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 1279 */, "ObjCRuntime.Runtime.FindPropertyInfo(MethodInfo): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Reflection.MemberInfo.DeclaringType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 1673 */, "ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
@ -43,8 +39,6 @@ namespace Xamarin.Tests {
break;
case ApplePlatform.MacCatalyst:
expectedWarnings = new ExpectedBuildMessage [] {
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 541 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveMethod(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 528 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveType(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 2434 */, "ObjCRuntime.Runtime.FindClosedMethod(Type, MethodBase): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'closed_type' of method 'ObjCRuntime.Runtime.FindClosedMethod(Type, MethodBase)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 1279 */, "ObjCRuntime.Runtime.FindPropertyInfo(MethodInfo): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Reflection.MemberInfo.DeclaringType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 1673 */, "ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
@ -75,8 +69,6 @@ namespace Xamarin.Tests {
new ExpectedBuildMessage ("src/ObjCRuntime/Blocks.cs" /* line 154 */, "ObjCRuntime.BlockLiteral.FindTrampoline(Type, String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethod(String, BindingFlags)'. The parameter 'trampolineType' of method 'ObjCRuntime.BlockLiteral.FindTrampoline(Type, String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Blocks.cs" /* line 213 */, "ObjCRuntime.BlockLiteral.SetupBlock(Delegate, Delegate, Boolean): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'ObjCRuntime.UserDelegateTypeAttribute.UserDelegateType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Blocks.cs" /* line 313 */, "ObjCRuntime.BlockLiteral.SetupBlock(Delegate, Delegate): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'ObjCRuntime.MonoPInvokeCallbackAttribute.DelegateType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 541 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveMethod(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 528 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveType(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 180 */, "Registrar.DynamicRegistrar.<FindMethods>d__16.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.FindMethods(Type, String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 206 */, "Registrar.DynamicRegistrar.CollectConstructors(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.CollectConstructors(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 211 */, "Registrar.DynamicRegistrar.CollectMethods(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.CollectMethods(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
@ -112,8 +104,6 @@ namespace Xamarin.Tests {
new ExpectedBuildMessage ("src/Foundation/NSObject2.cs" /* line 554 */, "Foundation.NSObject.DynamicConformsToProtocol(NativeHandle): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Blocks.cs" /* line 154 */, "ObjCRuntime.BlockLiteral.FindTrampoline(Type, String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethod(String, BindingFlags)'. The parameter 'trampolineType' of method 'ObjCRuntime.BlockLiteral.FindTrampoline(Type, String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Blocks.cs" /* line 213 */, "ObjCRuntime.BlockLiteral.SetupBlock(Delegate, Delegate, Boolean): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'ObjCRuntime.UserDelegateTypeAttribute.UserDelegateType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 541 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveMethod(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 528 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveType(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 180 */, "Registrar.DynamicRegistrar.<FindMethods>d__16.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.FindMethods(Type, String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 206 */, "Registrar.DynamicRegistrar.CollectConstructors(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.CollectConstructors(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 211 */, "Registrar.DynamicRegistrar.CollectMethods(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.CollectMethods(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
@ -151,8 +141,6 @@ namespace Xamarin.Tests {
new ExpectedBuildMessage ("src/Foundation/NSObject2.cs" /* line 554 */, "Foundation.NSObject.DynamicConformsToProtocol(NativeHandle): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Blocks.cs" /* line 154 */, "ObjCRuntime.BlockLiteral.FindTrampoline(Type, String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethod(String, BindingFlags)'. The parameter 'trampolineType' of method 'ObjCRuntime.BlockLiteral.FindTrampoline(Type, String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Blocks.cs" /* line 213 */, "ObjCRuntime.BlockLiteral.SetupBlock(Delegate, Delegate, Boolean): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'ObjCRuntime.UserDelegateTypeAttribute.UserDelegateType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 541 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveMethod(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 528 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveType(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 180 */, "Registrar.DynamicRegistrar.<FindMethods>d__16.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.FindMethods(Type, String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 206 */, "Registrar.DynamicRegistrar.CollectConstructors(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.CollectConstructors(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 211 */, "Registrar.DynamicRegistrar.CollectMethods(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.CollectMethods(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),