From 524b04e89619eb9b5da89033b56c35eae6f6200a Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 18 Feb 2026 16:22:07 +0100 Subject: [PATCH] [ObjCRuntime] Enable nullability and clean up TrampolineBlockBase. This is file 5 of 7 files with nullability disabled in ObjCRuntime. * Enable nullability (#nullable enable). * Add nullability annotations and validation for block pointers and managed delegate retrieval. * Add missing XML documentation for the public type and protected members. * Fix wording typo in infrastructure comment. Contributes towards https://github.com/dotnet/macios/issues/17285. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/ObjCRuntime/TrampolineBlockBase.cs | 17 +++++++++++++---- src/UIKit/UICellAccessory.cs | 2 +- src/UIKit/UIConfigurationColorTransformer.cs | 2 +- src/bgen/Generator.cs | 2 +- .../cecil-tests/Documentation.KnownFailures.txt | 3 --- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/ObjCRuntime/TrampolineBlockBase.cs b/src/ObjCRuntime/TrampolineBlockBase.cs index 9f2a519d6b60..33a0e8a5504b 100644 --- a/src/ObjCRuntime/TrampolineBlockBase.cs +++ b/src/ObjCRuntime/TrampolineBlockBase.cs @@ -3,12 +3,12 @@ using System.ComponentModel; -// Disable until we get around to enable + fix any issues. -#nullable disable +#nullable enable namespace ObjCRuntime { - // infrastucture code - not intended to be used directly by user code + /// Base type for Objective-C trampoline blocks. + // infrastructure code - not intended to be used directly by user code [EditorBrowsable (EditorBrowsableState.Never)] public abstract class TrampolineBlockBase { @@ -23,6 +23,8 @@ internal TrampolineBlockBase (IntPtr block, bool owns) } } + /// Initializes a new instance from a block literal pointer. + /// A pointer to a native . protected unsafe TrampolineBlockBase (BlockLiteral* block) : this ((IntPtr) block, false) { @@ -33,12 +35,19 @@ protected unsafe TrampolineBlockBase (BlockLiteral* block) Runtime.ReleaseBlockOnMainThread (blockPtr); } + /// Gets the native block pointer for this instance. + /// The native block pointer. protected IntPtr BlockPointer { get { return blockPtr; } } - protected unsafe static object GetExistingManagedDelegate (IntPtr block) + /// Gets the existing managed delegate for a block if it wraps managed code. + /// The native block pointer. + /// The managed delegate if this is a managed block; otherwise, . + protected unsafe static object? GetExistingManagedDelegate (IntPtr block) { + if (block == IntPtr.Zero) + return null; if (!BlockLiteral.IsManagedBlock (block)) return null; return ((BlockLiteral*) block)->Target; diff --git a/src/UIKit/UICellAccessory.cs b/src/UIKit/UICellAccessory.cs index 2b185775d881..a743fbb1aa7e 100644 --- a/src/UIKit/UICellAccessory.cs +++ b/src/UIKit/UICellAccessory.cs @@ -90,7 +90,7 @@ public unsafe NIDUICellAccessoryPosition (BlockLiteral* block) : base (block) { if (block == IntPtr.Zero) return null; - var del = (UICellAccessoryPosition) GetExistingManagedDelegate (block); + var del = (UICellAccessoryPosition?) GetExistingManagedDelegate (block); return del ?? new NIDUICellAccessoryPosition ((BlockLiteral*) block).Invoke; } diff --git a/src/UIKit/UIConfigurationColorTransformer.cs b/src/UIKit/UIConfigurationColorTransformer.cs index 5d75a8b1c7e0..7054516460f9 100644 --- a/src/UIKit/UIConfigurationColorTransformer.cs +++ b/src/UIKit/UIConfigurationColorTransformer.cs @@ -64,7 +64,7 @@ public unsafe NIDUIConfigurationColorTransformerHandler (BlockLiteral* block) : { if (block == IntPtr.Zero) return null; - var del = (UIConfigurationColorTransformerHandler) GetExistingManagedDelegate (block); + var del = (UIConfigurationColorTransformerHandler?) GetExistingManagedDelegate (block); return del ?? new NIDUIConfigurationColorTransformerHandler ((BlockLiteral*) block).Invoke; } diff --git a/src/bgen/Generator.cs b/src/bgen/Generator.cs index ea5ada12ea96..8f49ea31dd29 100644 --- a/src/bgen/Generator.cs +++ b/src/bgen/Generator.cs @@ -1681,7 +1681,7 @@ void GenerateTrampolinesForQueue (TrampolineInfo [] queue) print ("public unsafe static {0}? Create (IntPtr block)\n{{", ti.UserDelegate); indent++; print ("if (block == IntPtr.Zero)"); indent++; print ("return null;"); indent--; - print ($"var del = ({ti.UserDelegate}) GetExistingManagedDelegate (block);"); + print ($"var del = ({ti.UserDelegate}?) GetExistingManagedDelegate (block);"); print ($"return del ?? new {ti.NativeInvokerName} ((BlockLiteral *) block).Invoke;"); indent--; print ("}"); print (""); diff --git a/tests/cecil-tests/Documentation.KnownFailures.txt b/tests/cecil-tests/Documentation.KnownFailures.txt index 0a3dc185681b..028cfab1de5e 100644 --- a/tests/cecil-tests/Documentation.KnownFailures.txt +++ b/tests/cecil-tests/Documentation.KnownFailures.txt @@ -15322,9 +15322,7 @@ M:ObjCRuntime.ThrowHelper.ThrowArgumentNullException(System.String) M:ObjCRuntime.ThrowHelper.ThrowArgumentOutOfRangeException(System.String,System.Object,System.String) M:ObjCRuntime.ThrowHelper.ThrowArgumentOutOfRangeException(System.String,System.String) M:ObjCRuntime.ThrowHelper.ThrowObjectDisposedException(System.Object) -M:ObjCRuntime.TrampolineBlockBase.#ctor(ObjCRuntime.BlockLiteral*) M:ObjCRuntime.TrampolineBlockBase.Finalize -M:ObjCRuntime.TrampolineBlockBase.GetExistingManagedDelegate(System.IntPtr) M:ObjCRuntime.TransientAttribute.#ctor M:OpenGL.CGLContext.Release M:OpenGL.CGLContext.Retain @@ -22960,7 +22958,6 @@ P:ObjCRuntime.ObjCException.Message P:ObjCRuntime.ObjCException.Name P:ObjCRuntime.ObjCException.NSException P:ObjCRuntime.ObjCException.Reason -P:ObjCRuntime.TrampolineBlockBase.BlockPointer P:OSLog.IOSLogEntryFromProcess.ActivityIdentifier P:OSLog.IOSLogEntryFromProcess.Process P:OSLog.IOSLogEntryFromProcess.ProcessIdentifier