diff --git a/docs/code/class-handles.md b/docs/code/class-handles.md index 48255fb5aa6a..998267728cd9 100644 --- a/docs/code/class-handles.md +++ b/docs/code/class-handles.md @@ -14,6 +14,18 @@ It's highly desirable to use a direct native reference to Objective-C classes wh On the other hand there's one scenario when a direct native reference is not desirable: when the native Objective-C class does not exist. +This can happen for third-party bindings that declare a `[BaseType (typeof (NSObject))]` +type for something that is a protocol - and not a class - natively (bgen generates a +`[Protocol]` attribute on such a type). There's no native Objective-C class for these types +(the static registrar doesn't even register them), so a direct native reference would turn +into a hard link error (`Undefined symbols: _OBJC_CLASS_$_TheClass`), even if the class is +never actually used at runtime. + +To avoid this, we don't inline `Class.GetHandle` when the target class has a `[Protocol]` +attribute: the call falls back to a runtime lookup instead, which returns a zero handle +(`IntPtr.Zero`) for a missing native class - the same behavior these bindings had before +`Class.GetHandle` inlining existed. + In order to create a direct native reference to Objective-C classes, we need to know the names of those Objective-C classes. ## The `InlineClassGetHandle` property diff --git a/tests/linker/link all/dotnet/shared.csproj b/tests/linker/link all/dotnet/shared.csproj index bf4a46cecf39..d257acc41efa 100644 --- a/tests/linker/link all/dotnet/shared.csproj +++ b/tests/linker/link all/dotnet/shared.csproj @@ -91,9 +91,7 @@ - - - + diff --git a/tests/linker/trimmode link/dotnet/shared.csproj b/tests/linker/trimmode link/dotnet/shared.csproj index abe1e66a3c38..cc0428443ed8 100644 --- a/tests/linker/trimmode link/dotnet/shared.csproj +++ b/tests/linker/trimmode link/dotnet/shared.csproj @@ -92,9 +92,7 @@ - - - + diff --git a/tools/dotnet-linker/Steps/InlineClassGetHandleStep.cs b/tools/dotnet-linker/Steps/InlineClassGetHandleStep.cs index 84eb9833cd17..a6f48cde6b6e 100644 --- a/tools/dotnet-linker/Steps/InlineClassGetHandleStep.cs +++ b/tools/dotnet-linker/Steps/InlineClassGetHandleStep.cs @@ -194,6 +194,17 @@ bool isOurOwnCode () } if (objCType is not null) { + if (objCType.IsFakeProtocol) { + // This is a class with a [Protocol] attribute (typically a binding that declares a + // [BaseType (typeof (NSObject))] type for something that is a protocol - and not a class - + // natively). The static registrar doesn't register these classes, so there's no native + // Objective-C class to reference, which means inlining the call to Class.GetHandle would + // turn into a hard link error ('Undefined symbols: _OBJC_CLASS_$_X'). Don't inline it, so + // that the call falls back to a runtime lookup (which returns a zero handle for a missing class). + App.Log (3, "Not inlining the call to Class.GetHandle (\"{0}\") in method {1} because the class has a [Protocol] attribute, so there's no native Objective-C class to reference.", objectiveCClassName, FormatMethod (method)); + continue; + } + if (DerivedLinkContext.App.IsSimulatorBuild) { if (DerivedLinkContext.HasAvailabilityAttributesShowingUnavailableInSimulator (objCType.Type.Resolve (), method)) { App.Log (3, "Not inlining the call to Class.GetHandle (\"{0}\") in method {1} because the type is marked with an attribute indicating it's not available in the simulator.", objectiveCClassName, FormatMethod (method));