Skip to content

Commit

Permalink
Cython: Omit per-variant tags in unions generated for Rust enums
Browse files Browse the repository at this point in the history
The common `tag` should already be enough, al other tags are identical to it.
In Cython case they only create noise because the declarations only introduce names and do not determine layouts.
  • Loading branch information
petrochenkov authored and emilio committed Mar 31, 2022
1 parent 522759e commit ca7a942
Show file tree
Hide file tree
Showing 17 changed files with 12 additions and 47 deletions.
15 changes: 12 additions & 3 deletions src/bindgen/ir/enumeration.rs
Expand Up @@ -710,7 +710,7 @@ impl Source for Enum {
}

// Emit fields for all variants with data.
self.write_variant_fields(config, out);
self.write_variant_fields(config, out, inline_tag_field);

// Close union of all variants with data, only in the non-inline tag scenario.
// See the comment about Cython on `open_brace`.
Expand Down Expand Up @@ -940,7 +940,12 @@ impl Enum {
}

/// Emit fields for all variants with data.
fn write_variant_fields<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
fn write_variant_fields<F: Write>(
&self,
config: &Config,
out: &mut SourceWriter<F>,
inline_tag_field: bool,
) {
let mut first = true;
for variant in &self.variants {
if let VariantBody::Body {
Expand All @@ -962,11 +967,15 @@ impl Enum {
// by the corresponding C code. So we can inline the unnamed struct and get the
// same observable result. Moreother we have to do it because Cython doesn't
// support unnamed structs.
// For the same reason with Cython we can omit per-variant tags (the first
// field) to avoid extra noise, the main `tag` is enough in this case.
if config.language != Language::Cython {
out.write("struct");
out.open_brace();
}
out.write_vertical_source_list(&body.fields, ListType::Cap(";"));
let start_field =
usize::from(inline_tag_field && config.language == Language::Cython);
out.write_vertical_source_list(&body.fields[start_field..], ListType::Cap(";"));
if config.language != Language::Cython {
out.close_brace(true);
}
Expand Down
1 change: 0 additions & 1 deletion tests/expectations/annotation.pyx
Expand Up @@ -31,7 +31,6 @@ cdef extern from *:

ctypedef union F:
F_Tag tag;
F_Tag foo_tag;
int16_t foo;
Bar_Body bar;

Expand Down
1 change: 0 additions & 1 deletion tests/expectations/annotation.tag.pyx
Expand Up @@ -31,7 +31,6 @@ cdef extern from *:

cdef union F:
F_Tag tag;
F_Tag foo_tag;
int16_t foo;
Bar_Body bar;

Expand Down
1 change: 0 additions & 1 deletion tests/expectations/asserted_cast.pyx
Expand Up @@ -56,7 +56,6 @@ cdef extern from *:

ctypedef union K:
K_Tag tag;
K_Tag foo_tag;
int16_t foo;
K_Bar_Body bar;

Expand Down
1 change: 0 additions & 1 deletion tests/expectations/asserted_cast.tag.pyx
Expand Up @@ -56,7 +56,6 @@ cdef extern from *:

cdef union K:
K_Tag tag;
K_Tag foo_tag;
int16_t foo;
K_Bar_Body bar;

Expand Down
1 change: 0 additions & 1 deletion tests/expectations/derive_ostream.pyx
Expand Up @@ -36,7 +36,6 @@ cdef extern from *:

ctypedef union F:
F_Tag tag;
F_Tag foo_tag;
int16_t foo;
Bar_Body bar;

Expand Down
1 change: 0 additions & 1 deletion tests/expectations/derive_ostream.tag.pyx
Expand Up @@ -36,7 +36,6 @@ cdef extern from *:

cdef union F:
F_Tag tag;
F_Tag foo_tag;
int16_t foo;
Bar_Body bar;

Expand Down
11 changes: 0 additions & 11 deletions tests/expectations/destructor_and_copy_ctor.pyx
Expand Up @@ -81,11 +81,8 @@ cdef extern from *:

ctypedef union Baz_i32:
Baz_i32_Tag tag;
Baz_i32_Tag polygon21_tag;
Polygon_i32 polygon21;
Baz_i32_Tag slice21_tag;
OwnedSlice_i32 slice21;
Baz_i32_Tag slice22_tag;
OwnedSlice_i32 slice22;
Slice23_Body_i32 slice23;
Slice24_Body_i32 slice24;
Expand All @@ -98,9 +95,7 @@ cdef extern from *:

ctypedef union Taz:
Taz_Tag tag;
Taz_Tag taz1_tag;
int32_t taz1;
Taz_Tag taz3_tag;
OwnedSlice_i32 taz3;

cdef enum:
Expand All @@ -110,7 +105,6 @@ cdef extern from *:

ctypedef union Tazz:
Tazz_Tag tag;
Tazz_Tag taz2_tag;
int32_t taz2;

cdef enum:
Expand All @@ -120,7 +114,6 @@ cdef extern from *:

ctypedef union Tazzz:
Tazzz_Tag tag;
Tazzz_Tag taz5_tag;
int32_t taz5;

cdef enum:
Expand All @@ -130,9 +123,7 @@ cdef extern from *:

ctypedef union Tazzzz:
Tazzzz_Tag tag;
Tazzzz_Tag taz6_tag;
int32_t taz6;
Tazzzz_Tag taz7_tag;
uint32_t taz7;

cdef enum:
Expand All @@ -142,9 +133,7 @@ cdef extern from *:

ctypedef union Qux:
Qux_Tag tag;
Qux_Tag qux1_tag;
int32_t qux1;
Qux_Tag qux2_tag;
uint32_t qux2;

void root(const Foo_u32 *a,
Expand Down
11 changes: 0 additions & 11 deletions tests/expectations/destructor_and_copy_ctor.tag.pyx
Expand Up @@ -81,11 +81,8 @@ cdef extern from *:

cdef union Baz_i32:
Baz_i32_Tag tag;
Baz_i32_Tag polygon21_tag;
Polygon_i32 polygon21;
Baz_i32_Tag slice21_tag;
OwnedSlice_i32 slice21;
Baz_i32_Tag slice22_tag;
OwnedSlice_i32 slice22;
Slice23_Body_i32 slice23;
Slice24_Body_i32 slice24;
Expand All @@ -98,9 +95,7 @@ cdef extern from *:

cdef union Taz:
Taz_Tag tag;
Taz_Tag taz1_tag;
int32_t taz1;
Taz_Tag taz3_tag;
OwnedSlice_i32 taz3;

cdef enum:
Expand All @@ -110,7 +105,6 @@ cdef extern from *:

cdef union Tazz:
Tazz_Tag tag;
Tazz_Tag taz2_tag;
int32_t taz2;

cdef enum:
Expand All @@ -120,7 +114,6 @@ cdef extern from *:

cdef union Tazzz:
Tazzz_Tag tag;
Tazzz_Tag taz5_tag;
int32_t taz5;

cdef enum:
Expand All @@ -130,9 +123,7 @@ cdef extern from *:

cdef union Tazzzz:
Tazzzz_Tag tag;
Tazzzz_Tag taz6_tag;
int32_t taz6;
Tazzzz_Tag taz7_tag;
uint32_t taz7;

cdef enum:
Expand All @@ -142,9 +133,7 @@ cdef extern from *:

cdef union Qux:
Qux_Tag tag;
Qux_Tag qux1_tag;
int32_t qux1;
Qux_Tag qux2_tag;
uint32_t qux2;

void root(const Foo_u32 *a,
Expand Down
1 change: 0 additions & 1 deletion tests/expectations/enum.pyx
Expand Up @@ -95,7 +95,6 @@ cdef extern from *:

ctypedef union G:
G_Tag tag;
G_Tag foo_tag;
int16_t foo;
Bar_Body bar;

Expand Down
1 change: 0 additions & 1 deletion tests/expectations/enum.tag.pyx
Expand Up @@ -95,7 +95,6 @@ cdef extern from *:

cdef union G:
G_Tag tag;
G_Tag foo_tag;
int16_t foo;
Bar_Body bar;

Expand Down
2 changes: 0 additions & 2 deletions tests/expectations/enum_self.pyx
Expand Up @@ -17,9 +17,7 @@ cdef extern from *:

ctypedef union Bar:
Bar_Tag tag;
Bar_Tag min_tag;
Foo_Bar min;
Bar_Tag max_tag;
Foo_Bar max;

void root(Bar b);
2 changes: 0 additions & 2 deletions tests/expectations/enum_self.tag.pyx
Expand Up @@ -17,9 +17,7 @@ cdef extern from *:

cdef union Bar:
Bar_Tag tag;
Bar_Tag min_tag;
Foo_Bar min;
Bar_Tag max_tag;
Foo_Bar max;

void root(Bar b);
1 change: 0 additions & 1 deletion tests/expectations/prefix.pyx
Expand Up @@ -24,7 +24,6 @@ cdef extern from *:

ctypedef union PREFIX_AbsoluteFontWeight:
PREFIX_AbsoluteFontWeight_Tag tag;
PREFIX_AbsoluteFontWeight_Tag weight_tag;
float weight;

void root(PREFIX_NamedLenArray x, PREFIX_ValuedLenArray y, PREFIX_AbsoluteFontWeight z);
1 change: 0 additions & 1 deletion tests/expectations/prefix.tag.pyx
Expand Up @@ -24,7 +24,6 @@ cdef extern from *:

cdef union PREFIX_AbsoluteFontWeight:
PREFIX_AbsoluteFontWeight_Tag tag;
PREFIX_AbsoluteFontWeight_Tag weight_tag;
float weight;

void root(PREFIX_NamedLenArray x, PREFIX_ValuedLenArray y, PREFIX_AbsoluteFontWeight z);
4 changes: 0 additions & 4 deletions tests/expectations/transform_op.pyx
Expand Up @@ -30,9 +30,7 @@ cdef extern from *:
ctypedef union StyleFoo_i32:
StyleFoo_i32_Tag tag;
StyleFoo_Body_i32 foo;
StyleFoo_i32_Tag bar_tag;
int32_t bar;
StyleFoo_i32_Tag baz_tag;
StylePoint_i32 baz;

ctypedef enum StyleBar_i32_Tag:
Expand Down Expand Up @@ -83,9 +81,7 @@ cdef extern from *:

ctypedef union StyleBaz:
StyleBaz_Tag tag;
StyleBaz_Tag baz1_tag;
StyleBar_u32 baz1;
StyleBaz_Tag baz2_tag;
StylePoint_i32 baz2;

cdef enum:
Expand Down
4 changes: 0 additions & 4 deletions tests/expectations/transform_op.tag.pyx
Expand Up @@ -30,9 +30,7 @@ cdef extern from *:
cdef union StyleFoo_i32:
StyleFoo_i32_Tag tag;
StyleFoo_Body_i32 foo;
StyleFoo_i32_Tag bar_tag;
int32_t bar;
StyleFoo_i32_Tag baz_tag;
StylePoint_i32 baz;

cdef enum StyleBar_i32_Tag:
Expand Down Expand Up @@ -83,9 +81,7 @@ cdef extern from *:

cdef union StyleBaz:
StyleBaz_Tag tag;
StyleBaz_Tag baz1_tag;
StyleBar_u32 baz1;
StyleBaz_Tag baz2_tag;
StylePoint_i32 baz2;

cdef enum:
Expand Down

0 comments on commit ca7a942

Please sign in to comment.