public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] Ensure the coercion sites are applied correctly on unions
Date: Wed,  8 Jun 2022 12:44:34 +0000 (GMT)	[thread overview]
Message-ID: <20220608124434.315B938936C8@sourceware.org> (raw)

https://gcc.gnu.org/g:224647d24a3fe0411a46ac97d76e06fe2156a9fc

commit 224647d24a3fe0411a46ac97d76e06fe2156a9fc
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri May 6 12:22:46 2022 +0100

    Ensure the coercion sites are applied correctly on unions
    
    When we construct a union, we were wrongly compiling the constructor
    without taking into account the associated union index. This means we ended
    up trying to coerce the wrong field which was always zero here which
    resulted in a silent failure by returning error mark node.
    
    The offending test case was: gcc/testsuite/execute/torture/slice1.rs

Diff:
---
 gcc/rust/backend/rust-compile-expr.h | 48 ++++++++++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index bb81d4ed7bc..79d9e577dd2 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -419,18 +419,19 @@ public:
     // compile it
     tree compiled_adt_type = TyTyResolveCompile::compile (ctx, tyty);
 
-    // this assumes all fields are in order from type resolution and if a base
-    // struct was specified those fields are filed via accesors
     std::vector<tree> arguments;
-    for (size_t i = 0; i < struct_expr.get_fields ().size (); i++)
+    if (adt->is_union ())
       {
+	rust_assert (struct_expr.get_fields ().size () == 1);
+
 	// assignments are coercion sites so lets convert the rvalue if
 	// necessary
-	auto respective_field = variant->get_field_at_index (i);
+	auto respective_field
+	  = variant->get_field_at_index (union_disriminator);
 	auto expected = respective_field->get_field_type ();
 
 	// process arguments
-	auto &argument = struct_expr.get_fields ().at (i);
+	auto &argument = struct_expr.get_fields ().at (0);
 	auto lvalue_locus
 	  = ctx->get_mappings ()->lookup_location (expected->get_ty_ref ());
 	auto rvalue_locus = argument->get_locus ();
@@ -440,8 +441,6 @@ public:
 	bool ok = ctx->get_tyctx ()->lookup_type (
 	  argument->get_mappings ().get_hirid (), &actual);
 
-	// coerce it if required/possible see
-	// compile/torture/struct_base_init_1.rs
 	if (ok)
 	  {
 	    rvalue = coercion_site (rvalue, actual, expected, lvalue_locus,
@@ -451,6 +450,41 @@ public:
 	// add it to the list
 	arguments.push_back (rvalue);
       }
+    else
+      {
+	// this assumes all fields are in order from type resolution and if a
+	// base struct was specified those fields are filed via accesors
+	for (size_t i = 0; i < struct_expr.get_fields ().size (); i++)
+	  {
+	    // assignments are coercion sites so lets convert the rvalue if
+	    // necessary
+	    auto respective_field = variant->get_field_at_index (i);
+	    auto expected = respective_field->get_field_type ();
+
+	    // process arguments
+	    auto &argument = struct_expr.get_fields ().at (i);
+	    auto lvalue_locus
+	      = ctx->get_mappings ()->lookup_location (expected->get_ty_ref ());
+	    auto rvalue_locus = argument->get_locus ();
+	    auto rvalue
+	      = CompileStructExprField::Compile (argument.get (), ctx);
+
+	    TyTy::BaseType *actual = nullptr;
+	    bool ok = ctx->get_tyctx ()->lookup_type (
+	      argument->get_mappings ().get_hirid (), &actual);
+
+	    // coerce it if required/possible see
+	    // compile/torture/struct_base_init_1.rs
+	    if (ok)
+	      {
+		rvalue = coercion_site (rvalue, actual, expected, lvalue_locus,
+					rvalue_locus);
+	      }
+
+	    // add it to the list
+	    arguments.push_back (rvalue);
+	  }
+      }
 
     // the constructor depends on whether this is actually an enum or not if
     // its an enum we need to setup the discriminator


                 reply	other threads:[~2022-06-08 12:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220608124434.315B938936C8@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).