From f207dbae8def5927962c350f2c030db9895f4be1 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 15 Mar 2022 15:18:42 +0000 Subject: [PATCH] builder_field_attr, builder_setter_attr: switch to ( ) syntx As per https://github.com/colin-kiegel/rust-derive-builder/pull/237#discussion_r826912617 --- derive_builder/src/lib.rs | 4 +--- .../tests/compile-fail/builder_field_attrs.rs | 4 +--- .../compile-fail/builder_field_attrs.stderr | 8 ++++---- .../tests/compile-fail/builder_setter_attrs.rs | 4 +--- .../compile-fail/builder_setter_attrs.stderr | 4 ++-- .../src/macro_options/darling_opts.rs | 17 +++++++++++++---- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/derive_builder/src/lib.rs b/derive_builder/src/lib.rs index c8fcb157..b68db046 100644 --- a/derive_builder/src/lib.rs +++ b/derive_builder/src/lib.rs @@ -565,9 +565,7 @@ //! #[derive(Builder)] //! #[builder(derive(serde::Serialize))] //! struct Lorem { -//! #[builder_field_attrs( -//! #[serde(rename="dolor")] -//! )] +//! #[builder_field_attrs(serde(rename="dolor"))] //! ipsum: String, //! } //! diff --git a/derive_builder/tests/compile-fail/builder_field_attrs.rs b/derive_builder/tests/compile-fail/builder_field_attrs.rs index efdc67c9..14b5d305 100644 --- a/derive_builder/tests/compile-fail/builder_field_attrs.rs +++ b/derive_builder/tests/compile-fail/builder_field_attrs.rs @@ -6,9 +6,7 @@ pub struct Lorem { ok: String, // This foolish repr() attribute generates an unused attribute warning - #[builder_field_attrs( - #[no_such_attribute] - )] + #[builder_field_attrs(no_such_attribute)] broken: String, } diff --git a/derive_builder/tests/compile-fail/builder_field_attrs.stderr b/derive_builder/tests/compile-fail/builder_field_attrs.stderr index f6365220..47cc7994 100644 --- a/derive_builder/tests/compile-fail/builder_field_attrs.stderr +++ b/derive_builder/tests/compile-fail/builder_field_attrs.stderr @@ -1,5 +1,5 @@ error: cannot find attribute `no_such_attribute` in this scope - --> tests/compile-fail/builder_field_attrs.rs:10:11 - | -10 | #[no_such_attribute] - | ^^^^^^^^^^^^^^^^^ + --> tests/compile-fail/builder_field_attrs.rs:9:27 + | +9 | #[builder_field_attrs(no_such_attribute)] + | ^^^^^^^^^^^^^^^^^ diff --git a/derive_builder/tests/compile-fail/builder_setter_attrs.rs b/derive_builder/tests/compile-fail/builder_setter_attrs.rs index c1b81a90..2f066fad 100644 --- a/derive_builder/tests/compile-fail/builder_setter_attrs.rs +++ b/derive_builder/tests/compile-fail/builder_setter_attrs.rs @@ -8,9 +8,7 @@ pub struct Lorem { ok: String, // This foolish repr() attribute generates an unused attribute warning - #[builder_setter_attrs( - #[must_use] - )] + #[builder_setter_attrs(must_use)] broken: usize, } diff --git a/derive_builder/tests/compile-fail/builder_setter_attrs.stderr b/derive_builder/tests/compile-fail/builder_setter_attrs.stderr index c8032ad4..5bb060b0 100644 --- a/derive_builder/tests/compile-fail/builder_setter_attrs.stderr +++ b/derive_builder/tests/compile-fail/builder_setter_attrs.stderr @@ -1,7 +1,7 @@ error: unused return value of `LoremBuilder::broken` that must be used - --> tests/compile-fail/builder_setter_attrs.rs:18:5 + --> tests/compile-fail/builder_setter_attrs.rs:16:5 | -18 | LoremBuilder::default().broken(42); +16 | LoremBuilder::default().broken(42); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: lint level defined here diff --git a/derive_builder_core/src/macro_options/darling_opts.rs b/derive_builder_core/src/macro_options/darling_opts.rs index 21bcef49..891be60a 100644 --- a/derive_builder_core/src/macro_options/darling_opts.rs +++ b/derive_builder_core/src/macro_options/darling_opts.rs @@ -4,8 +4,8 @@ use crate::BuildMethod; use darling::util::{Flag, PathList}; use darling::{self, FromMeta}; -use proc_macro2::Span; -use syn::parse::ParseStream; +use proc_macro2::{Span, TokenStream}; +use syn::parse::{ParseStream, Parser}; use syn::Meta; use syn::{self, spanned::Spanned, Attribute, Generics, Ident, Path, Visibility}; @@ -303,9 +303,18 @@ fn unnest_from_one_attribute(attr: syn::Attribute) -> darling::Result); impl syn::parse::Parse for ContainedAttributes { fn parse(input: ParseStream) -> syn::Result { + // Strip parentheses, and save the span of the parenthesis token let content; - let _paren_token = parenthesized!(content in input); - let attrs = content.call(syn::Attribute::parse_outer)?; + let paren_token = parenthesized!(content in input); + let wrap_span = paren_token.span; + + // Wrap up in #[ ] instead. + let pound = Token![#](wrap_span); // We can't write a literal # inside quote + let content: TokenStream = content.parse()?; + let content = quote_spanned!(wrap_span=> #pound [ #content ]); + + let parser = syn::Attribute::parse_outer; + let attrs = parser.parse2(content)?; Ok(Self(attrs)) } }