Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add migration section for panic-macro-consistency #258

Merged
merged 2 commits into from
Jul 20, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/rust-2021/panic-macro-consistency.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ println!(a); // Error: First argument must be a format string literal
panic!(a); // Ok: The panic macro doesn't care
```

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

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

In addition, `core::panic!()` and `std::panic!()` will be identical in Rust 2021.
Currently, there are some historical differences between those two,
which can be noticable when switching `#![no_std]` on or off.
which can be noticeable when switching `#![no_std]` on or off.

## Migration

A lint, `non_fmt_panics`, gets triggered whenever there is some call to `panic` that uses some
deprecated behavior that will error in Rust 2021. The `non_fmt_panics` lint has already been a warning
by default on all editions since the 1.50 release (with several enhancements made in later releases).
If your code is already warning free, then it should already be ready to go for Rust 2021!

You can automatically migrate your code to be Rust 2021 Edition compatible or ensure it is already compatible by
running:

```sh
cargo fix --edition
```

Should you choose to or need to manually migrate, you'll need to update all panic invocations to either use the same
formatting as `println` currently does or use
rylev marked this conversation as resolved.
Show resolved Hide resolved

For example, in the case of `panic!(MyStruct)`, you'll need to either convert to using `std::panic::panic_any` (note
rylev marked this conversation as resolved.
Show resolved Hide resolved
that this is a function not a macro).

In the case of panic messages that include curly braces but no arguments (e.g., `panic!("Some curlies: {}")), you'll
rylev marked this conversation as resolved.
Show resolved Hide resolved
need to print the literal string by either using the same syntax as `println!` (i.e., `panic!("{}", Some curlies: {}")`)
rylev marked this conversation as resolved.
Show resolved Hide resolved
or by escaping them (i.e., `panic!("Some curlies: {{}}")`).