Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prost-build: CodeGenerator::boxed method #1019

Merged
merged 4 commits into from
May 5, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 47 additions & 22 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,19 +402,9 @@ impl<'a> CodeGenerator<'a> {
let repeated = field.descriptor.label == Some(Label::Repeated as i32);
let deprecated = self.deprecated(&field.descriptor);
let optional = self.optional(&field.descriptor);
let boxed = self.boxed(&field.descriptor, fq_message_name, None);
let ty = self.resolve_type(&field.descriptor, fq_message_name);

let boxed = !repeated
&& ((type_ == Type::Message || type_ == Type::Group)
&& self
.message_graph
.is_nested(field.descriptor.type_name(), fq_message_name))
|| (self
.config
.boxed
.get_first_field(fq_message_name, field.descriptor.name())
.is_some());

debug!(
" field: {:?}, type: {:?}, boxed: {}",
field.descriptor.name(),
Expand Down Expand Up @@ -636,8 +626,6 @@ impl<'a> CodeGenerator<'a> {
self.path.push(2);
self.depth += 1;
for field in &oneof.fields {
let type_ = field.descriptor.r#type();

self.path.push(field.path_index);
self.append_doc(fq_message_name, Some(field.descriptor.name()));
self.path.pop();
Expand All @@ -654,15 +642,11 @@ impl<'a> CodeGenerator<'a> {
self.push_indent();
let ty = self.resolve_type(&field.descriptor, fq_message_name);

let boxed = ((type_ == Type::Message || type_ == Type::Group)
&& self
.message_graph
.is_nested(field.descriptor.type_name(), fq_message_name))
|| (self
.config
.boxed
.get_first_field(&oneof_name, field.descriptor.name())
.is_some());
let boxed = self.boxed(
&field.descriptor,
fq_message_name,
Some(oneof.descriptor.name()),
);

debug!(
" oneof: {:?}, type: {:?}, boxed: {}",
Expand Down Expand Up @@ -1068,6 +1052,47 @@ impl<'a> CodeGenerator<'a> {
}
}

/// Returns whether the Rust type for this field needs to be `Box<_>`.
///
/// This can be explicitly configured with `Config::boxed`, or necessary
/// to prevent an infinitely sized type definition in case when the type of
/// a non-repeated message field transitively contains the message itself.
fn boxed(
&self,
field: &FieldDescriptorProto,
fq_message_name: &str,
oneof: Option<&str>,
) -> bool {
let repeated = field.label == Some(Label::Repeated as i32);
let fd_type = field.r#type();
if !repeated
&& (fd_type == Type::Message || fd_type == Type::Group)
&& self
.message_graph
.is_nested(field.type_name(), fq_message_name)
{
return true;
}
let config_path = match oneof {
None => Cow::Borrowed(fq_message_name),
Some(ooname) => Cow::Owned(format!("{fq_message_name}.{ooname}")),
};
caspermeijn marked this conversation as resolved.
Show resolved Hide resolved
if self
.config
.boxed
.get_first_field(&config_path, field.name())
.is_some()
{
println!(
"cargo:warning=\
Field X is repeated and manually marked as boxed. \
This is deprecated and support will be removed in a later release"
);
mzabaluev marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
false
}

/// Returns `true` if the field options includes the `deprecated` option.
fn deprecated(&self, field: &FieldDescriptorProto) -> bool {
field
Expand Down