Skip to content

Commit

Permalink
Implement codegen for message with huge number of fields
Browse files Browse the repository at this point in the history
`PartialEq` panics for these messages because of the issue in Rust
compiler.
  • Loading branch information
stepancheg committed May 18, 2018
1 parent 408fdf4 commit 4f1ca56
Show file tree
Hide file tree
Showing 3 changed files with 1,073 additions and 3 deletions.
26 changes: 25 additions & 1 deletion protobuf-codegen/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,18 @@ impl<'a> MessageGen<'a> {
});
}

fn supports_derive_partial_eq(&self) -> bool {
// There's stack overflow in the compiler when struct has too many fields
// https://github.com/rust-lang/rust/issues/40119
self.fields.len() <= 500
}

fn write_struct(&self, w: &mut CodeWriter) {
let mut derive = vec!["PartialEq", "Clone", "Default"];
let mut derive = Vec::new();
if self.supports_derive_partial_eq() {
derive.push("PartialEq");
}
derive.extend(&["Clone", "Default"]);
if self.lite_runtime {
derive.push("Debug");
}
Expand Down Expand Up @@ -430,9 +440,23 @@ impl<'a> MessageGen<'a> {
});
}

fn write_dummy_impl_partial_eq(&self, w: &mut CodeWriter) {
w.impl_for_block("::std::cmp::PartialEq", &self.type_name, |w| {
w.def_fn("eq(&self, _: &Self) -> bool", |w| {
w.comment("https://github.com/rust-lang/rust/issues/40119");
w.unimplemented();
});
});
}

pub fn write(&self, w: &mut CodeWriter) {
self.write_struct(w);

if !self.supports_derive_partial_eq() {
w.write_line("");
self.write_dummy_impl_partial_eq(w);
}

for oneof in self.oneofs() {
w.write_line("");
oneof.write_enum(w);
Expand Down
2 changes: 0 additions & 2 deletions protobuf-test/src/google/protobuf/import-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ cp protobuf-git/src/google/protobuf/unittest*.proto ./

rm -rf protobuf-git

# https://github.com/rust-lang/rust/issues/40119
rm unittest_enormous_descriptor.proto
rm *_proto3.proto

# vim: set ts=4 sw=4 et:
Loading

0 comments on commit 4f1ca56

Please sign in to comment.