[gcc r17-2241] gccrs: Fix field access on DST fat pointers

Arthur Cohen via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:3db9835f404e1a908e5c4ff86d277f3654c5c11b

commit r17-2241-g3db9835f404e1a908e5c4ff86d277f3654c5c11b
Author: Yap Zhi Heng <[email protected]>
Date:   Thu Jun 18 23:02:19 2026 +0800

    gccrs: Fix field access on DST fat pointers
    
    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]>

Diff:
---
 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 45765e72d688..7fabc25a5f8b 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,
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.