diff --git a/prost-build/src/code_generator.rs b/prost-build/src/code_generator.rs index f8d341445..b7deb6b59 100644 --- a/prost-build/src/code_generator.rs +++ b/prost-build/src/code_generator.rs @@ -639,21 +639,21 @@ impl CodeGenerator<'_> { self.push_indent(); let ty_tag = self.field_type_tag(&field.descriptor); - self.buf.push_str(&format!( - "#[prost({}, tag=\"{}\")]\n", - ty_tag, - field.descriptor.number() - )); - self.append_field_attributes(&oneof_name, field.descriptor.name()); - - self.push_indent(); - let ty = self.resolve_type(&field.descriptor, fq_message_name); - let boxed = self.boxed( &field.descriptor, fq_message_name, Some(oneof.descriptor.name()), ); + self.buf.push_str(&format!("#[prost({}", ty_tag)); + if boxed { + self.buf.push_str(", boxed"); + } + self.buf + .push_str(&format!(", tag=\"{}\")]\n", field.descriptor.number())); + self.append_field_attributes(&oneof_name, field.descriptor.name()); + + self.push_indent(); + let ty = self.resolve_type(&field.descriptor, fq_message_name); debug!( " oneof: {:?}, type: {:?}, boxed: {}", diff --git a/prost-build/src/fixtures/field_attributes/_expected_field_attributes.rs b/prost-build/src/fixtures/field_attributes/_expected_field_attributes.rs index bf1e8c517..ce66b3690 100644 --- a/prost-build/src/fixtures/field_attributes/_expected_field_attributes.rs +++ b/prost-build/src/fixtures/field_attributes/_expected_field_attributes.rs @@ -8,7 +8,7 @@ pub struct Container { pub mod container { #[derive(Clone, PartialEq, ::prost::Oneof)] pub enum Data { - #[prost(message, tag="1")] + #[prost(message, boxed, tag="1")] Foo(::prost::alloc::boxed::Box), #[prost(message, tag="2")] Bar(super::Bar), diff --git a/prost-build/src/fixtures/field_attributes/_expected_field_attributes_formatted.rs b/prost-build/src/fixtures/field_attributes/_expected_field_attributes_formatted.rs index c130aad2e..8027120f5 100644 --- a/prost-build/src/fixtures/field_attributes/_expected_field_attributes_formatted.rs +++ b/prost-build/src/fixtures/field_attributes/_expected_field_attributes_formatted.rs @@ -8,7 +8,7 @@ pub struct Container { pub mod container { #[derive(Clone, PartialEq, ::prost::Oneof)] pub enum Data { - #[prost(message, tag = "1")] + #[prost(message, boxed, tag = "1")] Foo(::prost::alloc::boxed::Box), #[prost(message, tag = "2")] Bar(super::Bar), diff --git a/tests/build.rs b/tests/build.rs index 6bcd68862..8daefe6f4 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -169,6 +169,7 @@ fn main() { prost_build::Config::new() .boxed("Foo.bar") + .boxed("Foo.oneof_field.box_qux") .compile_protos(&[src.join("boxed_field.proto")], includes) .unwrap(); diff --git a/tests/src/boxed_field.proto b/tests/src/boxed_field.proto index 17f543d92..79b521e51 100644 --- a/tests/src/boxed_field.proto +++ b/tests/src/boxed_field.proto @@ -4,6 +4,10 @@ package boxed_field; message Foo { Bar bar = 1; + oneof oneof_field { + string baz = 2; + Bar box_qux = 3; + } } message Bar { diff --git a/tests/src/boxed_field.rs b/tests/src/boxed_field.rs index b41b7735c..b8fae8f29 100644 --- a/tests/src/boxed_field.rs +++ b/tests/src/boxed_field.rs @@ -1,10 +1,19 @@ include!(concat!(env!("OUT_DIR"), "/boxed_field.rs")); +cfg_if! { + if #[cfg(feature = "edition-2015")] { + use boxed_field::foo::OneofField; + } else { + use foo::OneofField; + } +} + #[test] -/// Confirm `Foo::bar` is boxed by creating an instance -fn test_bar_is_boxed() { +/// Confirm `Foo::bar` and `OneofField::BoxQux` is boxed by creating an instance +fn test_boxed_field() { use alloc::boxed::Box; let _ = Foo { bar: Some(Box::new(Bar {})), + oneof_field: Some(OneofField::BoxQux(Box::new(Bar {}))), }; }