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

Document panic and format edition/verions, and improve xrefs #96518

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions library/alloc/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
//!
//! If a named parameter does not appear in the argument list, `format!` will
//! reference a variable with that name in the current scope.
//! (Rust 1.58 and later; in [`panic!`], requires Rust 2021.)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this line. We don't include version numbers in the docs for every difference -- we host versioned copies of the docs (like https://doc.rust-lang.org/1.57.0/std/macro.format.html) so people using a particular version can see what that version supports. And we don't need to document panic!'s edition behaviour here -- that's fine just in its own documentation.

Suggested change
//! (Rust 1.58 and later; in [`panic!`], requires Rust 2021.)

//!
//! ```
//! let argument = 2 + 2;
Expand Down
7 changes: 4 additions & 3 deletions library/alloc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ macro_rules! vec {
///
/// The first argument `format!` receives is a format string. This must be a string
/// literal. The power of the formatting string is in the `{}`s contained.
///
/// Additional parameters passed to `format!` replace the `{}`s within the
/// formatting string in the order given unless named or positional parameters
/// are used; see [`std::fmt`] for more information.
/// are used.
///
/// See [the formatting syntax documentation in `std::fmt`](../std/fmt/index.html)
/// for details.
///
/// A common use for `format!` is concatenation and interpolation of strings.
/// The same convention is used with [`print!`] and [`write!`] macros,
Expand All @@ -85,7 +87,6 @@ macro_rules! vec {
/// To convert a single value to a string, use the [`to_string`] method. This
/// will use the [`Display`] formatting trait.
///
/// [`std::fmt`]: ../std/fmt/index.html
/// [`print!`]: ../std/macro.print.html
/// [`write!`]: core::write
/// [`to_string`]: crate::string::ToString
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,8 @@ pub(crate) mod builtin {
/// assert_eq!(display, debug);
/// ```
///
/// For more information, see the documentation in [`std::fmt`].
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
/// for details of the macro argument syntax, and further information.
///
/// [`Display`]: crate::fmt::Display
/// [`Debug`]: crate::fmt::Debug
Expand Down
23 changes: 21 additions & 2 deletions library/core/src/macros/panic.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tests. `panic!` is closely tied with the `unwrap` method of both
`panic!` when they are set to [`None`] or [`Err`] variants.

When using `panic!()` you can specify a string payload, that is built using
the [`format!`] syntax. That payload is used when injecting the panic into
the [`format!` syntax]. That payload is used when injecting the panic into
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: maybe just say

Suggested change
the [`format!` syntax]. That payload is used when injecting the panic into
the [formatting syntax]. That payload is used when injecting the panic into

rather that mentioning a particular macro? That avoids a conversation about whether it's "the format! syntax" or "the format_args! syntax" or ...

the calling Rust thread, causing the thread to panic entirely.

The behavior of the default `std` hook, i.e. the code that runs directly
Expand Down Expand Up @@ -55,7 +55,7 @@ For more detailed information about error handling check out the [book] or the
[`panic_any`]: ../std/panic/fn.panic_any.html
[`Box`]: ../std/boxed/struct.Box.html
[`Any`]: crate::any::Any
[`format!`]: ../std/macro.format.html
[`format!` syntax]: ../std/fmt/index.html
[book]: ../book/ch09-00-error-handling.html
[`std::result`]: ../std/result/index.html

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

# Editions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, having this written out here is really handy 👍

One request: Can you put the information about the newest edition first in this section? We want to optimize the docs for people using the latest edition, and have people still using older ones need to scan down to find their behaviour, rather than having people using 2021 need to hunt.

Maybe it could work well to make subsections here? Sketch,

# Editions

## 2021 and later

... `panic!` always uses formatting; use `panic_any` if you want a value ...

## 2018 and 2015

... panics with a payload of literally ...


In Rust Editions prior to 2021, `std::panic!(x)` with a single
argument is equivalent to
[`std::panic::panic_any(x)`](../std/panic/fn.panic_any.html).
This is true even if the argument is a string literal.

For example, in Rust 2015 `panic!("problem: {reason}")` panics with a
payload of literally `"problem: {reason}"` (a `&'static str`), which
is probably not what was intended. In current Rust this usage
captures and formats a variable `reason` from the surrounding scope.

In Rust editions prior to 2021, `core::panic!(x)` requires that
`x` be `&str`, but does not require it to be a literal. In Rust 2021,
these cases must be written `panic!("{}", x)`.

In Rust 2021 and later, `panic!` always requires a format string and
the applicable format arguments, and is the same in `core` and `std`.

# Examples

```should_panic
Expand Down
12 changes: 12 additions & 0 deletions library/std/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ macro_rules! panic {
/// Use `print!` only for the primary output of your program. Use
/// [`eprint!`] instead to print error and progress messages.
///
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
/// for details of the macro argument syntax.
///
/// [flush]: crate::io::Write::flush
///
/// # Panics
Expand Down Expand Up @@ -76,6 +79,9 @@ macro_rules! print {
/// Use `println!` only for the primary output of your program. Use
/// [`eprintln!`] instead to print error and progress messages.
///
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
/// for details of the macro argument syntax.
///
/// [`std::fmt`]: crate::fmt
///
/// # Panics
Expand Down Expand Up @@ -116,6 +122,9 @@ macro_rules! println {
/// [`io::stderr`]: crate::io::stderr
/// [`io::stdout`]: crate::io::stdout
///
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
/// for details of the macro argument syntax.
///
/// # Panics
///
/// Panics if writing to `io::stderr` fails.
Expand Down Expand Up @@ -144,6 +153,9 @@ macro_rules! eprint {
/// Use `eprintln!` only for error and progress messages. Use `println!`
/// instead for the primary output of your program.
///
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
/// for details of the macro argument syntax.
///
/// [`io::stderr`]: crate::io::stderr
/// [`io::stdout`]: crate::io::stdout
///
Expand Down