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] Destructure our generics, placeholers or projections during coercion
Date: Wed,  8 Jun 2022 12:44:54 +0000 (GMT)	[thread overview]
Message-ID: <20220608124454.7B3413895FC5@sourceware.org> (raw)

https://gcc.gnu.org/g:55346cc59b766266202a0ba2114275eb2fc53dda

commit 55346cc59b766266202a0ba2114275eb2fc53dda
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri May 6 12:46:13 2022 +0100

    Destructure our generics, placeholers or projections during coercion
    
    When we coerce types we need to destructure from the generics in order to
    apply these coercion rules correctly or we end up returning a bad
    error_mark_node.

Diff:
---
 gcc/rust/backend/rust-compile-base.h |  4 ++--
 gcc/rust/backend/rust-compile.cc     | 25 ++++++++++++++++---------
 gcc/rust/typecheck/rust-tyty.cc      | 32 ++++++++++++++++++++++++++++++++
 gcc/rust/typecheck/rust-tyty.h       |  5 +++++
 4 files changed, 55 insertions(+), 11 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h
index 77e6cc34b9f..52e0568c88e 100644
--- a/gcc/rust/backend/rust-compile-base.h
+++ b/gcc/rust/backend/rust-compile-base.h
@@ -40,8 +40,8 @@ protected:
 protected:
   Context *get_context () { return ctx; }
 
-  tree coercion_site (tree rvalue, TyTy::BaseType *actual,
-		      TyTy::BaseType *expected, Location lvalue_locus,
+  tree coercion_site (tree rvalue, const TyTy::BaseType *actual,
+		      const TyTy::BaseType *expected, Location lvalue_locus,
 		      Location rvalue_locus);
 
   tree coerce_to_dyn_object (tree compiled_ref, const TyTy::BaseType *actual,
diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc
index 49155d22640..d9349d5a07b 100644
--- a/gcc/rust/backend/rust-compile.cc
+++ b/gcc/rust/backend/rust-compile.cc
@@ -198,21 +198,26 @@ CompileStructExprField::visit (HIR::StructExprFieldIdentifier &field)
 // Shared methods in compilation
 
 tree
-HIRCompileBase::coercion_site (tree rvalue, TyTy::BaseType *actual,
-			       TyTy::BaseType *expected, Location lvalue_locus,
-			       Location rvalue_locus)
+HIRCompileBase::coercion_site (tree rvalue, const TyTy::BaseType *rval,
+			       const TyTy::BaseType *lval,
+			       Location lvalue_locus, Location rvalue_locus)
 {
   if (rvalue == error_mark_node)
     return error_mark_node;
 
+  const TyTy::BaseType *actual = rval->destructure ();
+  const TyTy::BaseType *expected = lval->destructure ();
+
   if (expected->get_kind () == TyTy::TypeKind::REF)
     {
       // bad coercion... of something to a reference
       if (actual->get_kind () != TyTy::TypeKind::REF)
 	return error_mark_node;
 
-      TyTy::ReferenceType *exp = static_cast<TyTy::ReferenceType *> (expected);
-      TyTy::ReferenceType *act = static_cast<TyTy::ReferenceType *> (actual);
+      const TyTy::ReferenceType *exp
+	= static_cast<const TyTy::ReferenceType *> (expected);
+      const TyTy::ReferenceType *act
+	= static_cast<const TyTy::ReferenceType *> (actual);
 
       tree expected_type = TyTyResolveCompile::compile (ctx, act->get_base ());
       tree deref_rvalue
@@ -235,20 +240,22 @@ HIRCompileBase::coercion_site (tree rvalue, TyTy::BaseType *actual,
       if (!valid_coercion)
 	return error_mark_node;
 
-      TyTy::ReferenceType *exp = static_cast<TyTy::ReferenceType *> (expected);
+      const TyTy::ReferenceType *exp
+	= static_cast<const TyTy::ReferenceType *> (expected);
 
       TyTy::BaseType *actual_base = nullptr;
       tree expected_type = error_mark_node;
       if (actual->get_kind () == TyTy::TypeKind::REF)
 	{
-	  TyTy::ReferenceType *act
-	    = static_cast<TyTy::ReferenceType *> (actual);
+	  const TyTy::ReferenceType *act
+	    = static_cast<const TyTy::ReferenceType *> (actual);
 	  actual_base = act->get_base ();
 	  expected_type = TyTyResolveCompile::compile (ctx, act->get_base ());
 	}
       else if (actual->get_kind () == TyTy::TypeKind::POINTER)
 	{
-	  TyTy::PointerType *act = static_cast<TyTy::PointerType *> (actual);
+	  const TyTy::PointerType *act
+	    = static_cast<const TyTy::PointerType *> (actual);
 	  actual_base = act->get_base ();
 	  expected_type = TyTyResolveCompile::compile (ctx, act->get_base ());
 	}
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index fcbf9986d7d..09f5e4c5a36 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -229,6 +229,38 @@ BaseType::get_root () const
   return root;
 }
 
+const BaseType *
+BaseType::destructure () const
+{
+  switch (get_kind ())
+    {
+      case TyTy::TypeKind::PARAM: {
+	const TyTy::ParamType *p = static_cast<const TyTy::ParamType *> (this);
+	return p->resolve ();
+      }
+      break;
+
+      case TyTy::TypeKind::PLACEHOLDER: {
+	const TyTy::PlaceholderType *p
+	  = static_cast<const TyTy::PlaceholderType *> (this);
+	rust_assert (p->can_resolve ());
+	return p->resolve ();
+      }
+      break;
+
+      case TyTy::TypeKind::PROJECTION: {
+	const TyTy::ProjectionType *p
+	  = static_cast<const TyTy::ProjectionType *> (this);
+	return p->get ();
+      }
+
+    default:
+      return this;
+    }
+
+  return this;
+}
+
 TyVar::TyVar (HirId ref) : ref (ref)
 {
   // ensure this reference is defined within the context
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index b00237c1c27..8517b725ea2 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -253,8 +253,13 @@ public:
 		debug_str ().c_str ());
   }
 
+  // FIXME this will eventually go away
   const BaseType *get_root () const;
 
+  // This will get the monomorphized type from Params, Placeholders or
+  // Projections if available or error
+  const BaseType *destructure () const;
+
   const RustIdent &get_ident () const { return ident; }
 
   Location get_locus () const { return ident.locus; }


                 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=20220608124454.7B3413895FC5@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).