aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSid Nayyar <sidnayyar@google.com>2024-05-20 12:19:52 +0100
committerGiuliano Procida <gprocida@google.com>2024-05-21 11:39:06 +0100
commitd27f0faecf4741734cc3dfe8d6c504f4705d327e (patch)
tree64ef1ff80a866094610e5ce8ea737d76dceb348b
parentd25d2cba4160293a9348a08af2a46259e33f8dd8 (diff)
downloadstg-upstream-main.tar.gz
rust: add test where discriminant is offset from beginning in the variant data layoutupstream-main
This is an optimisation which the Rust compiler can make by overlapping the discriminant with variant members data locations while exploiting bit patterns which would be illegal for the variant members. PiperOrigin-RevId: 635414233 Change-Id: I5da9db7326dfea4756b8edebd19fa8e6c30ea30a
-rw-r--r--test_cases/info_tests/variant/expected/offset_discriminant_rs.elf_stg106
-rw-r--r--test_cases/info_tests/variant/offset_discriminant.rs13
2 files changed, 119 insertions, 0 deletions
diff --git a/test_cases/info_tests/variant/expected/offset_discriminant_rs.elf_stg b/test_cases/info_tests/variant/expected/offset_discriminant_rs.elf_stg
new file mode 100644
index 0000000..e7ab069
--- /dev/null
+++ b/test_cases/info_tests/variant/expected/offset_discriminant_rs.elf_stg
@@ -0,0 +1,106 @@
+version: 0x00000002
+root_id: 0x84ea5130 # interface
+primitive {
+ id: 0x384f7d7c
+ name: "char"
+ encoding: UTF
+ bytesize: 0x00000004
+}
+primitive {
+ id: 0x62aebfd4
+ name: "bool"
+ encoding: BOOLEAN
+ bytesize: 0x00000001
+}
+primitive {
+ id: 0xd4bacb77
+ name: "u32"
+ encoding: UNSIGNED_INTEGER
+ bytesize: 0x00000004
+}
+member {
+ id: 0x16e523e2
+ type_id: 0xd4bacb77 # u32
+ offset: 32
+}
+member {
+ id: 0x97813cf0
+ name: "__0"
+ type_id: 0x384f7d7c # char
+}
+member {
+ id: 0xdbc0cd2f
+ name: "__1"
+ type_id: 0x384f7d7c # char
+ offset: 32
+}
+variant_member {
+ id: 0x56b9f935
+ name: "Two"
+ type_id: 0x573dc947
+}
+variant_member {
+ id: 0x69cfc441
+ name: "One"
+ discriminant_value: 1114112
+ type_id: 0x988e2459
+}
+variant_member {
+ id: 0x276cfc01
+ name: "Zero"
+ discriminant_value: 1114113
+ type_id: 0xb2cd8432
+}
+struct_union {
+ id: 0x988e2459
+ kind: STRUCT
+ name: "offset_discriminant::Foo::One"
+ definition {
+ bytesize: 8
+ member_id: 0x97813cf0 # char __0
+ }
+}
+struct_union {
+ id: 0x573dc947
+ kind: STRUCT
+ name: "offset_discriminant::Foo::Two"
+ definition {
+ bytesize: 8
+ member_id: 0x97813cf0 # char __0
+ member_id: 0xdbc0cd2f # char __1
+ }
+}
+struct_union {
+ id: 0xb2cd8432
+ kind: STRUCT
+ name: "offset_discriminant::Foo::Zero"
+ definition {
+ bytesize: 8
+ }
+}
+variant {
+ id: 0x82b2aa29
+ name: "offset_discriminant::Foo"
+ bytesize: 8
+ discriminant: 0x16e523e2
+ member_id: 0x56b9f935
+ member_id: 0x69cfc441
+ member_id: 0x276cfc01
+}
+function {
+ id: 0x9efcc134
+ return_type_id: 0x62aebfd4 # bool
+ parameter_id: 0x82b2aa29 # variant offset_discriminant::Foo
+}
+elf_symbol {
+ id: 0x699cebd9
+ name: "is_zero"
+ is_defined: true
+ symbol_type: FUNCTION
+ type_id: 0x9efcc134 # bool(variant offset_discriminant::Foo)
+ full_name: "offset_discriminant::is_zero"
+}
+interface {
+ id: 0x84ea5130
+ symbol_id: 0x699cebd9 # bool offset_discriminant::is_zero(variant offset_discriminant::Foo)
+}
diff --git a/test_cases/info_tests/variant/offset_discriminant.rs b/test_cases/info_tests/variant/offset_discriminant.rs
new file mode 100644
index 0000000..a7aca13
--- /dev/null
+++ b/test_cases/info_tests/variant/offset_discriminant.rs
@@ -0,0 +1,13 @@
+pub enum Foo {
+ Two(char, char),
+ One(char),
+ Zero,
+}
+
+#[no_mangle]
+pub fn is_zero(foo: Foo) -> bool {
+ match foo {
+ Foo::Zero => true,
+ _ => false,
+ }
+}