{"id":46501,"date":"2025-01-22T13:52:10","date_gmt":"2025-01-22T18:52:10","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=46501"},"modified":"2025-01-22T13:52:10","modified_gmt":"2025-01-22T18:52:10","slug":"swift-proposal-opt-in-strict-memory-safety-checking","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2025\/01\/22\/swift-proposal-opt-in-strict-memory-safety-checking\/","title":{"rendered":"Swift Proposal: Opt-in Strict Memory Safety Checking"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0458-strict-memory-safety.md\">SE-0458<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0458-strict-memory-safety.md\"><p>For example, Swift solves null references with optional types. Statically, Swift prevents you from using an optional reference without checking it first. If you&rsquo;re sure it&rsquo;s non-null, you can use the <code>!<\/code> operator, which is safe because Swift will dynamically check for <code>nil<\/code>. If you really can&rsquo;t afford that dynamic check, you can use <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/optional\/unsafelyunwrapped\"><code>unsafelyUnwrapped<\/code><\/a>. This can still be correct if you can prove that the reference is definitely non-null for some reason that Swift doesn&rsquo;t know. But it is an unsafe feature because it admits violations if you&rsquo;re wrong.<\/p><p>[&#8230;]<\/p><p>A compiler flag <code>-strict-memory-safety<\/code> that enables warnings for all uses of unsafe constructs within a given module. All warnings will be in the diagnostic group <code>Unsafe<\/code>, enabling precise control over memory-safety-related warnings per <a href=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0443-warning-control-flags.md\">SE-0443<\/a>. When strict memory safety is enabled, the <code>StrictMemorySafety<\/code> feature will be set: <code>#if hasFeature(StrictMemorySafety)<\/code> can be used to detect when Swift code is being compiled in this mode.<\/p><p>[&#8230;]<\/p><p>Because <code>UnsafeBufferPointer<\/code> and <code>UnsafePointer<\/code> are <code>@unsafe<\/code> types, this code will get a warning regardless of whether the declarations were marked <code>@unsafe<\/code>, because having unsafe types in the signature of a declaration implies that they are <code>@unsafe<\/code>. This helps us identify more unsafe code even when the libraries we depend on haven&rsquo;t enabled strict safety checking themselves.<\/p><p>To suppress these warnings, the expressions involving unsafe code must be marked with <code>unsafe<\/code> in the same manner as one would mark a throwing expression with <code>try<\/code> or an asynchronous expression with <code>async<\/code>.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/12\/05\/swift-proposal-precise-control-flags-over-compiler-warnings\/\">Swift Proposal: Precise Control Flags Over Compiler Warnings<\/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\/09\/20\/unwanted-swift-concurrency-checking\/\">Unwanted Swift Concurrency Checking<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/02\/12\/the-evolution-of-safe-and-unsafe-in-swift\/\">The Evolution of &ldquo;safe&rdquo; and &ldquo;unsafe&rdquo; in Swift<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/02\/05\/swift-5-exclusivity-enforcement\/\">Swift 5 Exclusivity Enforcement<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/03\/03\/why-safe-c-is-sometimes-unsafe-swift\/\">Why Safe C Is Sometimes Unsafe Swift<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>SE-0458: For example, Swift solves null references with optional types. Statically, Swift prevents you from using an optional reference without checking it first. If you&rsquo;re sure it&rsquo;s non-null, you can use the ! operator, which is safe because Swift will dynamically check for nil. If you really can&rsquo;t afford that dynamic check, you can use [&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-01-22T18:52:13Z","apple_news_api_id":"dfdc2118-c9c2-4243-af15-65a727c6b537","apple_news_api_modified_at":"2025-01-22T18:52:13Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A39whGMnCQkOvFWWnJ8a1Nw","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,571,71,48,901],"class_list":["post-46501","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-memory-management","tag-programming","tag-security","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/46501","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=46501"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/46501\/revisions"}],"predecessor-version":[{"id":46502,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/46501\/revisions\/46502"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=46501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=46501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=46501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}