[sharpie] Emit correct nullability for block/delegate parameters#25837
[sharpie] Emit correct nullability for block/delegate parameters#25837rolfbjarne wants to merge 7 commits into
Conversation
When ObjC declares a block with nullable parameters (e.g., void (^)(BOOL, NSError * _Nullable)), sharpie now produces Action<bool, NSError?> instead of Action<bool, NSError>. This also applies to the return type of Func<> delegates. The logic checks for CX_AttrKind_TypeNullable annotations on resolved types and wraps them with ComposedType.HasNullableSpecifier to produce the '?' suffix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When CustomDelegateMassager or BindingGenerator convert inline Action<>/Func<> types to named delegate declarations, unwrap nullable ComposedTypes (T?) back to their base type. This preserves the annotations so NullabilityMassager can add [NullAllowed] to the delegate parameter instead, which is the correct representation for binding definition delegate parameters. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Extend the NullabilityCheck to inspect block/delegate parameters for nullability mismatches between native and managed code. When a method parameter is a block type (e.g., void (^)(BOOL, NSError *)), the check now validates nullability of each block parameter against the NullableAttribute byte array on the managed generic type arguments. Reports !missing-null-allowed! and !extra-null-allowed! for block parameter nullability mismatches. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When ObjC declares a block with nullable parameters (e.g., void (^)(BOOL, NSError * _Nullable)), sharpie now produces Action<bool, NSError?> instead of Action<bool, NSError>. This also applies to the return type of Func<> delegates. The logic checks for CX_AttrKind_TypeNullable annotations on resolved types and wraps them with ComposedType.HasNullableSpecifier to produce the '?' suffix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When CustomDelegateMassager or BindingGenerator convert inline Action<>/Func<> types to named delegate declarations, unwrap nullable ComposedTypes (T?) back to their base type. This preserves the annotations so NullabilityMassager can add [NullAllowed] to the delegate parameter instead, which is the correct representation for binding definition delegate parameters. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Extend the NullabilityCheck to inspect block/delegate parameters for nullability mismatches between native and managed code. When a method parameter is a block type (e.g., void (^)(BOOL, NSError *)), the check now validates nullability of each block parameter against the NullableAttribute byte array on the managed generic type arguments. Reports !missing-null-allowed! and !extra-null-allowed! for block parameter nullability mismatches. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…/sharpie-block-nullability
This comment has been minimized.
This comment has been minimized.
✅ [PR Build #898c0bb] Build passed (Detect API changes) ✅Pipeline on Agent |
✅ [PR Build #898c0bb] Build passed (Build packages) ✅Pipeline on Agent |
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
✅ [PR Build #898c0bb] Build passed (Build macOS tests) ✅Pipeline on Agent |
🔥 [CI Build #898c0bb] Test results 🔥Test results❌ Tests failed on VSTS: test results 0 tests crashed, 3 tests failed, 196 tests passed. Failures❌ monotouch tests (MacCatalyst)1 tests failed, 16 tests passed.Failed tests
Html Report (VSDrops) Download ❌ sharpie tests1 tests failed, 0 tests passed.Failed tests
Html Report (VSDrops) Download ❌ xtro tests1 tests failed, 0 tests passed.Failed tests
Html Report (VSDrops) Download Successes✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download macOS tests✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
When ObjC declares a block with nullable parameters (e.g.,
void (^)(BOOL success, NSError * _Nullable error)), sharpie previously producedAction<bool, NSError>, losing the nullability information. This PR teaches sharpie to produceAction<bool, NSError?>instead.Approach
The fix operates at two levels in the sharpie pipeline:
TypeBinder.VisitFunctionType -- After resolving each block parameter type, checks for
CX_AttrKind_TypeNullablein theAttributedTypeannotations (which clang already attaches). If found, wraps the resolved type withComposedType.HasNullableSpecifier = trueto produce the?suffix in type arguments. This handles the inlineAction<>/Func<>case.CustomDelegateMassager + BindingGenerator -- When converting inline delegates to named delegate declarations, unwraps the nullable
ComposedTypeback to its base type while preserving annotations. This allows the existingNullabilityMassagerto add[NullAllowed]to the delegate parameter (the correct representation for binding definition delegates).xtro-sharpie validation
Extends
NullabilityCheckto validate block parameter nullability:BlockPointerTypeparameters and inspects the underlyingFunctionProtoTypeNullableAttributebyte array positions!missing-null-allowed!/!extra-null-allowed!for mismatchesThis addresses the existing TODO in
NullabilityCheck: "check the generics (don't think we have such cases yet)".🤖 Pull request created by Copilot