# \[Pitch\] Exempt code paths ending in \`preconditionFailure\` from coverage checks when running tests

**URL:** <https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593>\
**Category:** Pitches\
**Tags:** testing, code-coverage, xctest\
**Created:** [January 17, 2023, 12:27am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593 "2023-01-17T00:27:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CharlesS](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/charless/32/6488_2.png) [@CharlesS](https://forums.swift.org/u/CharlesS)\
**Post date:** [January 17, 2023, 12:27am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/1 "2023-01-17T00:27:00Z")

</div>

Title says it all, really.

```swift
if someFatalConditionThatShouldNeverOccur {
    preconditionFailure("Uh oh, the dev team made a huge mistake!!")
}

```

The above branch cannot be unit tested, because doing so would cause the test process to crash. And, after all, the whole point of `preconditionFailure` is that the code execution should _never_ get there.

How feasible would it be to just make `preconditionFailure` branches get ignored by the coverage checks?

Bonus points if we could also exclude the `message` parameter from the `precondition()` function so that this wouldn't set off the coverage checker:

```swift
precondition(somePrereq, "Coverage check complains unless you leave this out")

```

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [January 17, 2023, 1:26am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/2 "2023-01-17T01:26:53Z")

</div>

@hamishknight any thoughts on this?

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [January 17, 2023, 1:44am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/3 "2023-01-17T01:44:35Z")

</div>

Rather than just considering just `fatalError`/`preconditionFailure` and other similar stdlib functions, to achieve the desired effect I think we'd need to exclude any regions that definitively end in a call to a (non-throwing) `Never`-returning function. Otherwise, you'd fail to identify situations like this:

```swift
if someFatalConditionThatShouldNeverOccur {
  logSomething() // line still marked as uncovered
  fatalError("There can be no recovery from this")
}

```

or basically any situation where your failure case goes into another function that unconditionally ends in something like `fatalError`:

```swift
if someFatalConditionThatShouldNeverOccur {
  // No statements in this block can be covered
  logSomething()
  myCustomFatalErrorFunction()
}

// This function can never be covered
func myCustomFatalErrorFunction() -> Never {
  doSomethingElse()
  fatalError()
}

```

Things get a little trickier with branch coverage (does Swift support this? I don't remember), since you'd also want to exclude the branches that would allow that non-coverable code to be entered.

---

<div class="post-metadata">

**Author:** ![CharlesS](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/charless/32/6488_2.png) [@CharlesS](https://forums.swift.org/u/CharlesS)\
**Post date:** [January 17, 2023, 2:11am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/4 "2023-01-17T02:11:22Z")

</div>

I'm somewhat loath to expand the problem such that it makes it difficult to implement, because that reduces the chances that it ever _will_ be implemented. Something simple that I can find ways to work around is better than nothing.

Heck, it'd be an improvement over the status quo even if we just had an annotation we could drop in:

```swift
if someFatalConditionThatShouldNeverOccur {
    // code-coverage: ignore-block (or something like that)
    logSomething("Hold my beer!")
    fatalError("Aieee!!! *BONK*")
}

```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 17, 2023, 4:29am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/5 "2023-01-17T04:29:39Z")

</div>

Wouldn’t it be better if we _could_ test these code paths?

> <https://stackoverflow.com/questions/32873212/unit-test-fatalerror-in-swift>

---

<div class="post-metadata">

**Author:** ![fclout](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fclout/32/36155_2.png) [@fclout](https://forums.swift.org/u/fclout)\
**Post date:** [January 17, 2023, 4:33am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/6 "2023-01-17T04:33:47Z")

</div>

If the code path ends in `fatalError` because it's unreachable but it can't be proved to the front-end, it might still be impossible to test.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 17, 2023, 4:45am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/7 "2023-01-17T04:45:36Z")

</div>

> [@fclout](#):
>
> If the code path ends in `fatalError` because it's unreachable but it can't be proved to the front-end, it might still be impossible to test.

Sure, I can buy that argument where one is asserting that a code path is _unreachable_.

I don’t think I’d agree with OP that such cases are the “whole point” of `fatalError`, though, and write them off across-the-board as untestable.

Indeed, the Swift standard library has internal testing facilities for expected crashes, and I’ve had many occasions where I wished I could have done the same with my own code just as the StackOverflow conversation participants have found.

---

<div class="post-metadata">

**Author:** ![CharlesS](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/charless/32/6488_2.png) [@CharlesS](https://forums.swift.org/u/CharlesS)\
**Post date:** [January 17, 2023, 5:04am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/8 "2023-01-17T05:04:21Z")

</div>

From that answer:

```swift
// backup of the original Swift `fatalError`
private static let defaultFatalErrorClosure = { Swift.fatalError($0, file: $1, line: $2) }

```

This line right here is going to set off the coverage checker, because the contents of that closure are not going to be covered. So this doesn't really solve the problem, it just moves it.

---

<div class="post-metadata">

**Author:** ![CharlesS](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/charless/32/6488_2.png) [@CharlesS](https://forums.swift.org/u/CharlesS)\
**Post date:** [January 17, 2023, 5:13am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/9 "2023-01-17T05:13:48Z")

</div>

> I don’t think I’d agree with OP that such cases are the “whole point” of `fatalError`, though, and write them off across-the-board as untestable.

What else do you use `fatalError` for? To me, if I have an error condition that I expect to be reachable, rather than an indication of a programming error, I'm going to throw an error or otherwise find a way to handle it more gracefully than that.

Compare to the `!` postfix operator, which says, "I am promising, based on some knowledge I have that the compiler is not privy to, that this variable will never be `nil`, and if it does, that is a programming error and therefore should crash." In this way, these two expressions could be equivalent in meaning:

```swift
let nonNil = typedAsOptionalButIKnowItsNotNil!

```

and

```swift
guard let nonNil = typedAsOptionalButIKnowItsNotNil else {
    fatalError("Programming Error: this thing should never be nil")
}

```

The main difference being that the less expressive version doesn't set off the coverage check, whereas the more expressive one does. 😕

---

<div class="post-metadata">

**Author:** ![filip-sakel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filip-sakel/32/8697_2.png) [@filip-sakel](https://forums.swift.org/u/filip-sakel)\
**Post date:** [January 17, 2023, 5:20am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/10 "2023-01-17T05:20:57Z")

</div>

> [@xwu](#):
>
> Indeed, the Swift standard library has internal testing facilities for expected crashes, and I’ve had many occasions where I wished I could have done the same with my own code just as the StackOverflow conversation participants have found.

What if fatal error indicates that some application state is corrupt (e.g. invalid indices to an array), and these indices are shared by many tests. After we “recover” from fatal error, do we restart the entire test execution skipping the crashing test, or do we just avoid running any further tests from the same test suite? Maybe neither is required if sharing state between tests turns out to be uncommon in the real world.

---

<div class="post-metadata">

**Author:** ![FranzBusch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franzbusch/32/19447_2.png) [@FranzBusch](https://forums.swift.org/u/FranzBusch)\
**Post date:** [January 17, 2023, 8:40am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/11 "2023-01-17T08:40:34Z")

</div>

I would love to see `fatalError` and friends becoming testable. While they are sometimes used in paths that are truly never reachable. They are also often used to catch developer errors or enforce API semantics. Making this code testable would be amazing!

---

<div class="post-metadata">

**Author:** ![tevelee](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tevelee/32/9775_2.png) [@tevelee](https://forums.swift.org/u/tevelee)\
**Post date:** [January 17, 2023, 10:14am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/12 "2023-01-17T10:14:39Z")

</div>

Matt Gallagher came up with an excellent workaround for testing these code paths:

> **[GitHub - mattgallagher/CwlPreconditionTesting: A Mach exception handler that...](https://github.com/mattgallagher/CwlPreconditionTesting)**
>
> A Mach exception handler that allows Swift precondition failures to be caught and tested. - GitHub - mattgallagher/CwlPreconditionTesting: A Mach exception handler that allows Swift precondition fa...

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 17, 2023, 10:24am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/13 "2023-01-17T10:24:48Z")

</div>

> [@CharlesS](#):
>
> This line right here is going to set off the coverage checker, because the contents of that closure are not going to be covered. So this doesn't really solve the problem, it just moves it.

Right, so adapt this solution in the test runner itself, moving this line or something equivalent into there. Your coverage checker isn’t coverage checking `XCTest`.

> [@CharlesS](#):
>
> What else do you use `fatalError` for?

A reachable but unrecoverable condition. This is a pervasive pattern and philosophy in the Swift standard library, though more so with preconditions which I’ve noticed you’ve lumped together with `fatalError` in your pitch.

> [@CharlesS](#):
>
> The main difference being that the less expressive version doesn't set off the coverage check, whereas the more expressive one does. 😕

Nit: in this specific example, narrating “this thing should never be nil” instead of writing `!` isn’t more expressive—indeed, as you say, it expresses exactly the same thing, but with more ceremony, demonstrating that it’s using _less expressive_ language features.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 17, 2023, 10:38am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/14 "2023-01-17T10:38:21Z")

</div>

> [@filip-sakel](#):
>
> What if fatal error indicates that some application state is corrupt (e.g. invalid indices to an array), and these indices are shared by many tests. After we “recover” from fatal error, do we restart the entire test execution skipping the crashing test, or do we just avoid running any further tests from the same test suite? Maybe neither is required if sharing state between tests turns out to be uncommon in the real world.

Good point. My guess would be that it’d be rare to share mutable state with a test that’s expected to fail _and_ corrupt that shared state while doing so. I’d expect that with our concurrency features now in place it would be possible to have a design for this feature that requires any state to be `Sendable` and fences off the entire region of logic that deliberately corrupts state in its own async context.

---

<div class="post-metadata">

**Author:** ![hamishknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hamishknight/32/14226_2.png) [@hamishknight](https://forums.swift.org/u/hamishknight)\
**Post date:** [January 17, 2023, 3:25pm UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/15 "2023-01-17T15:25:35Z")

</div>

> [@allevato](#):
>
> Rather than just considering just `fatalError`/`preconditionFailure` and other similar stdlib functions, to achieve the desired effect I think we'd need to exclude any regions that definitively end in a call to a (non-throwing) `Never`-returning function

Aside from the cases where `fatalError` might be used as a reachable but otherwise unrecoverable condition as mentioned by @xwu, it should alse be noted that there are some `Never` returning functions that gracefully exit e.g `exit`, which we probably wouldn't want to apply this to.

> [@allevato](#):
>
> Things get a little trickier with branch coverage (does Swift support this? I don't remember), since you'd also want to exclude the branches that would allow that non-coverable code to be entered.

If by branch coverage you're referring to [branch regions](https://www.llvm.org/docs/CoverageMappingFormat.html#mapping-region) in LLVM coverage maps, then no, the compiler does not currently support them. We currently only ever emit regular code regions. But even with just code regions, we'd need to do dominance checking to properly eliminate all regions that would unconditionally end in something unreachable. This isn't something that's straightforward to do currently since we emit coverage information while we do SILGen, which is pretty error prone as we're essentially doing ad-hoc control flow analysis. However I want to investigate pushing more of the coverage logic into the SILOptimizer pipeline, which would make this much easier and more robust to do.

> [@CharlesS](#):
>
> Heck, it'd be an improvement over the status quo even if we just had an annotation we could drop in:
> 
> ```swift
> if someFatalConditionThatShouldNeverOccur {
> // code-coverage: ignore-block (or something like that)
> logSomething("Hold my beer!")
> fatalError("Aieee!!! *BONK*")
> }
> 
> ```

Indeed, this is a somewhat commonly requested feature and is internally tracked by rdar://74839955.

---

<div class="post-metadata">

**Author:** ![CharlesS](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/charless/32/6488_2.png) [@CharlesS](https://forums.swift.org/u/CharlesS)\
**Post date:** [January 18, 2023, 2:47am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/16 "2023-01-18T02:47:25Z")

</div>

> [@xwu](#):
>
> > [@CharlesS](#):
> >
> > This line right here is going to set off the coverage checker, because the contents of that closure are not going to be covered. So this doesn't really solve the problem, it just moves it.
> 
> Right, so adapt this solution in the test runner itself, moving this line or something equivalent into there. Your coverage checker isn’t coverage checking `XCTest`.

But `XCTest` doesn't do this natively, so this will have to be in your own code—which won't be covered.

> [@](#):
>
> > [@CharlesS](#):
> >
> > What else do you use `fatalError` for?
> 
> A reachable but unrecoverable condition. This is a pervasive pattern and philosophy in the Swift standard library, though more so with preconditions which I’ve noticed you’ve lumped together with `fatalError` in your pitch.

If the team feels strongly about one or the other, I'd be willing to limit my use to one or the other of these functions, if one of them could do it. Right now, it's impossible to do this with any sort of precondition/fatal error mechanism of which I am aware.

> [@](#):
>
> > [@CharlesS](#):
> >
> > The main difference being that the less expressive version doesn't set off the coverage check, whereas the more expressive one does. 😕
> 
> Nit: in this specific example, narrating “this thing should never be nil” instead of writing `!` isn’t more expressive—indeed, as you say, it expresses exactly the same thing, but with more ceremony, demonstrating that it’s using _less expressive_ language features.

Not if the message contains information about _why_ this should be unreachable, which a real-world example would.

For a more fleshed-out example of a code path that should never be reachable, imagine we have a type that wraps several more primitive interfaces, providing a common interface to each. The backing of this may be implemented something like:

```swift
public class MyWrapperThingy {
    private enum Backing {
        case bleep(bleep_t)
        case bloop(bloop_t)
    }

    private let backing: Backing

    public init(bleep: bleep_t) {
        self.backing = .bleep(bleep)
    }

    public init(bloop: bloop_t) {
        self.backing = .bloop(bloop)
    }

    public func doSomething() {
        switch self.backing {
        case .bleep(let bleep):
            // do something with bleep
        case .bloop(let bloop):
            // do something with bloop
        }
    }
}

```

Now, suppose we want to add support for a backing type `blorp_t` that was introduced in a recent version of the OS. We will need to use availability markers:

```swift
public class MyWrapperThingy {
    private enum Backing {
        ...
        case blorp(Any) // Any because otherwise we will not be able to compile for earlier OS versions
    }

    ...

    @available(macOS whatever, *)
    public init(blorp: blorp_t) {
        self.backing = .blorp(blorp)
    }

```

And then in `doSomething`:

```swift
    public func doSomething() {
        switch self.backing {
        ...
        case blorp(let blorp):
            guard #available(macOS whatever, *) else {
                fatalError("Should be impossible to reach this point on old macOS")
            }

            ... do stuff with blorp ...
        }
    }
}

```

In the above case, it should be _literally impossible_ to reach that `fatalError`, but we have to have it to satisfy the compiler. Even if there _were_ a good way to test `fatalError`, in order to even manipulate things into a state where that path would even be reachable would require polluting the code with artificial spaghetti just to cause what should just be a programmer error.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 18, 2023, 3:31am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/17 "2023-01-18T03:31:20Z")

</div>

> [@CharlesS](#):
>
> But `XCTest` doesn't do this natively

Right, hence my first post—wouldn't it be better if Swift did offer this natively?

> [@CharlesS](#):
>
> ```swift
> case blorp(let blorp):
> guard #available(macOS whatever, *) else {
> fatalError("Should be impossible to reach this point on old macOS")
> }
> 
> ... do stuff with blorp ...
> 
> ```

Why wouldn't one write:

```swift
case blorp(let blorp):
if #available(macOS whatever, *) {
  // do stuff with blorp
}

```

In other words, why include an unreachable, untestable `fatalError`? Why's it "satisfying the compiler"?

---

<div class="post-metadata">

**Author:** ![CharlesS](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/charless/32/6488_2.png) [@CharlesS](https://forums.swift.org/u/CharlesS)\
**Post date:** [January 18, 2023, 3:38am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/18 "2023-01-18T03:38:40Z")

</div>

> [@xwu](#):
>
> > [@CharlesS](#):
> >
> > But `XCTest` doesn't do this natively
> 
> Right, hence my first post—wouldn't it be better if Swift did offer this natively?
> 
> > [@CharlesS](#):
> >
> > ```swift
> > case blorp(let blorp):
> > guard #available(macOS whatever, *) else {
> > fatalError("Should be impossible to reach this point on old macOS")
> > }
> > 
> > ... do stuff with blorp ...
> > 
> > ```
> 
> Why wouldn't one write:
> 
> ```swift
> case blorp(let blorp):
> if #available(macOS whatever, *) {
> // do stuff with blorp
> }
> 
> ```
> 
> In other words, why include an unreachable, untestable `fatalError`? Why's it "satisfying the compiler"?

Well, in the specific case I'm dealing with, a lot of these functions need to return a value. So in that case, in order to avoid a compiler error about a missing return, it's necessary either to add an unreachable `fatalError` or an unreachable dummy return value. Of the two, the `fatalError` is clearly preferable.

From a secondary, purely stylistic point of view, I prefer `#guard available` over `#if available` because it more cleanly isolates the "obsolete" code paths from the modern, preferred path. Once you can move the minimum target up such that you no longer need the availability checks, you can just delete the offending code rather than having to adjust the indentation levels of everything.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 18, 2023, 4:09am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/19 "2023-01-18T04:09:26Z")

</div>

> [@CharlesS](#):
>
> Well, in the specific case I'm dealing with, a lot of these functions need to return a value. So in that case, in order to avoid a compiler error about a missing return, it's necessary either to add an unreachable `fatalError` or an unreachable dummy return value. Of the two, the `fatalError` is clearly preferable.

In the case you’re actually dealing with (not the example you gave above), you’ve identified an example where you’re using `fatalError` to satisfy the compiler when some code path is _unreachable_.

As I said in my second post in this thread, I totally acknowledge that this is _one use case_ for `fatalError`. I’d have no such quibble with, say, proposing a Swift counterpart to an [`unreachable`](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2816.pdf) [annotation](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0627r6.pdf), particularly if paired with proposing a way to test _reachable_ fatal errors and preconditions.

However, that you’ve described one (current) use case of `fatalError` doesn’t mean that it’s the “whole point” of `fatalError`. And if one use case demands special treatment, then you’ve found some argument in favor of inventing some new way of expressing that use case, not for a change in behavior for all uses of the current feature.

---

<div class="post-metadata">

**Author:** ![CharlesS](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/charless/32/6488_2.png) [@CharlesS](https://forums.swift.org/u/CharlesS)\
**Post date:** [January 18, 2023, 4:38am UTC](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593/20 "2023-01-18T04:38:08Z")

</div>

> [@xwu](#):
>
> > [@CharlesS](#):
> >
> > Well, in the specific case I'm dealing with, a lot of these functions need to return a value. So in that case, in order to avoid a compiler error about a missing return, it's necessary either to add an unreachable `fatalError` or an unreachable dummy return value. Of the two, the `fatalError` is clearly preferable.
> 
> In the case you’re actually dealing with (not the example you gave above), you’ve identified an example where you’re using `fatalError` to satisfy the compiler when some code path is _unreachable_.
> 
> As I said in my second post in this thread, I totally acknowledge that this is _one use case_ for `fatalError`. I’d have no such quibble with, say, proposing a Swift counterpart to an [`unreachable`](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2816.pdf) [annotation](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0627r6.pdf), particularly if paired with proposing a way to test _reachable_ fatal errors and preconditions.
> 
> However, that you’ve described one (current) use case of `fatalError` doesn’t mean that it’s the “whole point” of `fatalError`. And if one use case demands special treatment, then you’ve found some argument in favor of inventing some new way of expressing that use case, not for a change in behavior for all uses of the current feature.

Well, it's not the _only_ use case that results in unreachable code; there's quite a few of them that I've seen. Off the top of my head, I can also think of:

- switch statements where, for some reason, I happen to know that one of the cases will never be reached
- switch statements that are on something other than an enum, like a series of integer ranges, that can't be formally exhaustive but which, for some reason, we happen to know what possible values the input will take
- `required init(coder:)` methods on `NSView` subclasses that are only to be initialized via `init(frame:)`
- force-unwrapped optionals in cases where we need to clarify to the reader of the code _why_ we know this value won't be nil, or where we want to make any programmer error that results in this crash easier to debug

What I _haven't_ seen is a convincing use case for `fatalError` in an expected code path that is _not_ the result of a programmer error and that warrants testing. However, giving the benefit of the doubt that this does exist, let me amend my position to be that we need to have _some_ way to assert that a path is unreachable in a way that satisfies the coverage checker. Whether we call that mechanism `fatalError`, `preconditionFailure`, `florglequat`, or something else is simply a matter of bikeshedding.

[Next page](https://forums.swift.org/t/pitch-exempt-code-paths-ending-in-preconditionfailure-from-coverage-checks-when-running-tests/62593.md?page=2)
