Skip to content

Commit

Permalink
Provide a better error on a packed non-primitive.
Browse files Browse the repository at this point in the history
Fixes #706.
  • Loading branch information
rcorre committed Jan 17, 2024
1 parent 43df676 commit d602529
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions protobuf-codegen/src/gen/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,40 +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
.unwrap_or(match field.message.scope.file_scope.syntax() {
Syntax::Proto2 => false,
// in proto3, repeated primitive types are packed by default
Syntax::Proto3 => 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 => true,
Type::TYPE_STRING
| Type::TYPE_GROUP
| Type::TYPE_MESSAGE
| Type::TYPE_BYTES
| Type::TYPE_ENUM => false,
},
}),
})
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 => true,
Type::TYPE_STRING
| Type::TYPE_GROUP
| Type::TYPE_MESSAGE
| Type::TYPE_BYTES
| Type::TYPE_ENUM => 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);
Expand Down

0 comments on commit d602529

Please sign in to comment.