Fix for "cannot infer type" from Deserialize derive macro with simple

variants and untagged variants
This commit is contained in:
Adam H. Leventhal 2023-09-06 15:25:05 -07:00
parent 5d1403461c
commit 09993a904a
2 changed files with 12 additions and 3 deletions

View File

@ -1737,7 +1737,6 @@ fn deserialize_untagged_enum_after(
quote!(__deserializer),
))
});
let attempts = first_attempt.into_iter().chain(attempts);
// TODO this message could be better by saving the errors from the failed
// attempts. The heuristic used by TOML was to count the number of fields
// processed before an error, and use the error that happened after the
@ -1750,10 +1749,19 @@ fn deserialize_untagged_enum_after(
);
let fallthrough_msg = cattrs.expecting().unwrap_or(&fallthrough_msg);
// This may be infallible so we need to provide the error type.
let first_attempt = first_attempt.map(|expr| {
quote! {
if let _serde::__private::Result::<_, __D::Error>::Ok(__ok) = #expr {
return _serde::__private::Ok(__ok);
}
}
});
quote_block! {
let __content = <_serde::__private::de::Content as _serde::Deserialize>::deserialize(__deserializer)?;
let __deserializer = _serde::__private::de::ContentRefDeserializer::<__D::Error>::new(&__content);
#first_attempt
#(
if let _serde::__private::Ok(__ok) = #attempts {
return _serde::__private::Ok(__ok);

View File

@ -2383,7 +2383,7 @@ fn test_partially_untagged_enum_desugared() {
#[test]
fn test_partially_untagged_simple_enum() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(tag = "tag")]
#[serde(tag = "t")]
enum Data {
A,
#[serde(untagged)]
@ -2391,7 +2391,8 @@ fn test_partially_untagged_simple_enum() {
}
let data = Data::A;
assert_tokens(
assert_de_tokens(
&data,
&[
Token::Map { len: None },