# Enforcing no-std and no-panic during build

**URL:** <https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505>\
**Category:** language design\
**Created:** [April 17, 2021, 12:31am UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505 "2021-04-17T00:31:12Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkaitchuck](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@tkaitchuck](https://internals.rust-lang.org/u/tkaitchuck)\
**Post date:** [April 17, 2021, 12:31am UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/1 "2021-04-17T00:31:12Z")

</div>

In the discussion on the linux mailing list: [https://lkml.org/lkml/2021/4/14/1099](https://lkml.org/lkml/2021/4/14/1099)[https://lkml.org/lkml/2021/4/14/1130](https://lkml.org/lkml/2021/4/14/1130)

It was mentioned that they need to operate with a subset of the language that cannot panic. (And also disabled automatic overflow in arithmetic). This seems fairly easy to achieve with flags and conditional compilation.

From an ecosystem point of view this creates a situation similar to `no-std` today. Crates are tagged and documented as being `no-std` in `crates.io` so that people can tell if they can be used in their context. It seems likely that we will eventually have crates being marked as `no-panic` to indicate they they are `no-std` and also compile without the need for code to support panicking. Obviously such crates can still be used by applications that don't care about such things.

This introduces a problem similar one that exists for `no-std` crates today. It's easy to make a mistake and call a function which is not available and cause downstream crates' builds to fail.

The problem is that during development `std`, `panic`, etc are available because they are needed for tests. Hence even if the code is made to accidently depend on them it will compile.

It would be extremely useful to have a way to fail a build of a crate which for example declares itself `no-std` and ends up linking against `std` and, assuming such an annotation is added, declares itself `no-panic` but ends up linking against code to panic. This way rather than failing a downstream build after the crate has be published, the bug is caught early and the developer sees an error as soon as they type the offending line.

---

<div class="post-metadata">

**Author:** ![lachlansneff](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lachlansneff/32/5924_2.png) [@lachlansneff](https://internals.rust-lang.org/u/lachlansneff)\
**Post date:** [April 17, 2021, 1:05am UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/2 "2021-04-17T01:05:53Z")

</div>

When compiling for no\_std and in release mode, you can enforce that panic is never called with [https://crates.io/crates/no-panics-whatsoever](https://crates.io/crates/no-panics-whatsoever). It's a bit hacky, but it will error at compile time if any panics are present in the binary.

---

<div class="post-metadata">

**Author:** ![bascule](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bascule/32/3057_2.png) [@bascule](https://internals.rust-lang.org/u/bascule)\
**Post date:** [April 17, 2021, 2:14am UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/3 "2021-04-17T02:14:45Z")

</div>

I'd be very interested in this as well.

I'm familiar with crates like dtolnay's [no-panic](https://github.com/dtolnay/no-panic) and japaric's [panic-never](https://github.com/japaric/panic-never), the latter of which is being suggested as a [convention or requirement for Rust embedded code](https://github.com/rust-embedded/wg/issues/551), but...

It really seems like the sort of thing that belongs in `rustc` proper, as opposed to some sort of external hack. It would be nice to say, for some degree of confidence, that a whole program composed of several crates is panic-free, instead of crates having to opt into it on a crate-by-crate basis.

I also imagine that if the compiler knows a program will never panic, it could automatically make use of optimizations which aren't possible in panicking code.

The only way I know how to do that today is using `const fn`, which presently entails writing panic free code because `const_panic` isn't stable, but that almost seems like an accidental way of ensuring code is panic-free, and it looks like [`const_panic` will be stable soon](https://github.com/rust-lang/rust/issues/51999). That's actually great and I actually want to use `const_panic` in other contexts, and perhaps it's okay for otherwise "panic free" code to have `const_panic` code because it will only panic at compile time.

---

<div class="post-metadata">

**Author:** ![djc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/djc/32/1592_2.png) [@djc](https://internals.rust-lang.org/u/djc)\
**Post date:** [April 17, 2021, 7:11am UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/4 "2021-04-17T07:11:21Z")

</div>

FFI is another situation where panicking gets complicated. I’m currently working on reducing panicking in rustls (motivated in part by the in progress C API that we are building) and it would be great to have “official” support for this in the language/tooling.

I suppose one of the ergonomic challenges here is that we might want to provide both fine-grained (this function doesn’t panic) and more coarse-grained annotations (functions defined in this module are not allowed to panic).

I think this could then also map to LLVM’s `nounwind` function attribute?

---

<div class="post-metadata">

**Author:** ![tspiteri](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tspiteri/32/7283_2.png) [@tspiteri](https://internals.rust-lang.org/u/tspiteri)\
**Post date:** [April 17, 2021, 10:09am UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/5 "2021-04-17T10:09:39Z")

</div>

You can still panic in a const function in stable by dividing an integer by zero.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [April 17, 2021, 3:00pm UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/6 "2021-04-17T15:00:27Z")

</div>

That would be lovely. Combined with `oom=panic`, it could also be used to enforce that libraries use fallible allocations.

I think it'd be very useful as a function annotation that checks, recursively, whether anything invoked in this function could panic.

```rust
#[no_panics_please]
fn foo() {
    println!("hi"); // compile error: println can panic!
}

```

---

<div class="post-metadata">

**Author:** ![djc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/djc/32/1592_2.png) [@djc](https://internals.rust-lang.org/u/djc)\
**Post date:** [April 17, 2021, 9:21pm UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/7 "2021-04-17T21:21:11Z")

</div>

I suppose a further question is: is not panicking part of the semver contract? That might inform whether only functions that are explicitly tagged `#[no_panic]` can be called by other functions that are `#[no_panic]`, or whether the compiler is allowed to infer that some functions cannot panic.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [April 17, 2021, 9:36pm UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/8 "2021-04-17T21:36:50Z")

</div>

If rust code isn't allowed to panic, would C code not be allowed to bug or oops? Rust are not meant for errors that can be handled in normal way. `Result` is meant for this. Panics are meant for logic bugs and are meant to terminate at leaat the current thread (aka an oops in the linux kernel). `std::process::abort` is meant for when it is impossible to safely recover from the error/bug at all (aka a kernel panic). What would make sense is to have a method to warn/error on operations that can implicitly panic like array indexing. Ensuring that a program doesn't panic is IMHO equivalent to either proving the absence of bugs in the program or just silently returning bogus results in case of bugs. If there is a sensible way to handle an error that doesn't involve discarding what you are currently doing, be it handling a syscall, an http request or something else, you should **not** panic.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [April 23, 2021, 3:19pm UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/9 "2021-04-23T15:19:55Z")

</div>

> [@djc](#):
>
> whether the compiler is allowed to infer that some functions cannot panic.

That's a good question. I guess it'd have to be explicit `#[no_panic]` on everything, because otherwise it'd be too easy to break someone at distance.

It would be a bit spammy, because you might want that on all applicable functions, but I suppose this problem can be solved later, e.g. with another feature that sets attributes en masse for all functions in a scope. `#[must_use]` is similarly a noisy default (compared to Swift's "must use" enabled by default and an attribute to mark a result as discardable), so I'd like ability to say e.g. "everything in this `impl` block is `must_use`, `inline`, and `no_panic`".

> [@bjorn3](#):
>
> If rust code isn't allowed to panic, would C code not be allowed to bug or oops?

C code is already not allowed to unwind through Rust's FFI boundary, so in a literal sense C can't panic either.

In a wider sense, I see two differences:

- in C it's just more customary to handle failures in not-world-ending ways. Because you can't rely on a panic keeping things safe, you just have no choice but to handle that problem before it happens, much more carefully and proactively than you'd have to in Rust. And this is typically done by returning an error. When `abort()` is used it's IMHO more intentional when it's the right thing to do for the whole program, not just an oversight caught by some leaf function that doesn't know what to do.

- even in like-for-like situations, it's just an opportunity for Rust to do better than C can. In C you can't enforce that every array index and null pointer is checked (neither static analyzers nor runtime sanitizers give a guarantee of catching every case), but with a no-panic Rule you could forbid every lazy `.unwrap()` and enforce that Rust uses `.get(x)?` everywhere instead of `[x]`.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [April 23, 2021, 6:14pm UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/10 "2021-04-23T18:14:12Z")

</div>

I wonder what core/std would look like if you tree-shook all (most?) potentially panicking code paths out. It'd definitely be overly conservative (as much of the library fairly uses `[ix]` even if the index is known inbounds, and lets the optimizer deal with the panicking branch), but I think it'd still be a useful experiment to see what is already done in a 100%-won't-panic manner. Whatever the outcome, even if it's barely any functionality, would be a interesting and potentially useful data point.

(That said, I think the use case of literally implementing the kernel is about the best possible argument for `#![no_core]`, because the constraints in kernel code are so different from even safety-critical microcontrollers.)

The general way you'd go about tree-shaking out potentially panicking code paths is theoretically simple: remove `panic!`, then recursively remove everything that uses the removed functionality.

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [April 23, 2021, 8:02pm UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/11 "2021-04-23T20:02:17Z")

</div>

> [@bjorn3](#):
>
> Panics are meant for logic bugs and are meant to terminate at leaat the current thread (aka an oops in the linux kernel). `std::process::abort` is meant for when it is impossible to safely recover from the error/bug at all (aka a kernel panic).

Rust panics seem [wholly unwelcome](https://lkml.org/lkml/2021/4/14/1099) in the Linux kernel. Categorically it's considered (at least by Linus himself) a "fundamental issue" apparently.

And honestly I can sort of understand it. The last thing you want when developing kernel code is to bump up against language-specific (anti)features which are standing in your way. I'm not saying that Rust panics are an anti-feature btw, more that in certain contexts it can be considered as such, as indicated by Linus' response.

---

<div class="post-metadata">

**Author:** ![djc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/djc/32/1592_2.png) [@djc](https://internals.rust-lang.org/u/djc)\
**Post date:** [April 23, 2021, 8:12pm UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/12 "2021-04-23T20:12:51Z")

</div>

> [@kornel](#):
>
> That's a good question. I guess it'd have to be explicit `#[no_panic]` on everything, because otherwise it'd be too easy to break someone at distance.

An interesting compromise might be that the compiler will infer it for crate-local functions, but will only rely on the annotation across crate boundaries.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [April 23, 2021, 8:55pm UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/13 "2021-04-23T20:55:42Z")

</div>

> [@jjpe](#):
>
> Rust panics seem [wholly unwelcome](https://lkml.org/lkml/2021/4/14/1099) in the Linux kernel. Categorically it's considered (at least by Linus himself) a "fundamental issue" apparently.

That was something where panics arguably shouldn't have been used at all as `BUG_ON` shouldn't have been used either. It was not a case of something has gone horribly wrong and there is no sensible way to handle to other than to restart. OOM handling in the kernel shouldn't use panic and the compiler builtin calls should have not been emitted at all. What I am saying is that panics should be used for cases where you can't recover aka the exact same cases where currenty `BUG_ON` is currently being used inside the kernel. I do agree that it may be a good idea to make it harder to **accidentally** introduce a potential panic through for example `[]`, but explicit panics should not be completely forbidden when `BUG_ON` isn't forbidden for the respective error case either. `#[no_panic]` would also prevent crashing in the face of genuine corruption that has to abort the system to prevent an attacker from causing harm.

---

<div class="post-metadata">

**Author:** ![samsieber](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/samsieber/32/4671_2.png) [@samsieber](https://internals.rust-lang.org/u/samsieber)\
**Post date:** [April 23, 2021, 9:40pm UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/14 "2021-04-23T21:40:37Z")

</div>

> [@bjorn3](#):
>
> `#[no_panic]` would also prevent crashing in the face of genuine corruption that has to abort the system to prevent an attacker from causing harm

In theory you could encode panic worthy bugs in a Result's Err type (as an enum perhaps). Nothing prevents you from plumbing it up all the way to the top, right? I won't say it's ergonomic, but it seems possible... and it might be less work than trying to prevent most but not all panics.

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [April 26, 2021, 11:55pm UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/15 "2021-04-26T23:55:59Z")

</div>

> [@bjorn3](#):
>
> `#[no_panic]` would also prevent crashing in the face of genuine corruption that has to abort the system to prevent an attacker from causing harm.

It would have to be done manually, e.g.

```rust
if data.is_corrupted() {
    abort();
}

```

Option and Result could also be extended with a `.unwrap_abort()` method that can be used even in a `#[no_panic]` function.

---

<div class="post-metadata">

**Author:** ![nacaclanga](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nacaclanga/32/7461_2.png) [@nacaclanga](https://internals.rust-lang.org/u/nacaclanga)\
**Post date:** [April 27, 2021, 6:39am UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/16 "2021-04-27T06:39:55Z")

</div>

I think one could just use `.unwrap_or_else(|| abort())`, given that the `abort` function is likely to be customised in this case.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [July 26, 2021, 6:40am UTC](https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505/17 "2021-07-26T06:40:42Z")

</div>

Closed on July 26, 2021, 6:40am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
