From: Philip Herron <herron.phi...@googlemail.com>

We cannot apply aligned or packed after layout_type is called you need
to set this up first then call it.

Fixes Rust-GCC#3260

gcc/rust/ChangeLog:

        * backend/rust-compile-type.cc (TyTyResolveCompile::visit): call lauout 
type directly
        * rust-backend.h (struct_type): add optional layout parameter
        (union_type): likewise
        (fill_in_fields): likewise
        * rust-gcc.cc (struct_type): likewise
        (union_type): likewise
        (fill_in_fields): only layout if we required

Signed-off-by: Philip Herron <herron.phi...@googlemail.com>
---
 gcc/rust/backend/rust-compile-type.cc |  8 +++++---
 gcc/rust/rust-backend.h               |  6 +++---
 gcc/rust/rust-gcc.cc                  | 15 +++++++++------
 3 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-type.cc 
b/gcc/rust/backend/rust-compile-type.cc
index 56d64e1405b..50b52fbd37f 100644
--- a/gcc/rust/backend/rust-compile-type.cc
+++ b/gcc/rust/backend/rust-compile-type.cc
@@ -22,6 +22,7 @@
 #include "rust-gcc.h"
 
 #include "tree.h"
+#include "stor-layout.h"
 
 namespace Rust {
 namespace Compile {
@@ -268,8 +269,8 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type)
          fields.push_back (std::move (f));
        }
 
-      type_record = type.is_union () ? Backend::union_type (fields)
-                                    : Backend::struct_type (fields);
+      type_record = type.is_union () ? Backend::union_type (fields, false)
+                                    : Backend::struct_type (fields, false);
     }
   else
     {
@@ -359,7 +360,7 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type)
        }
 
       // finally make the union or the enum
-      type_record = Backend::union_type (enum_fields);
+      type_record = Backend::union_type (enum_fields, false);
     }
 
   // Handle repr options
@@ -381,6 +382,7 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type)
       SET_TYPE_ALIGN (type_record, repr.align * 8);
       TYPE_USER_ALIGN (type_record) = 1;
     }
+  layout_type (type_record);
 
   std::string named_struct_str
     = type.get_ident ().path.get () + type.subst_as_string ();
diff --git a/gcc/rust/rust-backend.h b/gcc/rust/rust-backend.h
index 9d28cf6d393..414799edefe 100644
--- a/gcc/rust/rust-backend.h
+++ b/gcc/rust/rust-backend.h
@@ -133,11 +133,11 @@ function_ptr_type (tree result, const std::vector<tree> 
&praameters,
 
 // Get a struct type.
 tree
-struct_type (const std::vector<typed_identifier> &fields);
+struct_type (const std::vector<typed_identifier> &fields, bool layout = true);
 
 // Get a union type.
 tree
-union_type (const std::vector<typed_identifier> &fields);
+union_type (const std::vector<typed_identifier> &fields, bool layout = true);
 
 // Get an array type.
 tree
@@ -496,7 +496,7 @@ write_global_definitions (const std::vector<tree> 
&type_decls,
 // TODO: make static
 
 tree
-fill_in_fields (tree, const std::vector<typed_identifier> &);
+fill_in_fields (tree, const std::vector<typed_identifier> &, bool);
 
 tree fill_in_array (tree, tree, tree);
 
diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc
index 273ab7889b0..59983ede97d 100644
--- a/gcc/rust/rust-gcc.cc
+++ b/gcc/rust/rust-gcc.cc
@@ -592,23 +592,24 @@ function_ptr_type (tree result_type, const 
std::vector<tree> &parameters,
 // Make a struct type.
 
 tree
-struct_type (const std::vector<typed_identifier> &fields)
+struct_type (const std::vector<typed_identifier> &fields, bool layout)
 {
-  return fill_in_fields (make_node (RECORD_TYPE), fields);
+  return fill_in_fields (make_node (RECORD_TYPE), fields, layout);
 }
 
 // Make a union type.
 
 tree
-union_type (const std::vector<typed_identifier> &fields)
+union_type (const std::vector<typed_identifier> &fields, bool layout)
 {
-  return fill_in_fields (make_node (UNION_TYPE), fields);
+  return fill_in_fields (make_node (UNION_TYPE), fields, layout);
 }
 
 // Fill in the fields of a struct or union type.
 
 tree
-fill_in_fields (tree fill, const std::vector<typed_identifier> &fields)
+fill_in_fields (tree fill, const std::vector<typed_identifier> &fields,
+               bool layout)
 {
   tree field_trees = NULL_TREE;
   tree *pp = &field_trees;
@@ -625,7 +626,9 @@ fill_in_fields (tree fill, const 
std::vector<typed_identifier> &fields)
       pp = &DECL_CHAIN (field);
     }
   TYPE_FIELDS (fill) = field_trees;
-  layout_type (fill);
+
+  if (layout)
+    layout_type (fill);
 
   // Because Rust permits converting between named struct types and
   // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
-- 
2.45.2

Reply via email to