From 1d040f358a34d58139f1e1c12cec575319f16edf Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 10 Nov 2024 13:11:08 -0800 Subject: [PATCH] Use Var wrapper only for Pointer formatting --- impl/src/fmt.rs | 8 ++++-- src/var.rs | 54 +------------------------------------- tests/ui/no-display.stderr | 4 +-- 3 files changed, 9 insertions(+), 57 deletions(-) diff --git a/impl/src/fmt.rs b/impl/src/fmt.rs index 82fc68e..3860b4e 100644 --- a/impl/src/fmt.rs +++ b/impl/src/fmt.rs @@ -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; @@ -114,6 +114,8 @@ impl Display<'_> { } let formatvar_prefix = if bonus_display { "__display" + } else if bound == Trait::Pointer { + "__pointer" } else { "__field" }; @@ -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)); diff --git a/src/var.rs b/src/var.rs index 8643590..ecfcd85 100644 --- a/src/var.rs +++ b/src/var.rs @@ -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) - } -} diff --git a/tests/ui/no-display.stderr b/tests/ui/no-display.stderr index a60c0d3..8f35b82 100644 --- a/tests/ui/no-display.stderr +++ b/tests/ui/no-display.stderr @@ -32,12 +32,12 @@ error[E0277]: the trait bound `NoDisplay: Octal` is not satisfied &mut T NonZero Saturating - Var<'a, T> Wrapping i128 i16 + i32 and $N others - = note: required for `Var<'_, NoDisplay>` to implement `Octal` + = 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 |