Skip to content

Implement #[error(fmt = ...)]#367

Merged
dtolnay merged 2 commits intomasterfrom
fmt
Nov 5, 2024
Merged

Implement #[error(fmt = ...)]#367
dtolnay merged 2 commits intomasterfrom
fmt

Conversation

@dtolnay
Copy link
Owner

@dtolnay dtolnay commented Nov 5, 2024

Before:

#[derive(Error, Debug)]
pub enum Error {
    #[error("{code}{}", match .message {
        Some(msg) => format!(" - {}", &msg),
        None => "".to_owned(),
    })]
    Demo { code: u16, message: Option<String> },
}

After:

#[derive(Error, Debug)]
pub enum Error {
    #[error(fmt = demo_fmt)]
    Demo { code: u16, message: Option<String> },
}

fn demo_fmt(code: &u16, message: &Option<String>, formatter: &mut fmt::Formatter) -> fmt::Result {
    write!(formatter, "{code}")?;
    if let Some(msg) = message {
        write!(formatter, " - {msg}")?;
    }
    Ok(())
}

This avoids forcing allocations (or the even worse workarounds in #201) and keeps elaborate formatting logic out of the data structure.

The parameter order is designed to be usable with generic methods.

#[derive(Error, Debug)]
#[error(fmt = core::fmt::Octal::fmt)]
pub enum Error {
    I32(i32),
    I64(i64),
}

@dtolnay dtolnay linked an issue Nov 5, 2024 that may be closed by this pull request
    warning: this argument (4 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
       --> tests/test_display.rs:388:16
        |
    388 |     fn pair(k: &i32, v: &i32, formatter: &mut fmt::Formatter) -> fmt::Result {
        |                ^^^^ help: consider passing by value instead: `i32`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref
        = note: `-W clippy::trivially-copy-pass-by-ref` implied by `-W clippy::pedantic`
        = help: to override `-W clippy::pedantic` add `#[allow(clippy::trivially_copy_pass_by_ref)]`

    warning: this argument (4 byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
       --> tests/test_display.rs:388:25
        |
    388 |     fn pair(k: &i32, v: &i32, formatter: &mut fmt::Formatter) -> fmt::Result {
        |                         ^^^^ help: consider passing by value instead: `i32`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref
@dtolnay dtolnay merged commit 184c100 into master Nov 5, 2024
@dtolnay dtolnay deleted the fmt branch November 5, 2024 22:37
@kaivol
Copy link

kaivol commented Sep 4, 2025

I just came across this, looks like a very useful feature!

However, this doesn't seem to be documented anywhere except in the patch notes.
I would have expected a mention here.

jimsynz pushed a commit to jimsynz/outrun that referenced this pull request Oct 13, 2025
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [thiserror](https://github.com/dtolnay/thiserror) | dependencies | major | `1.0` -> `2.0` |

---

### Release Notes

<details>
<summary>dtolnay/thiserror (thiserror)</summary>

### [`v2.0.17`](https://github.com/dtolnay/thiserror/releases/tag/2.0.17)

[Compare Source](dtolnay/thiserror@2.0.16...2.0.17)

- Use differently named \_\_private module per patch release ([#&#8203;434](dtolnay/thiserror#434))

### [`v2.0.16`](https://github.com/dtolnay/thiserror/releases/tag/2.0.16)

[Compare Source](dtolnay/thiserror@2.0.15...2.0.16)

- Add to "no-std" crates.io category ([#&#8203;429](dtolnay/thiserror#429))

### [`v2.0.15`](https://github.com/dtolnay/thiserror/releases/tag/2.0.15)

[Compare Source](dtolnay/thiserror@2.0.14...2.0.15)

- Prevent `Error::provide` API becoming unavailable from a future new compiler lint ([#&#8203;427](dtolnay/thiserror#427))

### [`v2.0.14`](https://github.com/dtolnay/thiserror/releases/tag/2.0.14)

[Compare Source](dtolnay/thiserror@2.0.13...2.0.14)

- Allow build-script cleanup failure with NFSv3 output directory to be non-fatal ([#&#8203;426](dtolnay/thiserror#426))

### [`v2.0.13`](https://github.com/dtolnay/thiserror/releases/tag/2.0.13)

[Compare Source](dtolnay/thiserror@2.0.12...2.0.13)

- Documentation improvements

### [`v2.0.12`](https://github.com/dtolnay/thiserror/releases/tag/2.0.12)

[Compare Source](dtolnay/thiserror@2.0.11...2.0.12)

- Prevent elidable\_lifetime\_names pedantic clippy lint in generated impl ([#&#8203;413](dtolnay/thiserror#413))

### [`v2.0.11`](https://github.com/dtolnay/thiserror/releases/tag/2.0.11)

[Compare Source](dtolnay/thiserror@2.0.10...2.0.11)

- Add feature gate to tests that use std ([#&#8203;409](dtolnay/thiserror#409), [#&#8203;410](dtolnay/thiserror#410), thanks [@&#8203;Maytha8](https://github.com/Maytha8))

### [`v2.0.10`](https://github.com/dtolnay/thiserror/releases/tag/2.0.10)

[Compare Source](dtolnay/thiserror@2.0.9...2.0.10)

- Support errors containing a generic type parameter's associated type in a field ([#&#8203;408](dtolnay/thiserror#408))

### [`v2.0.9`](https://github.com/dtolnay/thiserror/releases/tag/2.0.9)

[Compare Source](dtolnay/thiserror@2.0.8...2.0.9)

- Work around `missing_inline_in_public_items` clippy restriction being triggered in macro-generated code ([#&#8203;404](dtolnay/thiserror#404))

### [`v2.0.8`](https://github.com/dtolnay/thiserror/releases/tag/2.0.8)

[Compare Source](dtolnay/thiserror@2.0.7...2.0.8)

- Improve support for macro-generated `derive(Error)` call sites ([#&#8203;399](dtolnay/thiserror#399))

### [`v2.0.7`](https://github.com/dtolnay/thiserror/releases/tag/2.0.7)

[Compare Source](dtolnay/thiserror@2.0.6...2.0.7)

- Work around conflict with #\[deny(clippy::allow\_attributes)] ([#&#8203;397](dtolnay/thiserror#397), thanks [@&#8203;zertosh](https://github.com/zertosh))

### [`v2.0.6`](https://github.com/dtolnay/thiserror/releases/tag/2.0.6)

[Compare Source](dtolnay/thiserror@2.0.5...2.0.6)

- Suppress deprecation warning on generated From impls ([#&#8203;396](dtolnay/thiserror#396))

### [`v2.0.5`](https://github.com/dtolnay/thiserror/releases/tag/2.0.5)

[Compare Source](dtolnay/thiserror@2.0.4...2.0.5)

- Prevent deprecation warning on generated impl for deprecated type ([#&#8203;394](dtolnay/thiserror#394))

### [`v2.0.4`](https://github.com/dtolnay/thiserror/releases/tag/2.0.4)

[Compare Source](dtolnay/thiserror@2.0.3...2.0.4)

- Eliminate needless\_lifetimes clippy lint in generated `From` impls ([#&#8203;391](dtolnay/thiserror#391), thanks [@&#8203;matt-phylum](https://github.com/matt-phylum))

### [`v2.0.3`](https://github.com/dtolnay/thiserror/releases/tag/2.0.3)

[Compare Source](dtolnay/thiserror@2.0.2...2.0.3)

- Support the same Path field being repeated in both Debug and Display representation in error message ([#&#8203;383](dtolnay/thiserror#383))
- Improve error message when a format trait used in error message is not implemented by some field ([#&#8203;384](dtolnay/thiserror#384))

### [`v2.0.2`](https://github.com/dtolnay/thiserror/releases/tag/2.0.2)

[Compare Source](dtolnay/thiserror@2.0.1...2.0.2)

- Fix hang on invalid input inside #\[error(...)] attribute ([#&#8203;382](dtolnay/thiserror#382))

### [`v2.0.1`](https://github.com/dtolnay/thiserror/releases/tag/2.0.1)

[Compare Source](dtolnay/thiserror@2.0.0...2.0.1)

- Support errors that contain a dynamically sized final field ([#&#8203;375](dtolnay/thiserror#375))
- Improve inference of trait bounds for fields that are interpolated multiple times in an error message ([#&#8203;377](dtolnay/thiserror#377))

### [`v2.0.0`](https://github.com/dtolnay/thiserror/releases/tag/2.0.0)

[Compare Source](dtolnay/thiserror@1.0.69...2.0.0)

##### Breaking changes

- Referencing keyword-named fields by a raw identifier like `{r#type}` inside a format string is no longer accepted; simply use the unraw name like `{type}` ([#&#8203;347](dtolnay/thiserror#347))

  This aligns thiserror with the standard library's formatting macros, which gained support for implicit argument capture later than the release of this feature in thiserror 1.x.

  ```rust
  #[derive(Error, Debug)]
  #[error("... {type} ...")]  // Before: {r#type}
  pub struct Error {
      pub r#type: Type,
  }
  ```

- Trait bounds are no longer inferred on fields whose value is shadowed by an explicit named argument in a format message ([#&#8203;345](dtolnay/thiserror#345))

  ```rust
  // Before: impl<T: Octal> Display for Error<T>
  // After: impl<T> Display for Error<T>
  #[derive(Error, Debug)]
  #[error("{thing:o}", thing = "...")]
  pub struct Error<T> {
      thing: T,
  }
  ```

- Tuple structs and tuple variants can no longer use numerical `{0}` `{1}` access at the same time as supplying extra positional arguments for a format message, as this makes it ambiguous whether the number refers to a tuple field vs a different positional arg ([#&#8203;354](dtolnay/thiserror#354))

  ```rust
  #[derive(Error, Debug)]
  #[error("ambiguous: {0} {}", $N)]
  //                  ^^^ Not allowed, use #[error("... {0} {n}", n = $N)]
  pub struct TupleError(i32);
  ```

- Code containing invocations of thiserror's `derive(Error)` must now have a direct dependency on the `thiserror` crate regardless of the error data structure's contents ([#&#8203;368](dtolnay/thiserror#368), [#&#8203;369](dtolnay/thiserror#369), [#&#8203;370](dtolnay/thiserror#370), [#&#8203;372](dtolnay/thiserror#372))

##### Features

- Support disabling thiserror's standard library dependency by disabling the default "std" Cargo feature: `thiserror = { version = "2", default-features = false }` ([#&#8203;373](dtolnay/thiserror#373))

- Support using `r#source` as field name to opt out of a field named "source" being treated as an error's `Error::source()` ([#&#8203;350](dtolnay/thiserror#350))

  ```rust
  #[derive(Error, Debug)]
  #[error("{source} ==> {destination}")]
  pub struct Error {
      r#source: char,
      destination: char,
  }

  let error = Error { source: 'S', destination: 'D' };
  ```

- Infinite recursion in a generated Display impl now produces an `unconditional_recursion` warning ([#&#8203;359](dtolnay/thiserror#359))

  ```rust
  #[derive(Error, Debug)]
  #[error("??? {self}")]
  pub struct Error;
  ```

- A new attribute `#[error(fmt = path::to::myfmt)]` can be used to write formatting logic for an enum variant out-of-line ([#&#8203;367](dtolnay/thiserror#367))

  ```rust
  #[derive(Error, Debug)]
  pub enum Error {
      #[error(fmt = demo_fmt)]
      Demo { code: u16, message: Option<String> },
  }

  fn demo_fmt(code: &u16, message: &Option<String>, formatter: &mut fmt::Formatter) -> fmt::Result {
      write!(formatter, "{code}")?;
      if let Some(msg) = message {
          write!(formatter, " - {msg}")?;
      }
      Ok(())
  }
  ```

- Enums with an enum-level format message are now able to have individual variants that are `transparent` to supersede the enum-level message ([#&#8203;366](dtolnay/thiserror#366))

  ```rust
  #[derive(Error, Debug)]
  #[error("my error {0}")]
  pub enum Error {
      Json(#[from] serde_json::Error),
      Yaml(#[from] serde_yaml::Error),
      #[error(transparent)]
      Other(#[from] anyhow::Error),
  }
  ```

</details>

---

### Configuration

📅 **Schedule**: Branch creation - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) in timezone Pacific/Auckland, Automerge - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) in timezone Pacific/Auckland.

🚦 **Automerge**: Disabled because a matching PR was automerged previously.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xNy4yIiwidXBkYXRlZEluVmVyIjoiNDEuMTMxLjkiLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbInJlbm92YXRlIl19-->

Reviewed-on: https://harton.dev/outrun/outrun/pulls/81
Co-authored-by: Renovate Bot <[email protected]>
Co-committed-by: Renovate Bot <[email protected]>
takumi-earth pushed a commit to earthlings-dev/thiserror that referenced this pull request Jan 27, 2026
Implement #[error(fmt = ...)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider exposing formatter in display attribute

2 participants