{"id":1701,"url":"https://github.com/nvzqz/Unreachable","last_synced_at":"2025-08-02T04:32:22.826Z","repository":{"id":56925401,"uuid":"96042871","full_name":"nvzqz/Unreachable","owner":"nvzqz","description":"Unreachable code path optimization hint for Swift","archived":false,"fork":false,"pushed_at":"2017-07-05T04:03:55.000Z","size":628,"stargazers_count":102,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-24T11:02:26.903Z","etag":null,"topics":["optimization","swift","unreachable"],"latest_commit_sha":null,"homepage":"https://nvzqz.github.io/Unreachable/docs/","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nvzqz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-02T18:54:26.000Z","updated_at":"2024-11-02T09:24:57.000Z","dependencies_parsed_at":"2022-08-20T22:50:31.477Z","dependency_job_id":null,"html_url":"https://github.com/nvzqz/Unreachable","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nvzqz%2FUnreachable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nvzqz%2FUnreachable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nvzqz%2FUnreachable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nvzqz%2FUnreachable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nvzqz","download_url":"https://codeload.github.com/nvzqz/Unreachable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228439102,"owners_count":17920018,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["optimization","swift","unreachable"],"created_at":"2024-01-05T20:15:53.726Z","updated_at":"2024-12-06T08:31:29.738Z","avatar_url":"https://github.com/nvzqz.png","language":"Swift","funding_links":[],"categories":["Optimization"],"sub_categories":["Other free courses","Push Notification Providers","Local Notifications"],"readme":"[![Unreachable](https://github.com/nvzqz/Unreachable/raw/assets/banner.png)](https://github.com/nvzqz/Unreachable)\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/platform-ios%20%7C%20macos%20%7C%20watchos%20%7C%20tvos%20%7C%20linux-lightgrey.svg\" alt=\"Platform\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/language-swift-orange.svg\" alt=\"Language: Swift\"\u003e\n\u003ca href=\"https://cocoapods.org/pods/Unreachable\"\u003e\u003cimg src=\"https://img.shields.io/cocoapods/v/Unreachable.svg\" alt=\"CocoaPods - Unreachable\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/Carthage/Carthage\"\u003e\u003cimg src=\"https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat\" alt=\"Carthage\"\u003e\u003c/a\u003e\n\u003ca href=\"https://codebeat.co/projects/github-com-nvzqz-unreachable-master\"\u003e\u003cimg src=\"https://codebeat.co/badges/3e01a261-54d1-400e-8820-6aadb177c01a\" alt=\"codebeat badge\"\u003e\u003c/a\u003e\n\u003cimg src=\"https://img.shields.io/badge/license-MIT-000000.svg\" alt=\"License\"\u003e\n\u003c/p\u003e\n\nUnreachable is a Swift µframework that allows for letting the compiler know when\na code path is unreachable.\n\n- [Build Status](#build-status)\n- [Installation](#installation)\n    - [Compatibility](#compatibility)\n    - [Swift Package Manager](#install-using-swift-package-manager)\n    - [CocoaPods](#install-using-cocoapods)\n    - [Carthage](#install-using-carthage)\n- [Usage](#usage)\n    - [Dynamic Loop Exit](#dynamic-loop-exit)\n    - [Switch Conditions](#switch-conditions)\n    - [Safety](#safety)\n    - [Unreachable vs fatalError](#unreachable-vs-fatalerror)\n        - [Unreachable Example](#with-unreachable)\n        - [fatalError Example](#with-fatalerror)\n- [License](#license)\n\n## Build Status\n\n| Branch   | Status |\n| :------: | :----: |\n| `master` | [![Build Status](https://travis-ci.org/nvzqz/Unreachable.svg?branch=master)](https://travis-ci.org/nvzqz/Unreachable)\n\n## Installation\n\n### Compatibility\n\n- Platforms:\n    - macOS 10.9+\n    - iOS 8.0+\n    - watchOS 2.0+\n    - tvOS 9.0+\n    - Linux\n- Xcode 8.0+\n- Swift 3.0+ \u0026 4.0\n\n### Install Using Swift Package Manager\nThe [Swift Package Manager](https://swift.org/package-manager/) is a\ndecentralized dependency manager for Swift.\n\n1. Add the project to your `Package.swift`.\n\n    ```swift\n    import PackageDescription\n\n    let package = Package(\n        name: \"MyAwesomeProject\",\n        dependencies: [\n            .Package(url: \"https://github.com/nvzqz/Unreachable.git\",\n                     majorVersion: 1)\n        ]\n    )\n    ```\n\n2. Import the Unreachable module.\n\n    ```swift\n    import Unreachable\n    ```\n\n### Install Using CocoaPods\n[CocoaPods](https://cocoapods.org/) is a centralized dependency manager for\nObjective-C and Swift. Go [here](https://guides.cocoapods.org/using/index.html)\nto learn more.\n\n1. Add the project to your [Podfile](https://guides.cocoapods.org/using/the-podfile.html).\n\n    ```ruby\n    use_frameworks!\n\n    pod 'Unreachable', '~\u003e 1.2.0'\n    ```\n\n    If you want to be on the bleeding edge, replace the last line with:\n\n    ```ruby\n    pod 'Unreachable', :git =\u003e 'https://github.com/nvzqz/Unreachable.git'\n    ```\n\n2. Run `pod install` and open the `.xcworkspace` file to launch Xcode.\n\n3. Import the Unreachable framework.\n\n    ```swift\n    import Unreachable\n    ```\n\n### Install Using Carthage\n[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency\nmanager for Objective-C and Swift.\n\n1. Add the project to your [Cartfile](https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md#cartfile).\n\n    ```\n    github \"nvzqz/Unreachable\"\n    ```\n\n2. Run `carthage update` and follow [the additional steps](https://github.com/Carthage/Carthage#getting-started)\n   in order to add Unreachable to your project.\n\n3. Import the Unreachable framework.\n\n    ```swift\n    import Unreachable\n    ```\n\n### Install Manually\n\nSimply add `Unreachable.swift` into your project.\n\n## Usage\n\nTry it out for yourself! Download the repo and open 'Unreachable.playground'.\n\n### Dynamic Loop Exit\n\nIn some cases, the only way a function returns a value is from within a loop,\nbut the compiler may not have enough information to know that.\n\n```swift\nfunc getValue() -\u003e Int {\n    for i in 0... {\n        if i == 20 {\n            return i\n        }\n    }\n    assertUnreachable()\n}\n```\n\n### Switch Conditions\n\nA `switch` statement may have conditions applied to its branches that make it\nexhaustive, but that may not obvious to the compiler.\n\n```swift\nfunc sign(of value: Double?) -\u003e FloatingPointSign? {\n    switch value {\n    case let x? where x \u003e= 0:\n        return .plus\n    case let x? where x \u003c 0:\n        return .minus\n    case .some:\n        assertUnreachable()\n    case .none:\n        return nil\n    }\n}\n```\n\n### Safety\n\nIt is [undefined behavior][ub] for `unreachable()` to be called. To protect\nagainst this, it is recommended to use `assertUnreachable()` instead.\n\nWith `assertUnreachable()`, debug builds will exit via a fatal error if the\nfunction is called. In optimized builds, it's no different than calling\n`unreachable()`.\n\n### Unreachable vs `fatalError()`\n\nThe `assertUnreachable()` function can be used as somewhat of a drop-in\nreplacement for `fatalError()`. In debug mode, they emit similar instructions.\nHowever, when compiling with optimizations, `assertUnreachable()` allows its\nparent to emit very few instructions.\n\nHere we're checking whether a `UnicodeScalar` has a value in the lower or upper\nrange. Because we know that these are the only valid ranges, we can let the\ncompiler know that the third branch is unreachable. If at some point `x` has a\nvalue that's not within either range, it will emit an assertion failure in\nunoptimized builds.\n\n#### With Unreachable\n\n```swift\nfunc isLowerRange(_ x: UnicodeScalar) -\u003e Bool {\n    switch x.value {\n    case 0...0xD7FF:\n        return true\n    case 0xE000...0x10FFFF:\n        return false\n    default:\n        assertUnreachable()\n    }\n}\n```\n\nAssembly output:\n\n```assembly\n        .globl  __T011Unreachable12isLowerRangeSbs7UnicodeO6ScalarVF\n        .p2align        4, 0x90\n__T011Unreachable12isLowerRangeSbs7UnicodeO6ScalarVF:\n        pushq   %rbp\n        movq    %rsp, %rbp\n        cmpl    $55296, %edi\n        setb    %al\n        popq    %rbp\n        retq\n```\n\n#### With `fatalError()`\n\n```swift\nfunc isLowerRange(_ x: UnicodeScalar) -\u003e Bool {\n    switch x.value {\n    case 0...0xD7FF:\n        return true\n    case 0xE000...0x10FFFF:\n        return false\n    default:\n        fatalError(\"Unreachable\")\n    }\n}\n```\n\nAssembly output:\n\n```assembly\n        .globl  __T011Unreachable12isLowerRangeSbs7UnicodeO6ScalarVF\n        .p2align        4, 0x90\n__T011Unreachable12isLowerRangeSbs7UnicodeO6ScalarVF:\n        .cfi_startproc\n        movb    $1, %al\n        cmpl    $55296, %edi\n        jb      LBB4_3\n        addl    $-57344, %edi\n        cmpl    $1056768, %edi\n        jae     LBB4_4\n        xorl    %eax, %eax\nLBB4_3:\n        retq\nLBB4_4:\n        pushq   %rbp\nLcfi0:\n        .cfi_def_cfa_offset 16\nLcfi1:\n        .cfi_offset %rbp, -16\n        movq    %rsp, %rbp\nLcfi2:\n        .cfi_def_cfa_register %rbp\n        subq    $48, %rsp\n        leaq    L___unnamed_2(%rip), %rax\n        movq    %rax, (%rsp)\n        movl    $0, 32(%rsp)\n        movq    $56, 24(%rsp)\n        movl    $2, 16(%rsp)\n        movq    $69, 8(%rsp)\n        leaq    L___unnamed_3(%rip), %rdi\n        leaq    L___unnamed_4(%rip), %rcx\n        movl    $11, %esi\n        movl    $2, %edx\n        movl    $11, %r8d\n        xorl    %r9d, %r9d\n        callq   __T0s17_assertionFailures5NeverOs12StaticStringV_SSAE4fileSu4lines6UInt32V5flagstFTfq4nxnnn_n\n        subq    $40, %rsp\n        .cfi_endproc\n```\n\n## License\n\nAll source code for Unreachable is released under the [MIT License][license].\n\nAssets for Unreachable are released under the [CC BY-SA 4.0 License][assets-license]\nand can be found in the [`assets` branch](https://github.com/nvzqz/Unreachable/tree/assets).\n\n[ub]: https://en.wikipedia.org/wiki/Undefined_behavior\n[license]: https://github.com/nvzqz/Unreachable/blob/master/LICENSE.md\n[assets-license]: https://github.com/nvzqz/Unreachable/blob/assets/LICENSE.txt\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnvzqz%2FUnreachable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnvzqz%2FUnreachable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnvzqz%2FUnreachable/lists"}