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

Use Var wrapper only for Pointer formatting #384

Merged
merged 2 commits into from
Nov 10, 2024
Merged
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
8 changes: 6 additions & 2 deletions impl/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::attr::{Display, Trait};
use crate::scan_expr::scan_expr;
use crate::unraw::{IdentUnraw, MemberUnraw};
use proc_macro2::{Delimiter, TokenStream, TokenTree};
use quote::{format_ident, quote, quote_spanned};
use quote::{format_ident, quote, quote_spanned, ToTokens as _};
use std::collections::{BTreeSet, HashMap};
use std::iter;
use syn::ext::IdentExt;
Expand Down Expand Up @@ -114,6 +114,8 @@ impl Display<'_> {
}
let formatvar_prefix = if bonus_display {
"__display"
} else if bound == Trait::Pointer {
"__pointer"
} else {
"__field"
};
Expand All @@ -137,8 +139,10 @@ impl Display<'_> {
};
let wrapped_binding_value = if bonus_display {
quote_spanned!(span=> #binding_value.as_display())
} else {
} else if bound == Trait::Pointer {
quote!(::thiserror::__private::Var(#binding_value))
} else {
binding_value.into_token_stream()
};
has_bonus_display |= bonus_display;
bindings.push((formatvar.to_local(), wrapped_binding_value));
Expand Down
54 changes: 1 addition & 53 deletions src/var.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,9 @@
use core::fmt::{
self, Binary, Debug, Display, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex,
};
use core::fmt::{self, Pointer};

pub struct Var<'a, T: ?Sized>(pub &'a T);

/// Pointer is the only one for which there is a difference in behavior between
/// `Var<'a, T>` vs `&'a T`.
impl<'a, T: Pointer + ?Sized> Pointer for Var<'a, T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Pointer::fmt(self.0, formatter)
}
}

impl<'a, T: Binary + ?Sized> Binary for Var<'a, T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Binary::fmt(self.0, formatter)
}
}

impl<'a, T: Debug + ?Sized> Debug for Var<'a, T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(self.0, formatter)
}
}

impl<'a, T: Display + ?Sized> Display for Var<'a, T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(self.0, formatter)
}
}

impl<'a, T: LowerExp + ?Sized> LowerExp for Var<'a, T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
LowerExp::fmt(self.0, formatter)
}
}

impl<'a, T: LowerHex + ?Sized> LowerHex for Var<'a, T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
LowerHex::fmt(self.0, formatter)
}
}

impl<'a, T: Octal + ?Sized> Octal for Var<'a, T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Octal::fmt(self.0, formatter)
}
}

impl<'a, T: UpperExp + ?Sized> UpperExp for Var<'a, T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
UpperExp::fmt(self.0, formatter)
}
}

impl<'a, T: UpperHex + ?Sized> UpperHex for Var<'a, T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
UpperHex::fmt(self.0, formatter)
}
}
6 changes: 6 additions & 0 deletions tests/ui/no-display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ pub struct Error {
thread: NoDisplay,
}

#[derive(Error, Debug)]
#[error("thread: {thread:o}")]
pub struct ErrorOctal {
thread: NoDisplay,
}

fn main() {}
26 changes: 26 additions & 0 deletions tests/ui/no-display.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,29 @@ note: the trait `std::fmt::Display` must be implemented
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `as_display`, perhaps you need to implement it:
candidate #1: `AsDisplay`

error[E0277]: the trait bound `NoDisplay: Octal` is not satisfied
--> tests/ui/no-display.rs:13:9
|
12 | #[derive(Error, Debug)]
| ----- in this derive macro expansion
13 | #[error("thread: {thread:o}")]
| ^^^^^^^^^^^^^^^^^^^^ the trait `Octal` is not implemented for `NoDisplay`
|
= help: the following other types implement trait `Octal`:
&T
&mut T
NonZero<T>
Saturating<T>
Wrapping<T>
i128
i16
i32
and $N others
= note: required for `&NoDisplay` to implement `Octal`
note: required by a bound in `core::fmt::rt::Argument::<'_>::new_octal`
--> $RUST/core/src/fmt/rt.rs
|
| pub fn new_octal<T: Octal>(x: &T) -> Argument<'_> {
| ^^^^^ required by this bound in `Argument::<'_>::new_octal`
= note: this error originates in the macro `$crate::format_args` which comes from the expansion of the derive macro `Error` (in Nightly builds, run with -Z macro-backtrace for more info)
Loading