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

Cython: Omit per-variant tags in unions generated for Rust enums #748

Merged
merged 1 commit into from Mar 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit subtle, might be a bit clearer with:

let fields_to_write = if inline_tag_field && config.language == Language::Cython {
    &body.fields[1..]
} else {
    &body.fields
};

But sure, fine.

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