[PR testsuite/116860] Testsuite adjustment for recently added tests
[official-gcc.git] / gcc / rust / typecheck / rust-hir-path-probe.h
blob09a6492596d38160a6c5d576a49dd177e0ca3ee0
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 #ifndef RUST_HIR_PATH_PROBE_H
20 #define RUST_HIR_PATH_PROBE_H
22 #include "rust-hir-type-check-base.h"
23 #include "rust-hir-visitor.h"
24 #include "rust-tyty.h"
26 namespace Rust {
27 namespace Resolver {
29 struct PathProbeCandidate
31 enum CandidateType
33 ERROR,
35 ENUM_VARIANT,
37 IMPL_CONST,
38 IMPL_TYPE_ALIAS,
39 IMPL_FUNC,
41 TRAIT_ITEM_CONST,
42 TRAIT_TYPE_ALIAS,
43 TRAIT_FUNC,
46 struct EnumItemCandidate
48 const TyTy::ADTType *parent;
49 const TyTy::VariantDef *variant;
52 struct ImplItemCandidate
54 HIR::ImplItem *impl_item;
55 HIR::ImplBlock *parent;
58 struct TraitItemCandidate
60 const TraitReference *trait_ref;
61 const TraitItemReference *item_ref;
62 HIR::ImplBlock *impl;
65 CandidateType type;
66 TyTy::BaseType *ty;
67 location_t locus;
68 union Candidate
70 EnumItemCandidate enum_field;
71 ImplItemCandidate impl;
72 TraitItemCandidate trait;
74 Candidate (EnumItemCandidate enum_field);
75 Candidate (ImplItemCandidate impl);
76 Candidate (TraitItemCandidate trait);
77 } item;
79 PathProbeCandidate (CandidateType type, TyTy::BaseType *ty, location_t locus,
80 EnumItemCandidate enum_field);
82 PathProbeCandidate (CandidateType type, TyTy::BaseType *ty, location_t locus,
83 ImplItemCandidate impl);
85 PathProbeCandidate (CandidateType type, TyTy::BaseType *ty, location_t locus,
86 TraitItemCandidate trait);
88 std::string as_string () const;
90 bool is_enum_candidate () const;
92 bool is_impl_candidate () const;
94 bool is_trait_candidate () const;
96 bool is_full_trait_item_candidate () const;
98 static PathProbeCandidate get_error ();
100 bool is_error () const;
102 DefId get_defid () const;
104 bool operator< (const PathProbeCandidate &c) const;
107 class PathProbeType : public TypeCheckBase, public HIR::HIRImplVisitor
109 public:
110 static std::set<PathProbeCandidate>
111 Probe (const TyTy::BaseType *receiver,
112 const HIR::PathIdentSegment &segment_name, bool probe_impls,
113 bool probe_bounds, bool ignore_mandatory_trait_items,
114 DefId specific_trait_id = UNKNOWN_DEFID);
116 void visit (HIR::TypeAlias &alias) override;
117 void visit (HIR::ConstantItem &constant) override;
118 void visit (HIR::Function &function) override;
120 protected:
121 void process_enum_item_for_candiates (const TyTy::ADTType *adt);
123 void process_impl_items_for_candidates ();
125 void process_impl_item_candidate (HirId id, HIR::ImplItem *item,
126 HIR::ImplBlock *impl);
128 void
129 process_associated_trait_for_candidates (const TraitReference *trait_ref,
130 HIR::ImplBlock *impl,
131 bool ignore_mandatory_trait_items);
133 void
134 process_predicate_for_candidates (const TyTy::TypeBoundPredicate &predicate,
135 bool ignore_mandatory_trait_items);
137 protected:
138 PathProbeType (const TyTy::BaseType *receiver,
139 const HIR::PathIdentSegment &query, DefId specific_trait_id);
141 std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>>
142 union_bounds (
143 const std::vector<std::pair</*const*/ TraitReference *, HIR::ImplBlock *>>
145 const std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>> b)
146 const;
148 bool is_reciever_generic () const;
150 const TyTy::BaseType *receiver;
151 const HIR::PathIdentSegment &search;
152 std::set<PathProbeCandidate> candidates;
153 HIR::ImplBlock *current_impl;
154 DefId specific_trait_id;
157 class ReportMultipleCandidateError : private TypeCheckBase
159 public:
160 static void Report (std::set<PathProbeCandidate> &candidates,
161 const HIR::PathIdentSegment &query,
162 location_t query_locus)
164 rich_location r (line_table, query_locus);
165 for (auto &c : candidates)
166 r.add_range (c.locus);
168 std::string rich_msg = "multiple " + query.as_string () + " found";
169 r.add_fixit_replace (rich_msg.c_str ());
171 rust_error_at (r, ErrorCode::E0034,
172 "multiple applicable items in scope for: %qs",
173 query.as_string ().c_str ());
177 class PathProbeImplTrait : public PathProbeType
179 public:
180 static std::set<PathProbeCandidate>
181 Probe (const TyTy::BaseType *receiver,
182 const HIR::PathIdentSegment &segment_name,
183 const TraitReference *trait_reference);
185 private:
186 PathProbeImplTrait (const TyTy::BaseType *receiver,
187 const HIR::PathIdentSegment &query,
188 const TraitReference *trait_reference);
190 void process_trait_impl_items_for_candidates ();
192 const TraitReference *trait_reference;
195 } // namespace Resolver
196 } // namespace Rust
198 #endif // RUST_HIR_PATH_PROBE_H