Skip to content

Commit 1536f52

Browse files
authored
Fix order sensitivity of Display attributes (#320, #319)
## Synopsis See #319 (comment): > The following code used to work in 1.0.0-beta.3: > > ```rust > #[derive(Display)] > #[display("{name}@{tag}")] > #[display(bound(Tag: std::fmt::Display))] > struct Specifier<Tag> { > name: String, > tag: Tag, > } > ``` > > However, in 1.0.0-beta.6, it emits the following error message: > > ``` > error: multiple `#[display("...", ...)]` attributes aren't allowed > ``` > > It took me a long time to figure out that the fix was to swap place between the 2 display attributes. The regression lies [here](https://github.com/JelteF/derive_more/blob/v1.0.0-beta.6/impl/src/fmt/mod.rs#L394-L399), where `mem::replace` doesn't provide the implied and expected "and" logic for merging two `Option`s. ## Solution - Merge those two `Option`s correctly. - Recheck similar places over the code.
1 parent b0b575f commit 1536f52

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

impl/src/fmt/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ pub(crate) mod debug;
88
pub(crate) mod display;
99
mod parsing;
1010

11-
use std::mem;
12-
1311
use proc_macro2::TokenStream;
1412
use quote::ToTokens;
1513
use syn::{
@@ -391,7 +389,7 @@ impl attr::ParseMultiple for ContainerAttributes {
391389
item: new,
392390
} = new;
393391

394-
if mem::replace(&mut prev.fmt, new.fmt).is_some() {
392+
if new.fmt.and_then(|n| prev.fmt.replace(n)).is_some() {
395393
return Err(syn::Error::new(
396394
new_span,
397395
format!("multiple `#[{name}(\"...\", ...)]` attributes aren't allowed"),

tests/display.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,8 @@ mod generic {
786786
}
787787

788788
#[derive(Display)]
789-
#[display(bound(T1: Trait1 + Trait2, T2: Trait1))]
790789
#[display("{} {} {} {} {:?}", _0.function1(), _0, _0.function2(), _1.function1(), _1)]
790+
#[display(bound(T1: Trait1 + Trait2, T2: Trait1))]
791791
struct Struct<T1, T2>(T1, T2);
792792

793793
let s = Struct(10, DebugOnly);
@@ -849,8 +849,8 @@ mod generic {
849849
}
850850

851851
#[derive(Display)]
852-
#[display(bound(T: Trait))]
853852
#[display("{}", _0.function())]
853+
#[display(bound(T: Trait))]
854854
struct Struct<T>(T);
855855

856856
let s = Struct(NoDisplay);

0 commit comments

Comments
 (0)