Skip to content

Commit 2eff0de

Browse files
authored
Rollup merge of #115310 - RalfJung:panic-and-format, r=scottmcm
Document panic behavior across editions, and improve xrefs This revives (parts of) #96518. r? `@scottmcm` Cc `@ijackson`
2 parents b4c63f0 + 5016695 commit 2eff0de

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

library/alloc/src/macros.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ macro_rules! vec {
7979
///
8080
/// The first argument `format!` receives is a format string. This must be a string
8181
/// literal. The power of the formatting string is in the `{}`s contained.
82-
///
8382
/// Additional parameters passed to `format!` replace the `{}`s within the
8483
/// formatting string in the order given unless named or positional parameters
85-
/// are used; see [`std::fmt`] for more information.
84+
/// are used.
85+
///
86+
/// See [the formatting syntax documentation in `std::fmt`](../std/fmt/index.html)
87+
/// for details.
8688
///
8789
/// A common use for `format!` is concatenation and interpolation of strings.
8890
/// The same convention is used with [`print!`] and [`write!`] macros,
@@ -91,7 +93,6 @@ macro_rules! vec {
9193
/// To convert a single value to a string, use the [`to_string`] method. This
9294
/// will use the [`Display`] formatting trait.
9395
///
94-
/// [`std::fmt`]: ../std/fmt/index.html
9596
/// [`print!`]: ../std/macro.print.html
9697
/// [`write!`]: core::write
9798
/// [`to_string`]: crate::string::ToString

library/core/src/macros/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,8 @@ pub(crate) mod builtin {
849849
/// assert_eq!(display, debug);
850850
/// ```
851851
///
852-
/// For more information, see the documentation in [`std::fmt`].
852+
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
853+
/// for details of the macro argument syntax, and further information.
853854
///
854855
/// [`Display`]: crate::fmt::Display
855856
/// [`Debug`]: crate::fmt::Debug

library/core/src/macros/panic.md

+27-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ tests. `panic!` is closely tied with the `unwrap` method of both
88
[`Option`][ounwrap] and [`Result`][runwrap] enums. Both implementations call
99
`panic!` when they are set to [`None`] or [`Err`] variants.
1010

11-
When using `panic!()` you can specify a string payload, that is built using
12-
the [`format!`] syntax. That payload is used when injecting the panic into
11+
When using `panic!()` you can specify a string payload that is built using
12+
[formatting syntax]. That payload is used when injecting the panic into
1313
the calling Rust thread, causing the thread to panic entirely.
1414

1515
The behavior of the default `std` hook, i.e. the code that runs directly
@@ -18,6 +18,7 @@ after the panic is invoked, is to print the message payload to
1818
call. You can override the panic hook using [`std::panic::set_hook()`].
1919
Inside the hook a panic can be accessed as a `&dyn Any + Send`,
2020
which contains either a `&str` or `String` for regular `panic!()` invocations.
21+
(Whether a particular invocation contains the payload at type `&str` or `String` is unspecified and can change.)
2122
To panic with a value of another other type, [`panic_any`] can be used.
2223

2324
See also the macro [`compile_error!`], for raising errors during compilation.
@@ -55,7 +56,7 @@ For more detailed information about error handling check out the [book] or the
5556
[`panic_any`]: ../std/panic/fn.panic_any.html
5657
[`Box`]: ../std/boxed/struct.Box.html
5758
[`Any`]: crate::any::Any
58-
[`format!`]: ../std/macro.format.html
59+
[`format!` syntax]: ../std/fmt/index.html
5960
[book]: ../book/ch09-00-error-handling.html
6061
[`std::result`]: ../std/result/index.html
6162

@@ -64,6 +65,29 @@ For more detailed information about error handling check out the [book] or the
6465
If the main thread panics it will terminate all your threads and end your
6566
program with code `101`.
6667

68+
# Editions
69+
70+
Behavior of the panic macros changed over editions.
71+
72+
## 2021 and later
73+
74+
In Rust 2021 and later, `panic!` always requires a format string and
75+
the applicable format arguments, and is the same in `core` and `std`.
76+
Use [`std::panic::panic_any(x)`](../std/panic/fn.panic_any.html) to
77+
panic with an arbitrary payload.
78+
79+
## 2018 and 2015
80+
81+
In Rust Editions prior to 2021, `std::panic!(x)` with a single
82+
argument directly uses that argument as a payload.
83+
This is true even if the argument is a string literal.
84+
For example, `panic!("problem: {reason}")` panics with a
85+
payload of literally `"problem: {reason}"` (a `&'static str`).
86+
87+
`core::panic!(x)` with a single argument requires that `x` be `&str`,
88+
but otherwise behaves like `std::panic!`. In particular, the string
89+
need not be a literal, and is not interpreted as a format string.
90+
6791
# Examples
6892

6993
```should_panic

library/std/src/macros.rs

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ macro_rules! panic {
4141
/// Use `print!` only for the primary output of your program. Use
4242
/// [`eprint!`] instead to print error and progress messages.
4343
///
44+
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
45+
/// for details of the macro argument syntax.
46+
///
4447
/// [flush]: crate::io::Write::flush
4548
/// [`println!`]: crate::println
4649
/// [`eprint!`]: crate::eprint
@@ -103,6 +106,9 @@ macro_rules! print {
103106
/// Use `println!` only for the primary output of your program. Use
104107
/// [`eprintln!`] instead to print error and progress messages.
105108
///
109+
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
110+
/// for details of the macro argument syntax.
111+
///
106112
/// [`std::fmt`]: crate::fmt
107113
/// [`eprintln!`]: crate::eprintln
108114
/// [lock]: crate::io::Stdout
@@ -150,6 +156,9 @@ macro_rules! println {
150156
/// [`io::stderr`]: crate::io::stderr
151157
/// [`io::stdout`]: crate::io::stdout
152158
///
159+
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
160+
/// for details of the macro argument syntax.
161+
///
153162
/// # Panics
154163
///
155164
/// Panics if writing to `io::stderr` fails.
@@ -181,6 +190,9 @@ macro_rules! eprint {
181190
/// Use `eprintln!` only for error and progress messages. Use `println!`
182191
/// instead for the primary output of your program.
183192
///
193+
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
194+
/// for details of the macro argument syntax.
195+
///
184196
/// [`io::stderr`]: crate::io::stderr
185197
/// [`io::stdout`]: crate::io::stdout
186198
/// [`println!`]: crate::println

0 commit comments

Comments
 (0)