Skip to content

Emit the enum discriminant separately for the Encodable macro #108440

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

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions compiler/rustc_macros/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ fn decodable_body(
}
let ty_name = s.ast().ident.to_string();
let decode_body = match s.variants() {
[] => {
let message = format!("`{}` has no variants to decode", ty_name);
quote! {
panic!(#message)
}
}
[vi] => vi.construct(|field, _index| decode_field(field)),
variants => {
let match_inner: TokenStream = variants
Expand Down Expand Up @@ -139,6 +145,11 @@ fn encodable_body(
});

let encode_body = match s.variants() {
[] => {
quote! {
match *self {}
}
}
[_] => {
let encode_inner = s.each_variant(|vi| {
vi.bindings()
Expand All @@ -160,6 +171,23 @@ fn encodable_body(
}
}
_ => {
let disc = {
let mut variant_idx = 0usize;
let encode_inner = s.each_variant(|_| {
let result = quote! {
#variant_idx
};
variant_idx += 1;
result
});
quote! {
let disc = match *self {
#encode_inner
};
::rustc_serialize::Encoder::emit_usize(__encoder, disc);
}
};

let mut variant_idx = 0usize;
let encode_inner = s.each_variant(|vi| {
let encode_fields: TokenStream = vi
Expand All @@ -176,26 +204,11 @@ fn encodable_body(
result
})
.collect();

let result = if !vi.bindings().is_empty() {
quote! {
::rustc_serialize::Encoder::emit_enum_variant(
__encoder,
#variant_idx,
|__encoder| { #encode_fields }
)
}
} else {
quote! {
::rustc_serialize::Encoder::emit_fieldless_enum_variant::<#variant_idx>(
__encoder,
)
}
};
variant_idx += 1;
result
encode_fields
});
quote! {
#disc
match *self {
#encode_inner
}
Expand Down
12 changes: 0 additions & 12 deletions compiler/rustc_serialize/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,13 @@ pub trait Encoder {
fn emit_str(&mut self, v: &str);
fn emit_raw_bytes(&mut self, s: &[u8]);

// Convenience for the derive macro:
fn emit_enum_variant<F>(&mut self, v_id: usize, f: F)
where
F: FnOnce(&mut Self),
{
self.emit_usize(v_id);
f(self);
}

// We put the field index in a const generic to allow the emit_usize to be
// compiled into a more efficient form. In practice, the variant index is
// known at compile-time, and that knowledge allows much more efficient
// codegen than we'd otherwise get. LLVM isn't always able to make the
// optimization that would otherwise be necessary here, likely due to the
// multiple levels of inlining and const-prop that are needed.
#[inline]
fn emit_fieldless_enum_variant<const ID: usize>(&mut self) {
self.emit_usize(ID)
}
}

// Note: all the methods in this trait are infallible, which may be surprising.
Expand Down