public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
From: arthur.cohen@embecosm.com
To: gcc-patches@gcc.gnu.org
Cc: gcc-rust@gcc.gnu.org, Philip Herron <philip.herron@embecosm.com>
Subject: [committed 049/103] gccrs: Initial Type resolution for closures
Date: Tue, 21 Feb 2023 13:01:39 +0100	[thread overview]
Message-ID: <20230221120230.596966-50-arthur.cohen@embecosm.com> (raw)
In-Reply-To: <20230221120230.596966-1-arthur.cohen@embecosm.com>

From: Philip Herron <philip.herron@embecosm.com>

gcc/rust/ChangeLog:

	* hir/tree/rust-hir-expr.h: Add `get_params` method.
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Typecheck closure nodes.
	(TypeCheckExpr::resolve_fn_trait_call): New function.
	* typecheck/rust-hir-type-check-expr.h: Declare `resolve_fn_trait_call` and
	`resolve_possible_fn_trait_call_method_name`.
	* typecheck/rust-hir-type-check.h: Declare `get_context_type`.
	* typecheck/rust-tyctx.cc (TypeCheckContextItem::get_context_type): New function.
	* typecheck/rust-tyty-cmp.h: Visit closures properly.
	* typecheck/rust-tyty-rules.h: Likewise.
	* typecheck/rust-tyty.cc (BaseType::bounds_compatible): Add commented out assertin.
	(ClosureType::as_string): Implement it.
	(ClosureType::clone): Fix closure cloning.
	(ClosureType::setup_fn_once_output): New function.
	* typecheck/rust-tyty.h: Improve `ClosureType` class and declare `setup_fn_once_output`.
---
 gcc/rust/hir/tree/rust-hir-expr.h             |   2 +
 .../typecheck/rust-hir-type-check-expr.cc     | 308 +++++++++++++++++-
 gcc/rust/typecheck/rust-hir-type-check-expr.h |   7 +
 gcc/rust/typecheck/rust-hir-type-check.h      |   2 +
 gcc/rust/typecheck/rust-tyctx.cc              |  32 ++
 gcc/rust/typecheck/rust-tyty-cmp.h            |  23 ++
 gcc/rust/typecheck/rust-tyty-rules.h          |  40 ++-
 gcc/rust/typecheck/rust-tyty.cc               |  58 +++-
 gcc/rust/typecheck/rust-tyty.h                |  38 ++-
 9 files changed, 479 insertions(+), 31 deletions(-)

diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h
index 4c5caf17ac3..227bacbe641 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -2131,6 +2131,8 @@ public:
   };
   std::unique_ptr<Expr> &get_expr () { return expr; }
 
+  std::vector<ClosureParam> &get_params () { return params; }
+
   void accept_vis (HIRFullVisitor &vis) override;
   void accept_vis (HIRExpressionVisitor &vis) override;
 
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index 9140b8b1305..85590630378 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -178,16 +178,6 @@ TypeCheckExpr::visit (HIR::CallExpr &expr)
 {
   TyTy::BaseType *function_tyty = TypeCheckExpr::Resolve (expr.get_fnexpr ());
 
-  bool valid_tyty = function_tyty->get_kind () == TyTy::TypeKind::ADT
-		    || function_tyty->get_kind () == TyTy::TypeKind::FNDEF
-		    || function_tyty->get_kind () == TyTy::TypeKind::FNPTR;
-  if (!valid_tyty)
-    {
-      rust_error_at (expr.get_locus (),
-		     "Failed to resolve expression of function call");
-      return;
-    }
-
   rust_debug_loc (expr.get_locus (), "resolved_call_expr to: {%s}",
 		  function_tyty->get_name ().c_str ());
 
@@ -214,6 +204,24 @@ TypeCheckExpr::visit (HIR::CallExpr &expr)
 	  rust_assert (adt->number_of_variants () == 1);
 	  variant = *adt->get_variants ().at (0);
 	}
+
+      infered
+	= TyTy::TypeCheckCallExpr::go (function_tyty, expr, variant, context);
+      return;
+    }
+
+  bool resolved_fn_trait_call
+    = resolve_fn_trait_call (expr, function_tyty, &infered);
+  if (resolved_fn_trait_call)
+    return;
+
+  bool valid_tyty = function_tyty->get_kind () == TyTy::TypeKind::FNDEF
+		    || function_tyty->get_kind () == TyTy::TypeKind::FNPTR;
+  if (!valid_tyty)
+    {
+      rust_error_at (expr.get_locus (),
+		     "Failed to resolve expression of function call");
+      return;
     }
 
   infered = TyTy::TypeCheckCallExpr::go (function_tyty, expr, variant, context);
@@ -1422,7 +1430,123 @@ TypeCheckExpr::visit (HIR::MatchExpr &expr)
 void
 TypeCheckExpr::visit (HIR::ClosureExpr &expr)
 {
-  gcc_unreachable ();
+  TypeCheckContextItem &current_context = context->peek_context ();
+  TyTy::FnType *current_context_fndecl = current_context.get_context_type ();
+
+  HirId ref = expr.get_mappings ().get_hirid ();
+  DefId id = expr.get_mappings ().get_defid ();
+  RustIdent ident{current_context_fndecl->get_ident ().path, expr.get_locus ()};
+
+  // get from parent context
+  std::vector<TyTy::SubstitutionParamMapping> subst_refs
+    = current_context_fndecl->clone_substs ();
+
+  std::vector<TyTy::TyVar> parameter_types;
+  for (auto &p : expr.get_params ())
+    {
+      if (p.has_type_given ())
+	{
+	  TyTy::BaseType *param_tyty
+	    = TypeCheckType::Resolve (p.get_type ().get ());
+	  TyTy::TyVar param_ty (param_tyty->get_ref ());
+	  parameter_types.push_back (param_ty);
+
+	  TypeCheckPattern::Resolve (p.get_pattern ().get (),
+				     param_ty.get_tyty ());
+	}
+      else
+	{
+	  TyTy::TyVar param_ty
+	    = TyTy::TyVar::get_implicit_infer_var (p.get_locus ());
+	  parameter_types.push_back (param_ty);
+
+	  TypeCheckPattern::Resolve (p.get_pattern ().get (),
+				     param_ty.get_tyty ());
+	}
+    }
+
+  // we generate an implicit hirid for the closure args
+  HirId implicit_args_id = mappings->get_next_hir_id ();
+  TyTy::TupleType *closure_args
+    = new TyTy::TupleType (implicit_args_id, expr.get_locus (),
+			   parameter_types);
+  context->insert_implicit_type (closure_args);
+
+  Location result_type_locus = expr.has_return_type ()
+				 ? expr.get_return_type ()->get_locus ()
+				 : expr.get_locus ();
+  TyTy::TyVar result_type
+    = expr.has_return_type ()
+	? TyTy::TyVar (
+	  TypeCheckType::Resolve (expr.get_return_type ().get ())->get_ref ())
+	: TyTy::TyVar::get_implicit_infer_var (expr.get_locus ());
+
+  // resolve the block
+  Location closure_expr_locus = expr.get_expr ()->get_locus ();
+  TyTy::BaseType *closure_expr_ty
+    = TypeCheckExpr::Resolve (expr.get_expr ().get ());
+  coercion_site (expr.get_mappings ().get_hirid (),
+		 TyTy::TyWithLocation (result_type.get_tyty (),
+				       result_type_locus),
+		 TyTy::TyWithLocation (closure_expr_ty, closure_expr_locus),
+		 expr.get_locus ());
+
+  // generate the closure type
+  infered = new TyTy::ClosureType (ref, id, ident, closure_args, result_type,
+				   subst_refs);
+
+  // FIXME
+  // all closures automatically inherit the appropriate fn trait. Lets just
+  // assume FnOnce for now. I think this is based on the return type of the
+  // closure
+
+  Analysis::RustLangItem::ItemType lang_item_type
+    = Analysis::RustLangItem::ItemType::FN_ONCE;
+  DefId respective_lang_item_id = UNKNOWN_DEFID;
+  bool lang_item_defined
+    = mappings->lookup_lang_item (lang_item_type, &respective_lang_item_id);
+  if (!lang_item_defined)
+    {
+      // FIXME
+      // we need to have a unified way or error'ing when we are missing lang
+      // items that is useful
+      rust_fatal_error (
+	expr.get_locus (), "unable to find lang item: %<%s%>",
+	Analysis::RustLangItem::ToString (lang_item_type).c_str ());
+    }
+  rust_assert (lang_item_defined);
+
+  // these lang items are always traits
+  HIR::Item *item = mappings->lookup_defid (respective_lang_item_id);
+  rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
+  HIR::Trait *trait_item = static_cast<HIR::Trait *> (item);
+
+  TraitReference *trait = TraitResolver::Resolve (*trait_item);
+  rust_assert (!trait->is_error ());
+
+  TyTy::TypeBoundPredicate predicate (*trait, expr.get_locus ());
+
+  // resolve the trait bound where the <(Args)> are the parameter tuple type
+  HIR::GenericArgs args = HIR::GenericArgs::create_empty (expr.get_locus ());
+
+  // lets generate an implicit Type so that it resolves to the implict tuple
+  // type we have created
+  auto crate_num = mappings->get_current_crate ();
+  Analysis::NodeMapping mapping (crate_num, expr.get_mappings ().get_nodeid (),
+				 implicit_args_id, UNKNOWN_LOCAL_DEFID);
+  HIR::TupleType *implicit_tuple
+    = new HIR::TupleType (mapping,
+			  {} // we dont need to fill this out because it will
+			     // auto resolve because the hir id's match
+			  ,
+			  expr.get_locus ());
+  args.get_type_args ().push_back (std::unique_ptr<HIR::Type> (implicit_tuple));
+
+  // apply the arguments
+  predicate.apply_generic_arguments (&args);
+
+  // finally inherit the trait bound
+  infered->inherit_bounds ({predicate});
 }
 
 bool
@@ -1630,6 +1754,168 @@ TypeCheckExpr::resolve_operator_overload (
   return true;
 }
 
+HIR::PathIdentSegment
+TypeCheckExpr::resolve_possible_fn_trait_call_method_name (
+  const TyTy::BaseType &receiver)
+{
+  // Question
+  // do we need to probe possible bounds here? I think not, i think when we
+  // support Fn traits they are explicitly specified
+
+  // FIXME
+  // the logic to map the FnTrait to their respective call trait-item is
+  // duplicated over in the backend/rust-compile-expr.cc
+  for (const auto &bound : receiver.get_specified_bounds ())
+    {
+      bool found_fn = bound.get_name ().compare ("Fn") == 0;
+      bool found_fn_mut = bound.get_name ().compare ("FnMut") == 0;
+      bool found_fn_once = bound.get_name ().compare ("FnOnce") == 0;
+
+      if (found_fn)
+	{
+	  return HIR::PathIdentSegment ("call");
+	}
+      else if (found_fn_mut)
+	{
+	  return HIR::PathIdentSegment ("call_mut");
+	}
+      else if (found_fn_once)
+	{
+	  return HIR::PathIdentSegment ("call_once");
+	}
+    }
+
+  // nothing
+  return HIR::PathIdentSegment ("");
+}
+
+bool
+TypeCheckExpr::resolve_fn_trait_call (HIR::CallExpr &expr,
+				      TyTy::BaseType *receiver_tyty,
+				      TyTy::BaseType **result)
+{
+  // we turn this into a method call expr
+  HIR::PathIdentSegment method_name
+    = resolve_possible_fn_trait_call_method_name (*receiver_tyty);
+  if (method_name.is_error ())
+    return false;
+
+  auto candidates = MethodResolver::Probe (receiver_tyty, method_name);
+  if (candidates.empty ())
+    return false;
+
+  if (candidates.size () > 1)
+    {
+      RichLocation r (expr.get_locus ());
+      for (auto &c : candidates)
+	r.add_range (c.candidate.locus);
+
+      rust_error_at (
+	r, "multiple candidates found for function trait method call %<%s%>",
+	method_name.as_string ().c_str ());
+      return false;
+    }
+
+  if (receiver_tyty->get_kind () == TyTy::TypeKind::CLOSURE)
+    {
+      const TyTy::ClosureType &closure
+	= static_cast<TyTy::ClosureType &> (*receiver_tyty);
+      closure.setup_fn_once_output ();
+    }
+
+  auto candidate = *candidates.begin ();
+  rust_debug_loc (expr.get_locus (),
+		  "resolved call-expr to fn trait: {%u} {%s}",
+		  candidate.candidate.ty->get_ref (),
+		  candidate.candidate.ty->debug_str ().c_str ());
+
+  // Get the adjusted self
+  Adjuster adj (receiver_tyty);
+  TyTy::BaseType *adjusted_self = adj.adjust_type (candidate.adjustments);
+
+  // store the adjustments for code-generation to know what to do which must be
+  // stored onto the receiver to so as we don't trigger duplicate deref mappings
+  // ICE when an argument is a method call
+  HirId autoderef_mappings_id = expr.get_mappings ().get_hirid ();
+  context->insert_autoderef_mappings (autoderef_mappings_id,
+				      std::move (candidate.adjustments));
+  context->insert_receiver (expr.get_mappings ().get_hirid (), receiver_tyty);
+
+  PathProbeCandidate &resolved_candidate = candidate.candidate;
+  TyTy::BaseType *lookup_tyty = candidate.candidate.ty;
+  NodeId resolved_node_id
+    = resolved_candidate.is_impl_candidate ()
+	? resolved_candidate.item.impl.impl_item->get_impl_mappings ()
+	    .get_nodeid ()
+	: resolved_candidate.item.trait.item_ref->get_mappings ().get_nodeid ();
+
+  if (lookup_tyty->get_kind () != TyTy::TypeKind::FNDEF)
+    {
+      RichLocation r (expr.get_locus ());
+      r.add_range (resolved_candidate.locus);
+      rust_error_at (r, "associated impl item is not a method");
+      return false;
+    }
+
+  TyTy::BaseType *lookup = lookup_tyty;
+  TyTy::FnType *fn = static_cast<TyTy::FnType *> (lookup);
+  if (!fn->is_method ())
+    {
+      RichLocation r (expr.get_locus ());
+      r.add_range (resolved_candidate.locus);
+      rust_error_at (r, "associated function is not a method");
+      return false;
+    }
+
+  // fn traits only support tuple argument passing so we need to implicitly set
+  // this up to get the same type checking we get in the rest of the pipeline
+
+  std::vector<TyTy::TyVar> call_args;
+  for (auto &arg : expr.get_arguments ())
+    {
+      TyTy::BaseType *a = TypeCheckExpr::Resolve (arg.get ());
+      call_args.push_back (TyTy::TyVar (a->get_ref ()));
+    }
+
+  // crate implicit tuple
+  HirId implicit_arg_id = mappings->get_next_hir_id ();
+  Analysis::NodeMapping mapping (mappings->get_current_crate (), UNKNOWN_NODEID,
+				 implicit_arg_id, UNKNOWN_LOCAL_DEFID);
+
+  TyTy::TupleType *tuple
+    = new TyTy::TupleType (implicit_arg_id, expr.get_locus (), call_args);
+  context->insert_implicit_type (implicit_arg_id, tuple);
+
+  std::vector<TyTy::Argument> args;
+  TyTy::Argument a (mapping, tuple,
+		    expr.get_locus () /*FIXME is there a better location*/);
+  args.push_back (std::move (a));
+
+  TyTy::BaseType *function_ret_tyty
+    = TyTy::TypeCheckMethodCallExpr::go (fn, expr.get_mappings (), args,
+					 expr.get_locus (), expr.get_locus (),
+					 adjusted_self, context);
+  if (function_ret_tyty == nullptr
+      || function_ret_tyty->get_kind () == TyTy::TypeKind::ERROR)
+    {
+      rust_error_at (expr.get_locus (),
+		     "failed check fn trait call-expr MethodCallExpr");
+      return false;
+    }
+
+  // store the expected fntype
+  context->insert_operator_overload (expr.get_mappings ().get_hirid (), fn);
+
+  // set up the resolved name on the path
+  resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (),
+				  resolved_node_id);
+
+  // return the result of the function back
+  *result = function_ret_tyty;
+
+  return true;
+}
+
 bool
 TypeCheckExpr::validate_arithmetic_type (
   const TyTy::BaseType *tyty, HIR::ArithmeticOrLogicalExpr::ExprType expr_type)
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index 7f787fa9766..d800549dea2 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -103,6 +103,13 @@ protected:
 			     HIR::OperatorExprMeta expr, TyTy::BaseType *lhs,
 			     TyTy::BaseType *rhs);
 
+  bool resolve_fn_trait_call (HIR::CallExpr &expr,
+			      TyTy::BaseType *function_tyty,
+			      TyTy::BaseType **result);
+
+  HIR::PathIdentSegment
+  resolve_possible_fn_trait_call_method_name (const TyTy::BaseType &receiver);
+
 private:
   TypeCheckExpr ();
 
diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h
index 2b47c6738b5..adec2f91961 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.h
+++ b/gcc/rust/typecheck/rust-hir-type-check.h
@@ -70,6 +70,8 @@ public:
     return item.trait_item;
   }
 
+  TyTy::FnType *get_context_type ();
+
 private:
   union Item
   {
diff --git a/gcc/rust/typecheck/rust-tyctx.cc b/gcc/rust/typecheck/rust-tyctx.cc
index a0b5c5e849e..886842b0623 100644
--- a/gcc/rust/typecheck/rust-tyctx.cc
+++ b/gcc/rust/typecheck/rust-tyctx.cc
@@ -154,5 +154,37 @@ TypeCheckContext::peek_context ()
   return return_type_stack.back ().first;
 }
 
+// TypeCheckContextItem
+
+TyTy::FnType *
+TypeCheckContextItem::get_context_type ()
+{
+  auto &context = *TypeCheckContext::get ();
+
+  HirId reference = UNKNOWN_HIRID;
+  switch (get_type ())
+    {
+    case ITEM:
+      reference = get_item ()->get_mappings ().get_hirid ();
+      break;
+
+    case IMPL_ITEM:
+      reference = get_impl_item ().second->get_mappings ().get_hirid ();
+      break;
+
+    case TRAIT_ITEM:
+      reference = get_trait_item ()->get_mappings ().get_hirid ();
+      break;
+    }
+
+  rust_assert (reference != UNKNOWN_HIRID);
+
+  TyTy::BaseType *lookup = nullptr;
+  bool ok = context.lookup_type (reference, &lookup);
+  rust_assert (ok);
+  rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF);
+  return static_cast<TyTy::FnType *> (lookup);
+}
+
 } // namespace Resolver
 } // namespace Rust
diff --git a/gcc/rust/typecheck/rust-tyty-cmp.h b/gcc/rust/typecheck/rust-tyty-cmp.h
index 5dfd2d7184a..293c8bfa641 100644
--- a/gcc/rust/typecheck/rust-tyty-cmp.h
+++ b/gcc/rust/typecheck/rust-tyty-cmp.h
@@ -871,6 +871,29 @@ public:
     ok = true;
   }
 
+  void visit (const ClosureType &type) override
+  {
+    if (base->get_def_id () != type.get_def_id ())
+      {
+	BaseCmp::visit (type);
+	return;
+      }
+
+    if (!base->get_parameters ().can_eq (&type.get_parameters (), false))
+      {
+	BaseCmp::visit (type);
+	return;
+      }
+
+    if (!base->get_result_type ().can_eq (&type.get_result_type (), false))
+      {
+	BaseCmp::visit (type);
+	return;
+      }
+
+    ok = true;
+  }
+
 private:
   const BaseType *get_base () const override { return base; }
   const ClosureType *base;
diff --git a/gcc/rust/typecheck/rust-tyty-rules.h b/gcc/rust/typecheck/rust-tyty-rules.h
index 7ffffc1dd59..4b1fe4fd418 100644
--- a/gcc/rust/typecheck/rust-tyty-rules.h
+++ b/gcc/rust/typecheck/rust-tyty-rules.h
@@ -624,7 +624,45 @@ class ClosureRules : public BaseRules
 public:
   ClosureRules (ClosureType *base) : BaseRules (base), base (base) {}
 
-  // TODO
+  void visit (InferType &type) override
+  {
+    if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL)
+      {
+	BaseRules::visit (type);
+	return;
+      }
+
+    resolved = base->clone ();
+    resolved->set_ref (type.get_ref ());
+  }
+
+  void visit (ClosureType &type) override
+  {
+    if (base->get_def_id () != type.get_def_id ())
+      {
+	BaseRules::visit (type);
+	return;
+      }
+
+    TyTy::BaseType *args_res
+      = base->get_parameters ().unify (&type.get_parameters ());
+    if (args_res == nullptr || args_res->get_kind () == TypeKind::ERROR)
+      {
+	BaseRules::visit (type);
+	return;
+      }
+
+    TyTy::BaseType *res
+      = base->get_result_type ().unify (&type.get_result_type ());
+    if (res == nullptr || res->get_kind () == TypeKind::ERROR)
+      {
+	BaseRules::visit (type);
+	return;
+      }
+
+    resolved = base->clone ();
+    resolved->set_ref (type.get_ref ());
+  }
 
 private:
   BaseType *get_base () override { return base; }
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index 462c5bf91fd..0d96c0f04fd 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -27,6 +27,7 @@
 #include "rust-substitution-mapper.h"
 #include "rust-hir-trait-ref.h"
 #include "rust-hir-type-bounds.h"
+#include "rust-hir-trait-resolve.h"
 #include "options.h"
 
 namespace Rust {
@@ -200,6 +201,7 @@ BaseType::bounds_compatible (const BaseType &other, Location locus,
 	  rust_error_at (r,
 			 "bounds not satisfied for %s %<%s%> is not satisfied",
 			 other.get_name ().c_str (), missing_preds.c_str ());
+	  // rust_assert (!emit_error);
 	}
     }
 
@@ -1672,7 +1674,9 @@ ClosureType::accept_vis (TyConstVisitor &vis) const
 std::string
 ClosureType::as_string () const
 {
-  return "TODO";
+  std::string params_buf = parameters->as_string ();
+  return "|" + params_buf + "| {" + result_type.get_tyty ()->as_string ()
+	 + "} {" + raw_bounds_as_string () + "}";
 }
 
 BaseType *
@@ -1699,8 +1703,10 @@ ClosureType::is_equal (const BaseType &other) const
 BaseType *
 ClosureType::clone () const
 {
-  return new ClosureType (get_ref (), get_ty_ref (), ident, id, parameter_types,
-			  result_type, clone_substs (), get_combined_refs ());
+  return new ClosureType (get_ref (), get_ty_ref (), ident, id,
+			  (TyTy::TupleType *) parameters->clone (), result_type,
+			  clone_substs (), get_combined_refs (),
+			  specified_bounds);
 }
 
 BaseType *
@@ -1716,6 +1722,52 @@ ClosureType::handle_substitions (SubstitutionArgumentMappings mappings)
   return nullptr;
 }
 
+void
+ClosureType::setup_fn_once_output () const
+{
+  // lookup the lang items
+  auto fn_once_lang_item = Analysis::RustLangItem::ItemType::FN_ONCE;
+  auto fn_once_output_lang_item
+    = Analysis::RustLangItem::ItemType::FN_ONCE_OUTPUT;
+
+  DefId trait_id = UNKNOWN_DEFID;
+  bool trait_lang_item_defined
+    = mappings->lookup_lang_item (fn_once_lang_item, &trait_id);
+  rust_assert (trait_lang_item_defined);
+
+  DefId trait_item_id = UNKNOWN_DEFID;
+  bool trait_item_lang_item_defined
+    = mappings->lookup_lang_item (fn_once_output_lang_item, &trait_item_id);
+  rust_assert (trait_item_lang_item_defined);
+
+  // resolve to the trait
+  HIR::Item *item = mappings->lookup_defid (trait_id);
+  rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
+  HIR::Trait *trait = static_cast<HIR::Trait *> (item);
+
+  Resolver::TraitReference *trait_ref
+    = Resolver::TraitResolver::Resolve (*trait);
+  rust_assert (!trait_ref->is_error ());
+
+  // resolve to trait item
+  HIR::TraitItem *trait_item
+    = mappings->lookup_trait_item_defid (trait_item_id);
+  rust_assert (trait_item != nullptr);
+  rust_assert (trait_item->get_item_kind ()
+	       == HIR::TraitItem::TraitItemKind::TYPE);
+  std::string item_identifier = trait_item->trait_identifier ();
+
+  // setup associated types  #[lang = "fn_once_output"]
+  Resolver::TraitItemReference *item_reference = nullptr;
+  bool found = trait_ref->lookup_trait_item_by_type (
+    item_identifier, Resolver::TraitItemReference::TraitItemType::TYPE,
+    &item_reference);
+  rust_assert (found);
+
+  // setup
+  item_reference->associated_type_set (&get_result_type ());
+}
+
 void
 ArrayType::accept_vis (TyVisitor &vis)
 {
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index b17f01f67ea..5eaec352567 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -1626,31 +1626,35 @@ class ClosureType : public BaseType, public SubstitutionRef
 {
 public:
   ClosureType (HirId ref, DefId id, RustIdent ident,
-	       std::vector<TyVar> parameter_types, TyVar result_type,
+	       TyTy::TupleType *parameters, TyVar result_type,
 	       std::vector<SubstitutionParamMapping> subst_refs,
-	       std::set<HirId> refs = std::set<HirId> ())
+	       std::set<HirId> refs = std::set<HirId> (),
+	       std::vector<TypeBoundPredicate> specified_bounds
+	       = std::vector<TypeBoundPredicate> ())
     : BaseType (ref, ref, TypeKind::CLOSURE, ident, refs),
       SubstitutionRef (std::move (subst_refs),
 		       SubstitutionArgumentMappings::error ()),
-      parameter_types (std::move (parameter_types)),
-      result_type (std::move (result_type)), id (id)
+      parameters (parameters), result_type (std::move (result_type)), id (id)
   {
     LocalDefId local_def_id = id.localDefId;
     rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
+    inherit_bounds (specified_bounds);
   }
 
   ClosureType (HirId ref, HirId ty_ref, RustIdent ident, DefId id,
-	       std::vector<TyVar> parameter_types, TyVar result_type,
+	       TyTy::TupleType *parameters, TyVar result_type,
 	       std::vector<SubstitutionParamMapping> subst_refs,
-	       std::set<HirId> refs = std::set<HirId> ())
+	       std::set<HirId> refs = std::set<HirId> (),
+	       std::vector<TypeBoundPredicate> specified_bounds
+	       = std::vector<TypeBoundPredicate> ())
     : BaseType (ref, ty_ref, TypeKind::CLOSURE, ident, refs),
       SubstitutionRef (std::move (subst_refs),
 		       SubstitutionArgumentMappings::error ()),
-      parameter_types (std::move (parameter_types)),
-      result_type (std::move (result_type)), id (id)
+      parameters (parameters), result_type (std::move (result_type)), id (id)
   {
     LocalDefId local_def_id = id.localDefId;
     rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
+    inherit_bounds (specified_bounds);
   }
 
   void accept_vis (TyVisitor &vis) override;
@@ -1669,13 +1673,8 @@ public:
 
   bool is_concrete () const override final
   {
-    for (auto &param : parameter_types)
-      {
-	auto p = param.get_tyty ();
-	if (!p->is_concrete ())
-	  return false;
-      }
-    return result_type.get_tyty ()->is_concrete ();
+    return parameters->is_concrete ()
+	   && result_type.get_tyty ()->is_concrete ();
   }
 
   bool needs_generic_substitutions () const override final
@@ -1693,8 +1692,15 @@ public:
   ClosureType *
   handle_substitions (SubstitutionArgumentMappings mappings) override final;
 
+  TyTy::TupleType &get_parameters () const { return *parameters; }
+  TyTy::BaseType &get_result_type () const { return *result_type.get_tyty (); }
+
+  DefId get_def_id () const { return id; }
+
+  void setup_fn_once_output () const;
+
 private:
-  std::vector<TyVar> parameter_types;
+  TyTy::TupleType *parameters;
   TyVar result_type;
   DefId id;
 };
-- 
2.39.1


  parent reply	other threads:[~2023-02-21 12:04 UTC|newest]

Thread overview: 107+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-21 12:00 Rust front-end update arthur.cohen
2023-02-21 12:00 ` [committed 001/103] gccrs: Fix missing dead code analysis ICE on local enum definition arthur.cohen
2023-02-21 12:00 ` [committed 002/103] gccrs: visibility: Rename get_public_vis_type -> get_vis_type arthur.cohen
2023-02-21 12:00 ` [committed 003/103] gccrs: dump: Emit visibility when dumping items arthur.cohen
2023-02-21 12:53   ` Update copyright years. (was: [committed 003/103] gccrs: dump: Emit visibility when dumping items) Thomas Schwinge
2023-02-21 12:00 ` [committed 004/103] gccrs: Add catch for recusive type queries arthur.cohen
2023-02-21 12:00 ` [committed 005/103] gccrs: testing: try loop in const function arthur.cohen
2023-02-21 12:00 ` [committed 006/103] gccrs: ast: dump assignment and compound assignment expr arthur.cohen
2023-02-21 12:00 ` [committed 007/103] gccrs: ast: dump If expressions arthur.cohen
2023-02-21 12:00 ` [committed 008/103] gccrs: builtins: Move implementation into source file arthur.cohen
2023-02-21 12:00 ` [committed 009/103] gccrs: Track DefId on ADT variants arthur.cohen
2023-02-21 12:01 ` [committed 010/103] gccrs: Ensure uniqueness on Path probe's arthur.cohen
2023-02-21 12:01 ` [committed 011/103] gccrs: Support looking up super traits for trait items arthur.cohen
2023-02-21 12:01 ` [committed 012/103] gccrs: ast: dump: add emit_generic_params helper arthur.cohen
2023-02-21 12:01 ` [committed 013/103] gccrs: ast: dump: add format_{tuple,struct}_field helpers arthur.cohen
2023-02-21 12:01 ` [committed 014/103] gccrs: ast: dump structs, enums and unions arthur.cohen
2023-02-21 12:01 ` [committed 015/103] gccrs: intrinsics: Add data prefetching intrinsics arthur.cohen
2023-02-21 12:01 ` [committed 016/103] gccrs: fix ICE on missing closing paren arthur.cohen
2023-02-21 12:01 ` [committed 017/103] gccrs: mappings: Add MacroInvocation -> MacroRulesDef mappings arthur.cohen
2023-02-21 12:01 ` [committed 018/103] gccrs: rust-ast-resolve-item: Add note about resolving glob uses arthur.cohen
2023-02-21 12:01 ` [committed 019/103] gccrs: ast: Add accept_vis() method to `GenericArg` arthur.cohen
2023-02-21 12:01 ` [committed 020/103] gccrs: early-name-resolver: Add simple macro name resolution arthur.cohen
2023-02-21 12:01 ` [committed 021/103] gccrs: Support type resolution on super traits on dyn objects arthur.cohen
2023-02-21 12:01 ` [committed 022/103] gccrs: Add mappings for fn_once lang item arthur.cohen
2023-02-21 12:01 ` [committed 023/103] gccrs: Add ABI mappings for rust-call to map to ABI::RUST arthur.cohen
2023-02-21 12:01 ` [committed 024/103] gccrs: Method resolution must support multiple candidates arthur.cohen
2023-02-21 12:01 ` [committed 025/103] gccrs: ast: dump: fix extra newline in block without tail arthur.cohen
2023-02-21 12:01 ` [committed 026/103] gccrs: ast: dump: minor fixups to IfExpr formatting arthur.cohen
2023-02-21 12:01 ` [committed 027/103] gccrs: ast: dump: ComparisonExpr and LazyBooleanExpr arthur.cohen
2023-02-21 12:01 ` [committed 028/103] gccrs: ast: dump: ArrayExpr arthur.cohen
2023-02-21 12:01 ` [committed 029/103] gccrs: ast: dump: various simple Exprs arthur.cohen
2023-02-21 12:01 ` [committed 030/103] gccrs: ast: dump: RangeExprs arthur.cohen
2023-02-21 12:01 ` [committed 031/103] gccrs: Refactor TraitResolver to not require a visitor arthur.cohen
2023-02-21 12:01 ` [committed 032/103] gccrs: ast: dump TypeAlias arthur.cohen
2023-02-21 12:01 ` [committed 033/103] gccrs: Support outer attribute handling on trait items just like normal items arthur.cohen
2023-02-21 12:01 ` [committed 034/103] gccrs: dump: Emit visibility when dumping items arthur.cohen
2023-02-23  1:01   ` Gerald Pfeifer
2023-02-23 10:53     ` Arthur Cohen
2023-02-21 12:01 ` [committed 035/103] gccrs: dump: Dump items within modules arthur.cohen
2023-02-21 12:01 ` [committed 036/103] gccrs: dump: Fix module dumping arthur.cohen
2023-02-21 12:01 ` [committed 037/103] gccrs: ast: Module: unloaded module and inner attributes arthur.cohen
2023-02-21 12:01 ` [committed 038/103] gccrs: dump: Dump macro rules definition arthur.cohen
2023-02-21 12:01 ` [committed 039/103] gccrs: Add check for recursive trait cycles arthur.cohen
2023-02-21 12:01 ` [committed 040/103] gccrs: ast: Refactor ASTFragment -> Fragment class arthur.cohen
2023-02-21 12:01 ` [committed 041/103] gccrs: rust: Replace uses of ASTFragment -> Fragment arthur.cohen
2023-02-21 12:01 ` [committed 042/103] gccrs: ast: Improve Fragment API arthur.cohen
2023-02-21 12:01 ` [committed 043/103] gccrs: Add missing fn_once_output langitem arthur.cohen
2023-02-21 12:01 ` [committed 044/103] gccrs: Refactor expression hir lowering into cc file arthur.cohen
2023-02-21 12:01 ` [committed 045/103] gccrs: Formatting cleanup in HIR lowering pattern arthur.cohen
2023-02-21 12:01 ` [committed 046/103] gccrs: Add name resolution for closures arthur.cohen
2023-02-21 12:01 ` [committed 047/103] gccrs: Refactor method call type checking arthur.cohen
2023-02-21 12:01 ` [committed 048/103] gccrs: Add closures to lints and error checking arthur.cohen
2023-02-21 12:01 ` arthur.cohen [this message]
2023-02-21 12:01 ` [committed 050/103] gccrs: Closure support at CallExpr arthur.cohen
2023-02-21 12:01 ` [committed 051/103] gccrs: Add missing name resolution to Function type-path segments arthur.cohen
2023-02-21 12:01 ` [committed 052/103] gccrs: Add missing hir lowering to function " arthur.cohen
2023-02-21 12:01 ` [committed 053/103] gccrs: Add missing type resolution for function type segments arthur.cohen
2023-02-21 12:01 ` [committed 054/103] gccrs: Support Closure calls as generic trait bounds arthur.cohen
2023-02-21 12:01 ` [committed 055/103] gccrs: Implement the inline visitor arthur.cohen
2023-02-21 12:01 ` [committed 056/103] gccrs: rust: Allow gccrs to build on x86_64-apple-darwin with clang/libc++ arthur.cohen
2023-02-21 12:01 ` [committed 057/103] gccrs: builtins: Rename all bang macro handlers arthur.cohen
2023-02-21 12:01 ` [committed 058/103] gccrs: intrinsics: Add `sorry_handler` intrinsic handler arthur.cohen
2023-02-21 12:01 ` [committed 059/103] gccrs: constexpr: Add `rust_sorry_at` in places relying on init values arthur.cohen
2023-02-21 12:01 ` [committed 060/103] gccrs: intrinsics: Add early implementation for atomic_store_{seqcst, relaxed, release} arthur.cohen
2023-02-21 12:01 ` [committed 061/103] gccrs: intrinsics: Add unchecked operation intrinsics arthur.cohen
2023-02-21 12:01 ` [committed 062/103] gccrs: intrinsics: Use lambdas for wrapping_<op> intrinsics arthur.cohen
2023-02-21 12:01 ` [committed 063/103] gccrs: intrinsics: Cleanup error handling around atomic_store_* arthur.cohen
2023-02-21 12:01 ` [committed 064/103] gccrs: intrinsics: Implement atomic_load intrinsics arthur.cohen
2023-02-21 12:01 ` [committed 065/103] gccrs: ast: visitor pattern -> overload syntax compatibility layer arthur.cohen
2023-02-21 12:01 ` [committed 066/103] gccrs: ast: transform helper methods to visits and add methods to simplify repeated patterns arthur.cohen
2023-02-21 12:01 ` [committed 067/103] gccrs: ast: refer correctly to arguments in docs-strings arthur.cohen
2023-02-21 12:01 ` [committed 068/103] gccrs: ast: Dump unit struct arthur.cohen
2023-02-21 12:01 ` [committed 069/103] gccrs: add lang item "phantom_data" arthur.cohen
2023-02-21 12:02 ` [committed 070/103] gccrs: add Location to AST::Visibility arthur.cohen
2023-02-21 12:02 ` [committed 071/103] gccrs: typecheck: Fix overzealous `delete` call arthur.cohen
2023-02-21 12:02 ` [committed 072/103] gccrs: ast: add visit overload for references arthur.cohen
2023-02-21 12:02 ` [committed 073/103] gccrs: ast: Dump where clause and recursively needed nodes arthur.cohen
2023-02-21 12:02 ` [committed 074/103] gccrs: ast: Dump slice type arthur.cohen
2023-02-21 12:02 ` [committed 075/103] gccrs: ast: Dump array type arthur.cohen
2023-02-21 12:02 ` [committed 076/103] gccrs: ast: Dump raw pointer type arthur.cohen
2023-02-21 12:02 ` [committed 077/103] gccrs: ast: Dump never type arthur.cohen
2023-02-21 12:02 ` [committed 078/103] gccrs: ast: Dump tuple type arthur.cohen
2023-02-21 12:02 ` [committed 079/103] gccrs: ast: Dump inferred type arthur.cohen
2023-02-21 12:02 ` [committed 080/103] gccrs: ast: Dump bare function type arthur.cohen
2023-02-21 12:02 ` [committed 081/103] gccrs: ast: Dump impl trait type one bound arthur.cohen
2023-02-21 12:02 ` [committed 082/103] gccrs: ast: Dump impl trait type arthur.cohen
2023-02-21 12:02 ` [committed 083/103] gccrs: ast: Dump trait object type arthur.cohen
2023-02-21 12:02 ` [committed 084/103] gccrs: ast: Dump parenthesised type arthur.cohen
2023-02-21 12:02 ` [committed 085/103] gccrs: ast: Dump trait object type one bound arthur.cohen
2023-02-21 12:02 ` [committed 086/103] gccrs: ast: Dump type param type arthur.cohen
2023-02-21 12:02 ` [committed 087/103] gccrs: ast: Dump generic parameters arthur.cohen
2023-02-21 12:02 ` [committed 088/103] gccrs: ast: Remove unused include in rust-ast-dump.cc arthur.cohen
2023-02-21 12:02 ` [committed 089/103] gccrs: ast: Dump remove /* stmp */ comment to not clutter the dump arthur.cohen
2023-02-21 12:02 ` [committed 090/103] gccrs: ast: Dump no comma after self in fn params if it is the last one arthur.cohen
2023-02-21 12:02 ` [committed 091/103] gccrs: Remove default location. Add visibility location to create_* functions arthur.cohen
2023-02-21 12:02 ` [committed 092/103] gccrs: Improve lexer dump arthur.cohen
2023-02-21 12:02 ` [committed 093/103] gccrs: Get rid of make builtin macro arthur.cohen
2023-02-21 12:02 ` [committed 094/103] gccrs: Refactor name resolver to take a Rib::ItemType arthur.cohen
2023-02-21 12:02 ` [committed 095/103] gccrs: Add closure binding's tracking to name resolution arthur.cohen
2023-02-21 12:02 ` [committed 096/103] gccrs: Add capture tracking to the type info for closures arthur.cohen
2023-02-21 12:02 ` [committed 097/103] gccrs: Add initial support for argument capture of closures arthur.cohen
2023-02-21 12:02 ` [committed 098/103] gccrs: Fix undefined behaviour issues on macos arthur.cohen
2023-02-21 12:02 ` [committed 099/103] gccrs: Skip this debug test case which is failing on the latest mac-os devtools and its only for debug info arthur.cohen
2023-02-21 12:02 ` [committed 100/103] gccrs: Cleanup unused parameters to fix the bootstrap build arthur.cohen
2023-02-21 12:02 ` [committed 101/103] gccrs: Repair 'gcc/rust/lang.opt' comment arthur.cohen
2023-02-21 12:02 ` [committed 102/103] gccrs: const evaluator: Remove get_nth_callarg arthur.cohen
2023-02-21 12:02 ` [committed 103/103] gccrs: add math intrinsics arthur.cohen

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=20230221120230.596966-50-arthur.cohen@embecosm.com \
    --to=arthur.cohen@embecosm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gcc-rust@gcc.gnu.org \
    --cc=philip.herron@embecosm.com \
    /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).