From: Yap Zhi Heng <[email protected]>

gcc/rust/ChangeLog:

        * backend/rust-compile-expr.cc (CompileExpr::visit (FieldAccessExpr)):
        Handle DST fat pointers by reinterpreting the fat pointer as its field 
type.

Signed-Off-By: Yap Zhi Heng <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/2190c42144381c879e5264d4d8cb812207a9172d

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4565

 gcc/rust/backend/rust-compile-expr.cc | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.cc 
b/gcc/rust/backend/rust-compile-expr.cc
index 45765e72d..7fabc25a5 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -717,8 +717,23 @@ CompileExpr::visit (HIR::FieldAccessExpr &expr)
                                       nullptr, &field_index);
       rust_assert (ok);
 
-      tree indirect = indirect_expression (receiver_ref, expr.get_locus ());
-      receiver_ref = indirect;
+      // TODO this check is only used for CStr, test again when we support
+      // compilation of #[repr(transparent)] structs
+      if (RS_DST_FLAG_P (TREE_TYPE (receiver_ref)))
+       {
+         const TyTy::StructFieldType *field
+           = variant->get_field_at_index (field_index);
+         tree field_type
+           = TyTyResolveCompile::compile (ctx, field->get_field_type ());
+         translated = fold_build1_loc (expr.get_locus (), VIEW_CONVERT_EXPR,
+                                       field_type, receiver_ref);
+         return;
+       }
+      else
+       {
+         tree indirect = indirect_expression (receiver_ref, expr.get_locus ());
+         receiver_ref = indirect;
+       }
     }
 
   translated = Backend::struct_field_expression (receiver_ref, field_index,
-- 
2.54.0

Reply via email to