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

Fix legacy diagnostic for Display-like macros #288

Merged
merged 3 commits into from
Aug 9, 2023
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
14 changes: 9 additions & 5 deletions impl/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use syn::{
token, Ident,
};

use crate::parsing::Expr;
use crate::{parsing::Expr, utils::Either};

/// Representation of a macro attribute expressing additional trait bounds.
#[derive(Debug, Default)]
Expand Down Expand Up @@ -141,16 +141,20 @@ impl FmtAttribute {
match path {
Ok(path) if path.is_ident("fmt") => (|| {
let args = fork
.parse_terminated(syn::Lit::parse, token::Comma)
.parse_terminated(
<Either<syn::Lit, syn::Ident>>::parse,
token::Comma,
)
.ok()?
.into_iter()
.enumerate()
.filter_map(|(i, lit)| match lit {
syn::Lit::Str(str) => Some(if i == 0 {
.filter_map(|(i, arg)| match arg {
Either::Left(syn::Lit::Str(str)) => Some(if i == 0 {
format!("\"{}\"", str.value())
} else {
str.value()
}),
Either::Right(ident) => Some(ident.to_string()),
_ => None,
})
.collect::<Vec<_>>();
Expand All @@ -171,7 +175,7 @@ impl FmtAttribute {
}

impl Parse for FmtAttribute {
fn parse(input: ParseStream) -> syn::Result<Self> {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
Ok(Self {
lit: input.parse()?,
comma: input
Expand Down
31 changes: 29 additions & 2 deletions impl/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ use syn::{
TypeParamBound, Variant, WhereClause,
};

#[cfg(any(
feature = "debug",
feature = "display",
feature = "from",
feature = "into",
))]
pub(crate) use self::either::Either;
#[cfg(any(feature = "from", feature = "into"))]
pub(crate) use self::{either::Either, fields_ext::FieldsExt};
pub(crate) use self::fields_ext::FieldsExt;

#[derive(Clone, Copy, Default)]
pub struct DeterministicState;
Expand Down Expand Up @@ -1347,10 +1354,16 @@ pub fn is_type_parameter_used_in_type(
}
}

#[cfg(any(feature = "from", feature = "into"))]
#[cfg(any(
feature = "debug",
feature = "display",
feature = "from",
feature = "into",
))]
mod either {
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::parse::{Parse, ParseStream};

/// Either [`Left`] or [`Right`].
///
Expand All @@ -1364,6 +1377,20 @@ mod either {
Right(R),
}

impl<L, R> Parse for Either<L, R>
where
L: Parse,
R: Parse,
{
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
if L::parse(&input.fork()).is_ok() {
L::parse(input).map(Self::Left)
} else {
R::parse(input).map(Self::Right)
}
}
}

impl<L, R, T> Iterator for Either<L, R>
where
L: Iterator<Item = T>,
Expand Down
4 changes: 4 additions & 0 deletions tests/compile_fail/debug/legacy_fmt_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ pub struct Foo {
bar: String,
}

#[derive(derive_more::Debug)]
#[debug(fmt = "Stuff({}): {}", _0)]
pub struct Bar(String);

fn main() {}
6 changes: 6 additions & 0 deletions tests/compile_fail/debug/legacy_fmt_syntax.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ error: legacy syntax, remove `fmt =` and use `"Stuff({}): {}", bar` instead
|
3 | #[debug(fmt = "Stuff({}): {}", "bar")]
| ^^^

error: legacy syntax, remove `fmt =` and use `"Stuff({}): {}", _0` instead
--> tests/compile_fail/debug/legacy_fmt_syntax.rs:8:9
|
8 | #[debug(fmt = "Stuff({}): {}", _0)]
| ^^^
4 changes: 4 additions & 0 deletions tests/compile_fail/display/legacy_fmt_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ pub struct Foo {
bar: String,
}

#[derive(derive_more::Display)]
#[display(fmt = "Stuff({}): {}", _0)]
pub struct Bar(String);

fn main() {}
6 changes: 6 additions & 0 deletions tests/compile_fail/display/legacy_fmt_syntax.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ error: legacy syntax, remove `fmt =` and use `"Stuff({}): {}", bar` instead
|
2 | #[display(fmt = "Stuff({}): {}", "bar")]
| ^^^

error: legacy syntax, remove `fmt =` and use `"Stuff({}): {}", _0` instead
--> tests/compile_fail/display/legacy_fmt_syntax.rs:8:11
|
8 | #[display(fmt = "Stuff({}): {}", _0)]
| ^^^