{"id":47068,"date":"2025-03-13T15:52:03","date_gmt":"2025-03-13T19:52:03","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=47068"},"modified":"2025-03-13T15:52:52","modified_gmt":"2025-03-13T19:52:52","slug":"swift-proposal-import-objective-c-completion-handler-parameters-as-sendable","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2025\/03\/13\/swift-proposal-import-objective-c-completion-handler-parameters-as-sendable\/","title":{"rendered":"Swift Proposal: Import Objective-C Completion Handler Parameters As @Sendable"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0463-sendable-completion-handlers.md\">SE-0463<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0463-sendable-completion-handlers.md\"><p>The <code>@Sendable<\/code> annotation indicates that closure parameters are passed over an isolation boundary before they&rsquo;re called. A missing <code>@Sendable<\/code> annotation in a library has negative effects on clients who call the function; the caller can unknowingly introduce data races, and <a href=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0423-dynamic-actor-isolation.md\">SE-0423: Dynamic actor isolation enforcement from non-strict-concurrency contexts<\/a> injects runtime assertions for non-<code>Sendable<\/code> closure parameters that are passed into libraries that don&rsquo;t have data-race safety checking. This means that a missing <code>@Sendable<\/code> annotation can lead to a runtime crash for any code that calls the API from an actor isolated context, which is extremely painful for projects that are migrating to the Swift 6 language mode.<\/p><p>There&rsquo;s a large category of APIs with closure parameters that can be automatically identified as <code>@Sendable<\/code> functions, even if the annotation is missing: Objective-C methods with completion handler parameters. <code>@Sendable<\/code> is nearly always the right default for Objective-C completion handlers, and <a href=\"https:\/\/forums.swift.org\/t\/clang-sendability-audit-for-closures\/75557\">programmers have already been searching for an automatic way for completion handlers to be <code>@Sendable<\/code> by default when auditing Clang headers<\/a>.<\/p><p>[&#8230;]<\/p><p>If a completion handler does not cross an isolation boundary before it&rsquo;s called, the parameter can be annotated in the header with the <code>@nonSendable<\/code> attribute using <code>__attribute__((swift_attr(\"@nonSendable\")))<\/code>.<\/p><p>[&#8230;]<\/p><p>Changing this proposal later to use <code>sending<\/code> instead will pose source compatibility issues, because it would become invalid to have a protocol requirement that is imported with a <code>sending<\/code> completion handler and implement the requirement with a <code>@Sendable<\/code> completion handler. The same source compatibility issue exists for overridden class methods.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2025\/03\/03\/swift-6-1\/\">Swift 6.1<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/11\/25\/swift-vision-improving-the-approachability-of-data-race-safety\/\">Swift Vision: Improving the Approachability of Data-Race Safety<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/10\/01\/swift-concurrency-and-objective-c\/\">Swift Concurrency and Objective-C<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>SE-0463: The @Sendable annotation indicates that closure parameters are passed over an isolation boundary before they&rsquo;re called. A missing @Sendable annotation in a library has negative effects on clients who call the function; the caller can unknowingly introduce data races, and SE-0423: Dynamic actor isolation enforcement from non-strict-concurrency contexts injects runtime assertions for non-Sendable closure [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2025-03-13T19:52:05Z","apple_news_api_id":"8fba5a08-661f-476c-a0b1-ef4b72c44df4","apple_news_api_modified_at":"2025-03-13T19:52:54Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/Aj7paCGYfR2ygse9LcsRN9A","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,54,71,2200,901],"class_list":["post-47068","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-objective-c","tag-programming","tag-swift-concurrency","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/47068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=47068"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/47068\/revisions"}],"predecessor-version":[{"id":47070,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/47068\/revisions\/47070"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=47068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=47068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=47068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}