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

When resolving type bounds, we need to examine super traits to properly
determine if type bindings are valid in the current context.

gcc/rust/ChangeLog:

        * typecheck/rust-tyty-bounds.cc: Check super traits for type bindings.
        * typecheck/rust-tyty.h: Add helper methods for bound checking.
---
 gcc/rust/typecheck/rust-tyty-bounds.cc | 32 ++++++++++++++++++++------
 gcc/rust/typecheck/rust-tyty.h         |  3 +++
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc 
b/gcc/rust/typecheck/rust-tyty-bounds.cc
index a36f7712dac..f5b18004c18 100644
--- a/gcc/rust/typecheck/rust-tyty-bounds.cc
+++ b/gcc/rust/typecheck/rust-tyty-bounds.cc
@@ -754,16 +754,34 @@ size_t
 TypeBoundPredicate::get_num_associated_bindings () const
 {
   size_t count = 0;
+
+  get_trait_hierachy ([&count] (const Resolver::TraitReference &ref) {
+    for (const auto &trait_item : ref.get_trait_items ())
+      {
+       bool is_associated_type
+         = trait_item.get_trait_item_type ()
+           == Resolver::TraitItemReference::TraitItemType::TYPE;
+       if (is_associated_type)
+         count++;
+      }
+  });
+
+  return count;
+}
+
+void
+TypeBoundPredicate::get_trait_hierachy (
+  std::function<void (const Resolver::TraitReference &)> callback) const
+{
   auto trait_ref = get ();
-  for (const auto &trait_item : trait_ref->get_trait_items ())
+  callback (*trait_ref);
+
+  for (auto &super : super_traits)
     {
-      bool is_associated_type
-       = trait_item.get_trait_item_type ()
-         == Resolver::TraitItemReference::TraitItemType::TYPE;
-      if (is_associated_type)
-       count++;
+      const auto &super_trait_ref = *super.get ();
+      callback (super_trait_ref);
+      super.get_trait_hierachy (callback);
     }
-  return count;
 }
 
 TypeBoundPredicateItem
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index c759521090d..e8ddd3e1d91 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -593,6 +593,9 @@ private:
 
   TypeBoundPredicate (mark_is_error);
 
+  void get_trait_hierachy (
+    std::function<void (const Resolver::TraitReference &)> callback) const;
+
   DefId reference;
   location_t locus;
   bool error_flag;
-- 
2.49.0

Reply via email to