From dd460f82a15656160d2f821ba91fc41578c1b399 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 20 Mar 2023 04:03:25 -0700 Subject: [PATCH] Check for None-delimited group in attribute value --- serde_derive/Cargo.toml | 2 +- serde_derive/src/internals/attr.rs | 35 +++++++++++++++++------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/serde_derive/Cargo.toml b/serde_derive/Cargo.toml index f2a3e37cf..b824bb993 100644 --- a/serde_derive/Cargo.toml +++ b/serde_derive/Cargo.toml @@ -24,7 +24,7 @@ proc-macro = true [dependencies] proc-macro2 = "1.0" quote = "1.0" -syn = "2.0" +syn = "2.0.3" [dev-dependencies] serde = { version = "1.0", path = "../serde" } diff --git a/serde_derive/src/internals/attr.rs b/serde_derive/src/internals/attr.rs index d541e3a9f..9875b6606 100644 --- a/serde_derive/src/internals/attr.rs +++ b/serde_derive/src/internals/attr.rs @@ -1381,21 +1381,26 @@ fn get_lit_str2( meta_item_name: Symbol, meta: &ParseNestedMeta, ) -> syn::Result> { - match meta.value()?.parse()? { - syn::Expr::Lit(syn::ExprLit { - lit: syn::Lit::Str(lit), - .. - }) => Ok(Some(lit)), - expr => { - cx.error_spanned_by( - expr, - format!( - "expected serde {} attribute to be a string: `{} = \"...\"`", - attr_name, meta_item_name - ), - ); - Ok(None) - } + let expr: syn::Expr = meta.value()?.parse()?; + let mut value = &expr; + while let syn::Expr::Group(e) = value { + value = &e.expr; + } + if let syn::Expr::Lit(syn::ExprLit { + lit: syn::Lit::Str(lit), + .. + }) = value + { + Ok(Some(lit.clone())) + } else { + cx.error_spanned_by( + expr, + format!( + "expected serde {} attribute to be a string: `{} = \"...\"`", + attr_name, meta_item_name + ), + ); + Ok(None) } }