diff --git a/protobuf-codegen/src/gen/field/mod.rs b/protobuf-codegen/src/gen/field/mod.rs index d6a5e44a4..ac9617289 100644 --- a/protobuf-codegen/src/gen/field/mod.rs +++ b/protobuf-codegen/src/gen/field/mod.rs @@ -185,11 +185,43 @@ impl<'a> FieldGen<'a> { } RuntimeFieldType::Repeated(..) => { let elem = field_elem(&field, root_scope, &customize); - - FieldKind::Repeated(RepeatedField { - elem, - packed: field.field.proto().options.get_or_default().packed(), - }) + let primitive = match field.field.proto().type_() { + Type::TYPE_DOUBLE + | Type::TYPE_FLOAT + | Type::TYPE_INT64 + | Type::TYPE_UINT64 + | Type::TYPE_INT32 + | Type::TYPE_FIXED64 + | Type::TYPE_FIXED32 + | Type::TYPE_BOOL + | Type::TYPE_UINT32 + | Type::TYPE_SFIXED32 + | Type::TYPE_SFIXED64 + | Type::TYPE_SINT32 + | Type::TYPE_SINT64 + | Type::TYPE_ENUM => true, + Type::TYPE_STRING + | Type::TYPE_GROUP + | Type::TYPE_MESSAGE + | Type::TYPE_BYTES => false, + }; + let packed = field + .field + .proto() + .options + .get_or_default() + .packed + .unwrap_or(match field.message.scope.file_scope.syntax() { + Syntax::Proto2 => false, + // in proto3, repeated primitive types are packed by default + Syntax::Proto3 => primitive, + }); + if packed && !primitive { + anyhow::bail!( + "[packed = true] can only be specified for repeated primitive fields" + ); + } + FieldKind::Repeated(RepeatedField { elem, packed }) } RuntimeFieldType::Singular(..) => { let elem = field_elem(&field, root_scope, &customize); diff --git a/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed.rs b/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed.rs index d8ecbb02a..8e6191aaa 100644 --- a/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed.rs +++ b/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed.rs @@ -1,3 +1,5 @@ +use protobuf::reflect::Syntax; +use protobuf::MessageFull; use protobuf_test_common::*; use super::test_repeated_packed_pb::*; @@ -83,3 +85,16 @@ fn test_issue_281() { test.values = (0..100).collect(); test_serialize_deserialize_no_hex(&test); } + +#[test] +fn test_write_packed_default() { + let mut test = TestPackedDefault::new(); + test.varints = vec![0, 1, 2, 3, 4, 5]; + + // Proto3 packs primitives by default, proto2 does not. + let expected_hex = match TestPackedDefault::descriptor().file_descriptor().syntax() { + Syntax::Proto2 => "08 00 08 01 08 02 08 03 08 04 08 05", + Syntax::Proto3 => "0a 06 00 01 02 03 04 05", + }; + test_serialize_deserialize(expected_hex, &test); +} diff --git a/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed_pb.proto b/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed_pb.proto index c4b543641..76743ea0d 100644 --- a/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed_pb.proto +++ b/test-crates/protobuf-codegen-protoc-test/src/common/v2/test_repeated_packed_pb.proto @@ -13,3 +13,7 @@ message TestUnpacked { message TestIssue281 { repeated fixed32 values = 1 [packed=true]; } + +message TestPackedDefault { + repeated uint32 varints = 1; +}