Skip to content

Commit 7ac9a76

Browse files
committed
Make TypeBoundPredicate a subclass of the SubstitutionRef
This will allow us to reuse our generic substitions code to manage generic traits and their substitions better. It will unify the handling in one path so we get the same error handling.
1 parent 56a1571 commit 7ac9a76

File tree

7 files changed

+75
-19
lines changed

7 files changed

+75
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ class TraitReference
407407

408408
bool trait_has_generics () const { return !trait_substs.empty (); }
409409

410-
const std::vector<TyTy::SubstitutionParamMapping> &get_trait_substs () const
410+
std::vector<TyTy::SubstitutionParamMapping> get_trait_substs () const
411411
{
412412
return trait_substs;
413413
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,13 @@ class TraitResolver : public TypeCheckBase
150150

151151
// They also inherit themselves as a bound this enables a trait item to
152152
// reference other Self::trait_items
153+
std::vector<TyTy::SubstitutionParamMapping> self_subst_copy;
154+
for (auto &sub : substitutions)
155+
self_subst_copy.push_back (sub.clone ());
156+
153157
specified_bounds.push_back (
154158
TyTy::TypeBoundPredicate (trait_reference->get_mappings ().get_defid (),
159+
std::move (self_subst_copy),
155160
trait_reference->get_locus ()));
156161

157162
std::vector<const TraitReference *> super_traits;
@@ -168,8 +173,8 @@ class TraitResolver : public TypeCheckBase
168173
// FIXME this might be recursive we need a check for that
169174

170175
TraitReference *trait = resolve_trait_path (b->get_path ());
171-
TyTy::TypeBoundPredicate predicate (
172-
trait->get_mappings ().get_defid (), bound->get_locus ());
176+
TyTy::TypeBoundPredicate predicate (*trait,
177+
bound->get_locus ());
173178

174179
specified_bounds.push_back (std::move (predicate));
175180
super_traits.push_back (predicate.get ());

gcc/rust/typecheck/rust-hir-type-check-item.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ class TypeCheckItem : public TypeCheckBase
7979
rust_assert (!trait_reference->is_error ());
8080

8181
// setup the bound
82-
TyTy::TypeBoundPredicate predicate (
83-
trait_reference->get_mappings ().get_defid (), ref->get_locus ());
82+
TyTy::TypeBoundPredicate predicate (*trait_reference,
83+
ref->get_locus ());
8484
auto &final_seg = ref->get_final_segment ();
8585
if (final_seg->is_generic_segment ())
8686
{

gcc/rust/typecheck/rust-hir-type-check-type.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,7 @@ TypeCheckType::visit (HIR::TraitObjectType &type)
550550

551551
auto &type_path = trait_bound.get_path ();
552552
TraitReference *trait = resolve_trait_path (type_path);
553-
TyTy::TypeBoundPredicate predicate (trait->get_mappings ().get_defid (),
554-
trait_bound.get_locus ());
553+
TyTy::TypeBoundPredicate predicate (*trait, trait_bound.get_locus ());
555554
auto &final_seg = type_path.get_final_segment ();
556555
if (final_seg->is_generic_segment ())
557556
{

gcc/rust/typecheck/rust-hir-type-check-type.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ class TypeResolveGenericParam : public TypeCheckBase
247247

248248
auto &type_path = b->get_path ();
249249
TraitReference *trait = resolve_trait_path (type_path);
250-
TyTy::TypeBoundPredicate predicate (
251-
trait->get_mappings ().get_defid (), b->get_locus ());
250+
TyTy::TypeBoundPredicate predicate (*trait, b->get_locus ());
252251

253252
auto &final_seg = type_path.get_final_segment ();
254253
if (final_seg->is_generic_segment ())
@@ -318,8 +317,7 @@ class ResolveWhereClauseItem : public TypeCheckBase
318317

319318
auto &type_path = b->get_path ();
320319
TraitReference *trait = resolve_trait_path (type_path);
321-
TyTy::TypeBoundPredicate predicate (
322-
trait->get_mappings ().get_defid (), b->get_locus ());
320+
TyTy::TypeBoundPredicate predicate (*trait, b->get_locus ());
323321

324322
auto &final_seg = type_path.get_final_segment ();
325323
if (final_seg->is_generic_segment ())

gcc/rust/typecheck/rust-tyty-bounds.cc

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,38 @@ TypeCheckBase::resolve_trait_path (HIR::TypePath &path)
6767

6868
namespace TyTy {
6969

70-
TypeBoundPredicate::TypeBoundPredicate (DefId reference, Location locus)
71-
: reference (reference), locus (locus), args (nullptr)
70+
TypeBoundPredicate::TypeBoundPredicate (
71+
const Resolver::TraitReference &trait_reference, Location locus)
72+
: SubstitutionRef (trait_reference.get_trait_substs (),
73+
SubstitutionArgumentMappings::error ()),
74+
reference (trait_reference.get_mappings ().get_defid ()), locus (locus),
75+
args (nullptr), error_flag (false)
7276
{}
7377

78+
TypeBoundPredicate::TypeBoundPredicate (
79+
DefId reference, std::vector<SubstitutionParamMapping> substitutions,
80+
Location locus)
81+
: SubstitutionRef (std::move (substitutions),
82+
SubstitutionArgumentMappings::error ()),
83+
reference (reference), locus (locus), args (nullptr), error_flag (false)
84+
{}
85+
86+
TypeBoundPredicate::TypeBoundPredicate (const TypeBoundPredicate &other)
87+
: SubstitutionRef ({}, other.used_arguments), reference (other.reference),
88+
locus (other.locus), args (other.args), error_flag (other.error_flag)
89+
{
90+
substitutions.clear ();
91+
if (!other.is_error ())
92+
{
93+
for (const auto &p : other.get_substs ())
94+
substitutions.push_back (p.clone ());
95+
}
96+
}
97+
7498
std::string
7599
TypeBoundPredicate::as_string () const
76100
{
77-
return get ()->as_string ()
78-
+ (has_generic_args ()
79-
? std::string ("<") + args->as_string () + std::string (">")
80-
: "");
101+
return get ()->as_string () + subst_as_string ();
81102
}
82103

83104
const Resolver::TraitReference *
@@ -183,6 +204,25 @@ TypeBoundPredicateItem::get_tyty_for_receiver (
183204

184205
return resolved;
185206
}
207+
bool
208+
TypeBoundPredicate::is_error () const
209+
{
210+
auto context = Resolver::TypeCheckContext::get ();
211+
212+
Resolver::TraitReference *ref = nullptr;
213+
bool ok = context->lookup_trait_reference (reference, &ref);
214+
215+
return !ok || error_flag;
216+
}
217+
218+
BaseType *
219+
TypeBoundPredicate::handle_substitions (SubstitutionArgumentMappings mappings)
220+
{
221+
gcc_unreachable ();
222+
return nullptr;
223+
}
224+
225+
// trait item reference
186226

187227
const Resolver::TraitItemReference *
188228
TypeBoundPredicateItem::get_raw_item () const

gcc/rust/typecheck/rust-tyty.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,10 +939,17 @@ class SubstitutionRef
939939
SubstitutionArgumentMappings used_arguments;
940940
};
941941

942-
class TypeBoundPredicate
942+
class TypeBoundPredicate : public SubstitutionRef
943943
{
944944
public:
945-
TypeBoundPredicate (DefId reference, Location locus);
945+
TypeBoundPredicate (const Resolver::TraitReference &trait_reference,
946+
Location locus);
947+
948+
TypeBoundPredicate (DefId reference,
949+
std::vector<SubstitutionParamMapping> substitutions,
950+
Location locus);
951+
952+
TypeBoundPredicate (const TypeBoundPredicate &other);
946953

947954
std::string as_string () const;
948955

@@ -975,10 +982,17 @@ class TypeBoundPredicate
975982
return args->has_generic_args ();
976983
}
977984

985+
// WARNING THIS WILL ALWAYS RETURN NULLPTR
986+
BaseType *
987+
handle_substitions (SubstitutionArgumentMappings mappings) override final;
988+
989+
bool is_error () const;
990+
978991
private:
979992
DefId reference;
980993
Location locus;
981994
HIR::GenericArgs *args;
995+
bool error_flag;
982996
};
983997

984998
// https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.VariantDef.html

0 commit comments

Comments
 (0)