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 generation of non-existent lifetime 'de when enum contains a #[serde(flatten)] field and a 'static reference #2383

Merged
merged 2 commits into from
Mar 8, 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
5 changes: 3 additions & 2 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ fn deserialize_struct(
lifetime: _serde::__private::PhantomData,
}
};
let need_seed = deserializer.is_none();
let dispatch = if let Some(deserializer) = deserializer {
quote! {
_serde::Deserializer::deserialize_any(#deserializer, #visitor_expr)
Expand Down Expand Up @@ -999,14 +1000,14 @@ fn deserialize_struct(
_ => None,
};

let visitor_seed = if is_enum && cattrs.has_flatten() {
let visitor_seed = if need_seed && is_enum && cattrs.has_flatten() {
Some(quote! {
impl #de_impl_generics _serde::de::DeserializeSeed<#delife> for __Visitor #de_ty_generics #where_clause {
type Value = #this_type #ty_generics;

fn deserialize<__D>(self, __deserializer: __D) -> _serde::__private::Result<Self::Value, __D::Error>
where
__D: _serde::Deserializer<'de>,
__D: _serde::Deserializer<#delife>,
{
_serde::Deserializer::deserialize_map(__deserializer, self)
}
Expand Down
50 changes: 50 additions & 0 deletions test_suite/tests/test_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,3 +892,53 @@ pub struct RemotePackedNonCopyDef {
impl Drop for RemotePackedNonCopyDef {
fn drop(&mut self) {}
}

//////////////////////////////////////////////////////////////////////////

/// Regression tests for <https://github.com/serde-rs/serde/issues/2371>
#[allow(dead_code)]
mod static_and_flatten {
use super::*;

#[derive(Deserialize)]
struct Nested;

#[derive(Deserialize)]
enum ExternallyTagged {
Flatten {
#[serde(flatten)]
nested: Nested,
string: &'static str,
},
}

#[derive(Deserialize)]
#[serde(tag = "tag")]
enum InternallyTagged {
Flatten {
#[serde(flatten)]
nested: Nested,
string: &'static str,
},
}

#[derive(Deserialize)]
#[serde(tag = "tag", content = "content")]
enum AdjacentlyTagged {
Flatten {
#[serde(flatten)]
nested: Nested,
string: &'static str,
},
}

#[derive(Deserialize)]
#[serde(untagged)]
enum UntaggedWorkaround {
Flatten {
#[serde(flatten)]
nested: Nested,
string: &'static str,
},
}
}