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] Add catch for recusive type queries
Date: Wed,  5 Oct 2022 19:45:19 +0000 (GMT)	[thread overview]
Message-ID: <20221005194519.CD0443858C2C@sourceware.org> (raw)

https://gcc.gnu.org/g:d1069815fa6238712f51a23dfd43d2ae6cd7c5e8

commit d1069815fa6238712f51a23dfd43d2ae6cd7c5e8
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri Sep 30 13:41:09 2022 +0100

    Add catch for recusive type queries
    
    When we have a type query where by generic substitution occurs we can hit
    the case where we need to Probe the bounds of the substited item to
    determine whether the the bounds are compatible this can cause us to
    end up querying the same type recursively.
    
    Fixes #1550

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-base.cc | 10 ++++++++++
 gcc/rust/typecheck/rust-hir-type-check.h       | 12 ++++++++++++
 gcc/rust/typecheck/rust-tyty-bounds.cc         |  5 +++--
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index bd6a2fb3e73..8107ef5a092 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -495,14 +495,20 @@ TypeCheckBase::resolve_generic_params (
 bool
 TypeCheckBase::query_type (HirId reference, TyTy::BaseType **result)
 {
+  if (context->query_in_progress (reference))
+    return false;
+
   if (context->lookup_type (reference, result))
     return true;
 
+  context->insert_query (reference);
+
   HIR::Item *item = mappings->lookup_hir_item (reference);
   if (item != nullptr)
     {
       rust_debug_loc (item->get_locus (), "resolved item {%u} to", reference);
       *result = TypeCheckItem::Resolve (*item);
+      context->query_completed (reference);
       return true;
     }
 
@@ -520,6 +526,7 @@ TypeCheckBase::query_type (HirId reference, TyTy::BaseType **result)
 		      reference);
 
       *result = TypeCheckItem::ResolveImplItem (*impl_block, *impl_item);
+      context->query_completed (reference);
       return true;
     }
 
@@ -530,6 +537,7 @@ TypeCheckBase::query_type (HirId reference, TyTy::BaseType **result)
   if (found_impl_block_type)
     {
       *result = TypeCheckItem::ResolveImplBlockSelf (*impl_block_by_type);
+      context->query_completed (reference);
       return true;
     }
 
@@ -544,6 +552,7 @@ TypeCheckBase::query_type (HirId reference, TyTy::BaseType **result)
       rust_assert (block != nullptr);
 
       *result = TypeCheckTopLevelExternItem::Resolve (extern_item, *block);
+      context->query_completed (reference);
       return true;
     }
 
@@ -551,6 +560,7 @@ TypeCheckBase::query_type (HirId reference, TyTy::BaseType **result)
   Location possible_locus = mappings->lookup_location (reference);
   rust_debug_loc (possible_locus, "query system failed to resolve: [%u]",
 		  reference);
+  context->query_completed (reference);
 
   return false;
 }
diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h
index c17db71aec7..f85585bccf6 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.h
+++ b/gcc/rust/typecheck/rust-hir-type-check.h
@@ -372,6 +372,15 @@ public:
     return true;
   }
 
+  void insert_query (HirId id) { querys_in_progress.insert (id); }
+
+  void query_completed (HirId id) { querys_in_progress.erase (id); }
+
+  bool query_in_progress (HirId id) const
+  {
+    return querys_in_progress.find (id) != querys_in_progress.end ();
+  }
+
 private:
   TypeCheckContext ();
 
@@ -406,6 +415,9 @@ private:
 
   // predicates
   std::map<HirId, TyTy::TypeBoundPredicate> predicates;
+
+  // query context lookups
+  std::set<HirId> querys_in_progress;
 };
 
 class TypeResolution
diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc b/gcc/rust/typecheck/rust-tyty-bounds.cc
index ec4a11fb592..d7647b75f7a 100644
--- a/gcc/rust/typecheck/rust-tyty-bounds.cc
+++ b/gcc/rust/typecheck/rust-tyty-bounds.cc
@@ -34,8 +34,9 @@ TypeBoundsProbe::scan ()
       if (!impl->has_trait_ref ())
 	return true;
 
-      TyTy::BaseType *impl_type = TypeCheckItem::ResolveImplBlockSelf (*impl);
-      if (impl_type->get_kind () == TyTy::TypeKind::ERROR)
+      HirId impl_ty_id = impl->get_type ()->get_mappings ().get_hirid ();
+      TyTy::BaseType *impl_type = nullptr;
+      if (!query_type (impl_ty_id, &impl_type))
 	return true;
 
       if (!receiver->can_eq (impl_type, false))

                 reply	other threads:[~2022-10-05 19:45 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=20221005194519.CD0443858C2C@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).