From 8ddeba50f47ec645eddf03d3fd9824acbf410c0a Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Sat, 21 Jun 2025 14:58:49 +0100 Subject: [PATCH] gccrs: Fix bug with bad type bindings not looking at super traits 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 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 callback) const; + DefId reference; location_t locus; bool error_flag; -- 2.47.2