Skip to content

Commit 397d1a6

Browse files
committed
Add migration section for panic-macro-consistency
1 parent 0a0ad79 commit 397d1a6

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/rust-2021/panic-macro-consistency.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ println!(a); // Error: First argument must be a format string literal
2929
panic!(a); // Ok: The panic macro doesn't care
3030
```
3131

32-
(It even accepts non-strings such as `panic!(123)`, which is uncommon and rarely useful.)
32+
It even accepts non-strings such as `panic!(123)`, which is uncommon and rarely useful since it
33+
produces a surprisingly unhelpful message: `panicked at 'Box<Any>'`.
3334

3435
This will especially be a problem once
3536
[implicit format arguments](https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html)
@@ -54,4 +55,28 @@ panic!(a); // Error, must be a string literal
5455

5556
In addition, `core::panic!()` and `std::panic!()` will be identical in Rust 2021.
5657
Currently, there are some historical differences between those two,
57-
which can be noticable when switching `#![no_std]` on or off.
58+
which can be noticeable when switching `#![no_std]` on or off.
59+
60+
## Migration
61+
62+
A lint, `non_fmt_panics`, gets triggered whenever there is some call to `panic` that uses some
63+
deprecated behavior that will error in Rust 2021. The `non_fmt_panics` lint has already been a warning
64+
by default on all editions since the 1.50 release (with several enhancements made in later releases).
65+
If your code is already warning free, then it should already be ready to go for Rust 2021!
66+
67+
You can automatically migrate your code to be Rust 2021 Edition compatible or ensure it is already compatible by
68+
running:
69+
70+
```sh
71+
cargo fix --edition
72+
```
73+
74+
Should you choose to or need to manually migrate, you'll need to update all panic invocations to either use the same
75+
formatting as `println` currently does or use
76+
77+
For example, in the case of `panic!(MyStruct)`, you'll need to either convert to using `std::panic::panic_any` (note
78+
that this is a function not a macro).
79+
80+
In the case of panic messages that include curly braces but no arguments (e.g., `panic!("Some curlies: {}")), you'll
81+
need to print the literal string by either using the same syntax as `println!` (i.e., `panic!("{}", Some curlies: {}")`)
82+
or by escaping them (i.e., `panic!("Some curlies: {{}}")`).

0 commit comments

Comments
 (0)