[NativeAOT] Fix ILC warning for NSObject.RegisterToggleRef (#18889)

This PR implements a workaround for the following build warning:

```
ILC: Method '[Microsoft.iOS]Foundation.NSObject.RegisterToggleRef(NSObject,native int,bool)' will always throw because:
Invalid IL or CLR metadata in 'Void Foundation.NSObject.RegisterToggleRef(Foundation.NSObject, IntPtr, Boolean)'
```

Ref #18524
This commit is contained in:
Šimon Rozsíval 2023-09-06 12:13:30 +02:00 коммит произвёл GitHub
Родитель ed23576fea
Коммит fb86b95849
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 37 добавлений и 0 удалений

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

@ -1220,6 +1220,20 @@ namespace Xamarin.Linker {
}
}
#if NET
public bool TryGet_NSObject_RegisterToggleRef (out MethodDefinition? md)
{
// the NSObject.RegisterToggleRef method isn't present on all platforms (for example on Mac)
try {
_ = GetMethodReference (PlatformAssembly, Foundation_NSObject, "RegisterToggleRef", "Foundation.NSObject::RegisterToggleRef", predicate: null, out md);
return true;
} catch (InvalidOperationException) {
md = null;
return false;
}
}
#endif
public void SetCurrentAssembly (AssemblyDefinition value)
{
current_assembly = value;

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

@ -168,6 +168,11 @@ namespace Xamarin.Linker {
Annotations.Mark (md);
}
// TODO: Move this to a separate "MakeEverythingWorkWithNativeAOTStep" linker step
if (App.XamarinRuntime == XamarinRuntime.NativeAOT && Configuration.Profile.IsProductAssembly (assembly)) {
ImplementNSObjectRegisterToggleRefMethodStub ();
}
abr.ClearCurrentAssembly ();
}
@ -1376,5 +1381,23 @@ namespace Xamarin.Linker {
return clonedCtor;
}
void ImplementNSObjectRegisterToggleRefMethodStub ()
{
// The NSObject.RegisterToggleRef method is a Mono icall that is unused in NativeAOT.
// The method isn't included on all platforms but when it is present, we need to modify it
// so that ILC can trim it and it doesn't report the following warning:
//
// ILC: Method '[Microsoft.iOS]Foundation.NSObject.RegisterToggleRef(NSObject,native int,bool)' will always throw because:
// Invalid IL or CLR metadata in 'Void Foundation.NSObject.RegisterToggleRef(Foundation.NSObject, IntPtr, Boolean)'
//
if (abr.TryGet_NSObject_RegisterToggleRef (out var registerToggleRef)) {
registerToggleRef!.IsPublic = false;
registerToggleRef!.IsInternalCall = false;
registerToggleRef!.CreateBody (out var il);
il.Emit (OpCodes.Ret);
}
}
}
}