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

Check for None-delimited group in attribute value #2410

Merged
merged 2 commits into from Mar 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion serde_derive/Cargo.toml
Expand Up @@ -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" }
Expand Down
35 changes: 20 additions & 15 deletions serde_derive/src/internals/attr.rs
Expand Up @@ -1381,21 +1381,26 @@ fn get_lit_str2(
meta_item_name: Symbol,
meta: &ParseNestedMeta,
) -> syn::Result<Option<syn::LitStr>> {
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)
}
}

Expand Down
11 changes: 11 additions & 0 deletions test_suite/tests/regression/issue2409.rs
@@ -0,0 +1,11 @@
use serde::Deserialize;

macro_rules! bug {
($serde_path:literal) => {
#[derive(Deserialize)]
#[serde(crate = $serde_path)]
pub struct Struct;
};
}

bug!("serde");