]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix bug with bad type bindings not looking at super traits
authorPhilip Herron <herron.philip@googlemail.com>
Sat, 21 Jun 2025 13:58:49 +0000 (14:58 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:50 +0000 (16:36 +0200)
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
gcc/rust/typecheck/rust-tyty.h

index a36f7712dac45c9fa08a1d90300c0bdd81740534..f5b18004c1875ea843e9da7be61674874e42b78e 100644 (file)
@@ -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
index c759521090da7c2581be51134623c82a3bf37307..e8ddd3e1d91db5c2327cf2df76ce2f4f46929393 100644 (file)
@@ -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;