Skip to content

Commit 56a1571

Browse files
committed
Keep track of substitution mappings as part of the TraitReference
The TraitReference wrapper is a class that allows us to work with traits in a query manar. This patch allows us to keep track of the substitution mappings that are defined on the trait. This will always be non-empty since traits always contain an implicit Self type parameter so there is special handling in how we perform monomorphization.
1 parent 9411c06 commit 56a1571

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

gcc/rust/typecheck/rust-hir-trait-ref.h

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,27 +181,48 @@ class TraitReference
181181
public:
182182
TraitReference (const HIR::Trait *hir_trait_ref,
183183
std::vector<TraitItemReference> item_refs,
184-
std::vector<const TraitReference *> super_traits)
184+
std::vector<const TraitReference *> super_traits,
185+
std::vector<TyTy::SubstitutionParamMapping> substs)
185186
: hir_trait_ref (hir_trait_ref), item_refs (item_refs),
186187
super_traits (super_traits)
187-
{}
188+
{
189+
trait_substs.clear ();
190+
trait_substs.reserve (substs.size ());
191+
for (const auto &p : substs)
192+
trait_substs.push_back (p.clone ());
193+
}
188194

189195
TraitReference (TraitReference const &other)
190-
: hir_trait_ref (other.hir_trait_ref), item_refs (other.item_refs)
191-
{}
196+
: hir_trait_ref (other.hir_trait_ref), item_refs (other.item_refs),
197+
super_traits (other.super_traits)
198+
{
199+
trait_substs.clear ();
200+
trait_substs.reserve (other.trait_substs.size ());
201+
for (const auto &p : other.trait_substs)
202+
trait_substs.push_back (p.clone ());
203+
}
192204

193205
TraitReference &operator= (TraitReference const &other)
194206
{
195207
hir_trait_ref = other.hir_trait_ref;
196208
item_refs = other.item_refs;
209+
super_traits = other.super_traits;
210+
211+
trait_substs.clear ();
212+
trait_substs.reserve (other.trait_substs.size ());
213+
for (const auto &p : other.trait_substs)
214+
trait_substs.push_back (p.clone ());
197215

198216
return *this;
199217
}
200218

201219
TraitReference (TraitReference &&other) = default;
202220
TraitReference &operator= (TraitReference &&other) = default;
203221

204-
static TraitReference error () { return TraitReference (nullptr, {}, {}); }
222+
static TraitReference error ()
223+
{
224+
return TraitReference (nullptr, {}, {}, {});
225+
}
205226

206227
bool is_error () const { return hir_trait_ref == nullptr; }
207228

@@ -384,10 +405,18 @@ class TraitReference
384405
return is_safe;
385406
}
386407

408+
bool trait_has_generics () const { return !trait_substs.empty (); }
409+
410+
const std::vector<TyTy::SubstitutionParamMapping> &get_trait_substs () const
411+
{
412+
return trait_substs;
413+
}
414+
387415
private:
388416
const HIR::Trait *hir_trait_ref;
389417
std::vector<TraitItemReference> item_refs;
390418
std::vector<const TraitReference *> super_traits;
419+
std::vector<TyTy::SubstitutionParamMapping> trait_substs;
391420
};
392421

393422
class AssociatedImplTrait

gcc/rust/typecheck/rust-hir-trait-resolve.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ class TraitResolver : public TypeCheckBase
193193
}
194194

195195
TraitReference trait_object (trait_reference, item_refs,
196-
std::move (super_traits));
196+
std::move (super_traits),
197+
std::move (substitutions));
197198
context->insert_trait_reference (
198199
trait_reference->get_mappings ().get_defid (), std::move (trait_object));
199200

0 commit comments

Comments
 (0)