From edcf973363ddfda0093c03814821efe7794251f1 Mon Sep 17 00:00:00 2001 From: Alex Konradi Date: Mon, 2 Dec 2024 11:12:34 -0500 Subject: [PATCH 1/2] Use destructuring instead of field point access This ensures that if a new field is added in the future, this code will be updated to handle it (one way or another), because not doing so will lead to a compilation error. This will prevent issues like what happened with oneof_nonexhaustive, where the field was added but its value was not propagated. --- protobuf-codegen/src/customize/mod.rs | 34 +++++++++++++++++++-------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/protobuf-codegen/src/customize/mod.rs b/protobuf-codegen/src/customize/mod.rs index 729aa4dd8..f04a63858 100644 --- a/protobuf-codegen/src/customize/mod.rs +++ b/protobuf-codegen/src/customize/mod.rs @@ -190,31 +190,45 @@ impl Customize { /// Update fields of self with fields defined in other customize pub fn update_with(&mut self, that: &Customize) { - if let Some(v) = &that.before { - self.before = Some(v.clone()); + // Intentionally exhaustive destructuring ensures that if fields are + // added but not at least mentioned here, this code won't compile. + let Customize { + before, + generate_accessors, + generate_getter, + tokio_bytes, + tokio_bytes_for_string, + oneofs_non_exhaustive, + lite_runtime, + gen_mod_rs, + inside_protobuf, + btreemap, + } = that.clone(); + if let Some(v) = before { + self.before = Some(v); } - if let Some(v) = that.generate_accessors { + if let Some(v) = generate_accessors { self.generate_accessors = Some(v); } - if let Some(v) = that.generate_getter { + if let Some(v) = generate_getter { self.generate_getter = Some(v); } - if let Some(v) = that.tokio_bytes { + if let Some(v) = tokio_bytes { self.tokio_bytes = Some(v); } - if let Some(v) = that.tokio_bytes_for_string { + if let Some(v) = tokio_bytes_for_string { self.tokio_bytes_for_string = Some(v); } - if let Some(v) = that.lite_runtime { + if let Some(v) = lite_runtime { self.lite_runtime = Some(v); } - if let Some(v) = that.gen_mod_rs { + if let Some(v) = gen_mod_rs { self.gen_mod_rs = Some(v); } - if let Some(v) = that.inside_protobuf { + if let Some(v) = inside_protobuf { self.inside_protobuf = Some(v); } - if let Some(v) = that.btreemap { + if let Some(v) = btreemap { self.btreemap = Some(v); } } From 6015b8d3767c5a94a499c14873be53b66d242890 Mon Sep 17 00:00:00 2001 From: Alex Konradi Date: Mon, 2 Dec 2024 11:17:03 -0500 Subject: [PATCH 2/2] Propagate oneofs_non_exhaustive value When constructing Customize instances for fields and inner messages, propagate the oneofs_non_exhaustive value. --- protobuf-codegen/src/customize/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/protobuf-codegen/src/customize/mod.rs b/protobuf-codegen/src/customize/mod.rs index f04a63858..ebc351640 100644 --- a/protobuf-codegen/src/customize/mod.rs +++ b/protobuf-codegen/src/customize/mod.rs @@ -231,6 +231,9 @@ impl Customize { if let Some(v) = btreemap { self.btreemap = Some(v); } + if let Some(v) = oneofs_non_exhaustive { + self.oneofs_non_exhaustive = Some(v); + } } /// Update unset fields of self with fields from other customize