diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index b74f895cb..33fa86bc5 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -663,14 +663,13 @@ impl Constant { Cow::Owned(format!("{}_{}", associated_name, self.export_name())) }; - let value = match self.value { - Literal::Struct { - ref fields, - ref path, - .. - } if out.bindings().struct_is_transparent(path) => fields.iter().next().unwrap().1, - _ => &self.value, - }; + let mut value = &self.value; + while let Literal::Struct { path, fields, .. } = value { + if !out.bindings().struct_is_transparent(path) { + break; + } + value = fields.iter().next().unwrap().1 + } language_backend.write_documentation(out, &self.documentation); diff --git a/tests/expectations/const_transparent.compat.c b/tests/expectations/const_transparent.compat.c index 642e72e01..62100f779 100644 --- a/tests/expectations/const_transparent.compat.c +++ b/tests/expectations/const_transparent.compat.c @@ -3,6 +3,17 @@ #include #include -typedef uint8_t Transparent; +typedef uint8_t TransparentStruct; +#define TransparentStruct_ASSOC_STRUCT_FOO 1 +#define TransparentStruct_ASSOC_STRUCT_BAR 2 + + +typedef uint8_t TransparentTupleStruct; + +#define STRUCT_FOO 4 + +#define STRUCT_BAR 5 + + + -#define FOO 0 diff --git a/tests/expectations/const_transparent.cpp b/tests/expectations/const_transparent.cpp index 920af50dc..963ee7fed 100644 --- a/tests/expectations/const_transparent.cpp +++ b/tests/expectations/const_transparent.cpp @@ -4,6 +4,23 @@ #include #include -using Transparent = uint8_t; +template +using Wrapper = T; -constexpr static const Transparent FOO = 0; +using TransparentStruct = uint8_t; +constexpr static const int64_t TransparentStruct_ASSOC_STRUCT_FOO = 1; +constexpr static const TransparentStruct TransparentStruct_ASSOC_STRUCT_BAR = 2; +constexpr static const Wrapper TransparentStruct_ASSOC_STRUCT_BAZ = 3; + +using TransparentTupleStruct = uint8_t; + +template +using TransparentStructWithErasedField = Wrapper; + +constexpr static const TransparentStruct STRUCT_FOO = 4; + +constexpr static const TransparentTupleStruct STRUCT_BAR = 5; + +constexpr static const Wrapper STRUCT_BAZ = 6; + +constexpr static const TransparentStructWithErasedField COMPLEX = 7; diff --git a/tests/expectations/const_transparent.pyx b/tests/expectations/const_transparent.pyx index 27e68d92c..c0993a3ae 100644 --- a/tests/expectations/const_transparent.pyx +++ b/tests/expectations/const_transparent.pyx @@ -6,6 +6,17 @@ cdef extern from *: cdef extern from *: - ctypedef uint8_t Transparent; + ctypedef uint8_t TransparentStruct; + const int64_t TransparentStruct_ASSOC_STRUCT_FOO # = 1 + const TransparentStruct TransparentStruct_ASSOC_STRUCT_BAR # = 2 + + + ctypedef uint8_t TransparentTupleStruct; + + const TransparentStruct STRUCT_FOO # = 4 + + const TransparentTupleStruct STRUCT_BAR # = 5 + + + - const Transparent FOO # = 0 diff --git a/tests/rust/const_transparent.rs b/tests/rust/const_transparent.rs index 7032953f7..83b9dacbe 100644 --- a/tests/rust/const_transparent.rs +++ b/tests/rust/const_transparent.rs @@ -1,4 +1,32 @@ #[repr(transparent)] -struct Transparent { field: u8 } +struct TransparentStruct { field: u8 } -pub const FOO: Transparent = Transparent { field: 0 }; +impl TransparentStruct { + pub const ASSOC_STRUCT_FOO: i64 = 1; + pub const ASSOC_STRUCT_BAR: TransparentStruct = TransparentStruct { field: 2 }; + + // TODO: Only C++ supports template constants so far. + pub const ASSOC_STRUCT_BAZ: Wrapper = Wrapper { field: TransparentStruct { field: 3 } }; +} + +#[repr(transparent)] +struct TransparentTupleStruct(u8); + +#[repr(transparent)] +struct Wrapper { field: T } + +pub const STRUCT_FOO: TransparentStruct = TransparentStruct { field: 4 }; +pub const STRUCT_BAR: TransparentTupleStruct = TransparentTupleStruct(5); + +// TODO: Only C++ supports template constants so far. +pub const STRUCT_BAZ: Wrapper = Wrapper { field: TransparentStruct { field: 6 } }; + +#[repr(transparent)] +struct TransparentStructWithErasedField { + field: Wrapper, +} + +// TODO: Only C++ supports template constants so far. +pub const COMPLEX: TransparentStructWithErasedField = TransparentStructWithErasedField { + field: Wrapper { field: TransparentStruct { field: 7 } } +};