https://gcc.gnu.org/g:7aa86077d8cfae5413ccbfc0e8f7862d93294ac0
commit r17-4301-g7aa86077d8cfae5413ccbfc0e8f7862d93294ac0 Author: Philip Herron <[email protected]> Date: Fri Aug 28 17:45:55 2026 +0100 gccrs: refactor out pathprobeimpltrait into its own class gcc/rust/ChangeLog: * Make-lang.in: new object * backend/rust-compile-base.cc: refactor * backend/rust-compile-drop.cc: likewise * backend/rust-compile-resolve-path.cc: likewise * typecheck/rust-hir-path-probe.cc (PathProbeImplTrait::PathProbeImplTrait): likewise (PathProbeImplTrait::Probe): likewise (PathProbeImplTrait::process_trait_impl_items_for_candidates): likewise * typecheck/rust-hir-path-probe.h (class PathProbeImplTrait): likewise * typecheck/rust-hir-path-probe-impl-trait.cc: New file. * typecheck/rust-hir-path-probe-impl-trait.h: New file. Signed-off-by: Philip Herron <[email protected]> Diff: --- gcc/rust/Make-lang.in | 1 + gcc/rust/backend/rust-compile-base.cc | 2 +- gcc/rust/backend/rust-compile-drop.cc | 2 +- gcc/rust/backend/rust-compile-resolve-path.cc | 2 +- .../typecheck/rust-hir-path-probe-impl-trait.cc | 121 +++++++++++++++++++++ .../typecheck/rust-hir-path-probe-impl-trait.h | 58 ++++++++++ gcc/rust/typecheck/rust-hir-path-probe.cc | 33 ------ gcc/rust/typecheck/rust-hir-path-probe.h | 17 --- 8 files changed, 183 insertions(+), 53 deletions(-) diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index 3f79477b79c0..91caebbee85e 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -187,6 +187,7 @@ GRS_OBJS = \ rust/rust-polonius.o\ rust/rust-hir-dot-operator.o \ rust/rust-hir-path-probe.o \ + rust/rust-hir-path-probe-impl-trait.o \ rust/rust-hir-path-probe-type.o \ rust/rust-hir-path.o \ rust/rust-hir-type.o \ diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 3c7cb71d283d..d4161a96f1f2 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -30,7 +30,7 @@ #include "rust-expr.h" // for AST::AttrInputLiteral #include "rust-hir-map.h" #include "rust-macro.h" // for AST::MetaNameValueStr -#include "rust-hir-path-probe.h" +#include "rust-hir-path-probe-impl-trait.h" #include "rust-type-util.h" #include "rust-compile-implitem.h" #include "rust-attribute-values.h" diff --git a/gcc/rust/backend/rust-compile-drop.cc b/gcc/rust/backend/rust-compile-drop.cc index d6bf79c07198..ca4e63f88274 100644 --- a/gcc/rust/backend/rust-compile-drop.cc +++ b/gcc/rust/backend/rust-compile-drop.cc @@ -22,7 +22,7 @@ #include "rust-compile-context.h" #include "rust-compile-implitem.h" #include "rust-bir-drop-analysis.h" -#include "rust-hir-path-probe.h" +#include "rust-hir-path-probe-impl-trait.h" #include "rust-hir-trait-reference.h" #include "rust-hir-type-bounds.h" #include "rust-lang-item.h" diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index d9225718b136..db984bee3393 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -24,7 +24,7 @@ #include "rust-compile-expr.h" #include "rust-hir-map.h" #include "rust-hir-trait-resolve.h" -#include "rust-hir-path-probe.h" +#include "rust-hir-path-probe-impl-trait.h" #include "rust-compile-extern.h" #include "rust-constexpr.h" #include "rust-rib.h" diff --git a/gcc/rust/typecheck/rust-hir-path-probe-impl-trait.cc b/gcc/rust/typecheck/rust-hir-path-probe-impl-trait.cc new file mode 100644 index 000000000000..7b6871fb69d2 --- /dev/null +++ b/gcc/rust/typecheck/rust-hir-path-probe-impl-trait.cc @@ -0,0 +1,121 @@ +// Copyright (C) 2026 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include "rust-hir-path-probe-impl-trait.h" +#include "rust-hir-item.h" +#include "rust-type-util.h" + +namespace Rust { +namespace Resolver { + +PathProbeImplTrait::PathProbeImplTrait (TyTy::BaseType *receiver, + const HIR::PathIdentSegment &query, + const TraitReference *trait_reference) + : TypeCheckBase (), mappings (Analysis::Mappings::get ()), + receiver (receiver), search (query), trait_reference (trait_reference), + current_impl (nullptr) +{} + +std::set<PathProbeCandidate> +PathProbeImplTrait::Probe (TyTy::BaseType *receiver, + const HIR::PathIdentSegment &segment_name, + const TraitReference *trait_reference) +{ + PathProbeImplTrait probe (receiver, segment_name, trait_reference); + probe.process_trait_impl_items_for_candidates (); + return probe.candidates; +} + +void +PathProbeImplTrait::process_trait_impl_items_for_candidates () +{ + NodeId trait_node_id = trait_reference->get_mappings ().get_nodeid (); + mappings.iterate_trait_impl_items (trait_node_id, + [this] (HirId id, HIR::ImplItem *item, + HIR::ImplBlock *impl) -> bool { + process_impl_item_candidate (id, item, + impl); + return true; + }); +} + +void +PathProbeImplTrait::process_impl_item_candidate (HirId id, HIR::ImplItem *item, + HIR::ImplBlock *impl) +{ + current_impl = impl; + HirId impl_ty_id = impl->get_type ().get_mappings ().get_hirid (); + TyTy::BaseType *impl_block_ty = nullptr; + if (!query_type (impl_ty_id, &impl_block_ty)) + return; + + if (!types_compatable (TyTy::TyWithLocation (receiver), + TyTy::TyWithLocation (impl_block_ty), + impl->get_locus (), false)) + return; + + item->accept_vis (*this); +} + +void +PathProbeImplTrait::visit (HIR::TypeAlias &alias) +{ + if (search.to_string () != alias.get_new_type_name ().as_string ()) + return; + + TyTy::BaseType *ty = nullptr; + if (!query_type (alias.get_mappings ().get_hirid (), &ty)) + return; + + PathProbeCandidate::ImplItemCandidate item{&alias, current_impl}; + candidates.insert ( + {PathProbeCandidate::IMPL_TYPE_ALIAS, ty, alias.get_locus (), item}); +} + +void +PathProbeImplTrait::visit (HIR::ConstantItem &constant) +{ + if (search.to_string () != constant.get_identifier ().as_string ()) + return; + + TyTy::BaseType *ty = nullptr; + if (!query_type (constant.get_mappings ().get_hirid (), &ty)) + return; + + PathProbeCandidate::ImplItemCandidate item{&constant, current_impl}; + candidates.insert ( + {PathProbeCandidate::IMPL_CONST, ty, constant.get_locus (), item}); +} + +void +PathProbeImplTrait::visit (HIR::Function &function) +{ + if (search.to_string () != function.get_function_name ().as_string ()) + return; + + TyTy::BaseType *ty = nullptr; + if (!query_type (function.get_mappings ().get_hirid (), &ty)) + return; + + PathProbeCandidate::ImplItemCandidate item{&function, current_impl}; + candidates.insert ( + {PathProbeCandidate::IMPL_FUNC, ty, function.get_locus (), item}); +} + +} // namespace Resolver +} // namespace Rust diff --git a/gcc/rust/typecheck/rust-hir-path-probe-impl-trait.h b/gcc/rust/typecheck/rust-hir-path-probe-impl-trait.h new file mode 100644 index 000000000000..d61019748230 --- /dev/null +++ b/gcc/rust/typecheck/rust-hir-path-probe-impl-trait.h @@ -0,0 +1,58 @@ +// Copyright (C) 2026 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#ifndef RUST_HIR_PATH_PROBE_IMPL_TRAIT_H +#define RUST_HIR_PATH_PROBE_IMPL_TRAIT_H + +#include "rust-hir-path-probe.h" + +namespace Rust { +namespace Resolver { + +class PathProbeImplTrait : public TypeCheckBase, public HIR::HIRImplVisitor +{ +public: + static std::set<PathProbeCandidate> + Probe (TyTy::BaseType *receiver, const HIR::PathIdentSegment &segment_name, + const TraitReference *trait_reference); + + void visit (HIR::TypeAlias &alias) override; + void visit (HIR::ConstantItem &constant) override; + void visit (HIR::Function &function) override; + +private: + PathProbeImplTrait (TyTy::BaseType *receiver, + const HIR::PathIdentSegment &query, + const TraitReference *trait_reference); + + void process_trait_impl_items_for_candidates (); + void process_impl_item_candidate (HirId id, HIR::ImplItem *item, + HIR::ImplBlock *impl); + + Analysis::Mappings &mappings; + TyTy::BaseType *receiver; + const HIR::PathIdentSegment &search; + const TraitReference *trait_reference; + std::set<PathProbeCandidate> candidates; + HIR::ImplBlock *current_impl; +}; + +} // namespace Resolver +} // namespace Rust + +#endif // RUST_HIR_PATH_PROBE_IMPL_TRAIT_H diff --git a/gcc/rust/typecheck/rust-hir-path-probe.cc b/gcc/rust/typecheck/rust-hir-path-probe.cc index a6c378f25644..2a6f93797447 100644 --- a/gcc/rust/typecheck/rust-hir-path-probe.cc +++ b/gcc/rust/typecheck/rust-hir-path-probe.cc @@ -472,38 +472,5 @@ PathProbeType::is_receiver_generic () const return receiver_is_type_param || receiver_is_dyn; } -// PathProbImplTrait - -PathProbeImplTrait::PathProbeImplTrait (TyTy::BaseType *receiver, - const HIR::PathIdentSegment &query, - const TraitReference *trait_reference) - : PathProbeType (receiver, query, UNKNOWN_DEFID), - trait_reference (trait_reference) -{} - -std::set<PathProbeCandidate> -PathProbeImplTrait::Probe (TyTy::BaseType *receiver, - const HIR::PathIdentSegment &segment_name, - const TraitReference *trait_reference) -{ - PathProbeImplTrait probe (receiver, segment_name, trait_reference); - // iterate all impls for this trait and receiver - // then search for possible candidates using base class behaviours - probe.process_trait_impl_items_for_candidates (); - return probe.candidates; -} - -void -PathProbeImplTrait::process_trait_impl_items_for_candidates () -{ - NodeId trait_node_id = trait_reference->get_mappings ().get_nodeid (); - mappings.iterate_trait_impl_items ( - trait_node_id, - [&] (HirId id, HIR::ImplItem *item, HIR::ImplBlock *impl) mutable -> bool { - process_impl_item_candidate (id, item, impl); - return true; - }); -} - } // namespace Resolver } // namespace Rust diff --git a/gcc/rust/typecheck/rust-hir-path-probe.h b/gcc/rust/typecheck/rust-hir-path-probe.h index ad83ddf95289..d9800e1c696f 100644 --- a/gcc/rust/typecheck/rust-hir-path-probe.h +++ b/gcc/rust/typecheck/rust-hir-path-probe.h @@ -173,23 +173,6 @@ public: } }; -class PathProbeImplTrait : public PathProbeType -{ -public: - static std::set<PathProbeCandidate> - Probe (TyTy::BaseType *receiver, const HIR::PathIdentSegment &segment_name, - const TraitReference *trait_reference); - -private: - PathProbeImplTrait (TyTy::BaseType *receiver, - const HIR::PathIdentSegment &query, - const TraitReference *trait_reference); - - void process_trait_impl_items_for_candidates (); - - const TraitReference *trait_reference; -}; - } // namespace Resolver } // namespace Rust
