From 870925d503e208606b947d4e359d9723d0bde8da Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 20 Aug 2023 21:35:08 -0700 Subject: [PATCH 1/2] Add repro of issue 2591 error[E0507]: cannot move out of `*__self` which is behind a shared reference --> test_suite/tests/test_remote.rs:210:10 | 210 | #[derive(Serialize, Deserialize)] | ^^^^^^^^^ | | | data moved here | move occurs because `unrecognized` has type `ErrorKind`, which does not implement the `Copy` trait | = note: this error originates in the derive macro `Serialize` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider borrowing here | 210 | #[derive(&Serialize, Deserialize)] | + --- test_suite/tests/test_remote.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test_suite/tests/test_remote.rs b/test_suite/tests/test_remote.rs index 7069cf9b6..c1f152eb8 100644 --- a/test_suite/tests/test_remote.rs +++ b/test_suite/tests/test_remote.rs @@ -127,7 +127,7 @@ struct Test { enum_concrete: remote::EnumGeneric, #[serde(with = "ErrorKindDef")] - io_error_kind: std::io::ErrorKind, + io_error_kind: ErrorKind, } #[derive(Serialize, Deserialize)] @@ -200,8 +200,16 @@ enum EnumConcrete { Variant(u8), } +#[derive(Debug)] +enum ErrorKind { + NotFound, + PermissionDenied, + #[allow(dead_code)] + ConnectionRefused, +} + #[derive(Serialize, Deserialize)] -#[serde(remote = "std::io::ErrorKind")] +#[serde(remote = "ErrorKind")] #[non_exhaustive] enum ErrorKindDef { NotFound, From 1f8c8ad5a3c70782ddfd1c219227bac3b139d15d Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 20 Aug 2023 21:36:55 -0700 Subject: [PATCH 2/2] Fix "cannot move out of `*self` which is behind a shared reference" --- serde_derive/src/ser.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/serde_derive/src/ser.rs b/serde_derive/src/ser.rs index 1f67f0430..a2cab263c 100644 --- a/serde_derive/src/ser.rs +++ b/serde_derive/src/ser.rs @@ -409,9 +409,9 @@ fn serialize_enum(params: &Parameters, variants: &[Variant], cattrs: &attr::Cont }) .collect(); - if cattrs.non_exhaustive() { + if cattrs.remote().is_some() && cattrs.non_exhaustive() { arms.push(quote! { - unrecognized => _serde::__private::Err(_serde::ser::Error::custom(_serde::__private::ser::CannotSerializeVariant(unrecognized))), + ref unrecognized => _serde::__private::Err(_serde::ser::Error::custom(_serde::__private::ser::CannotSerializeVariant(unrecognized))), }); }