Skip to content

Commit 9411c06

Browse files
committed
Refactor TypeBoundPredicate to be below the definition for SubstitutionRef
This means TypeBoundPredicate will now be able to inherit all behaviours of normal generics so we do not duplicate the work in handling generics it will also allow us to more easily check for unconstrained type parameters on traits.
1 parent 2dfc196 commit 9411c06

File tree

2 files changed

+103
-75
lines changed

2 files changed

+103
-75
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ 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)
72+
{}
73+
7074
std::string
7175
TypeBoundPredicate::as_string () const
7276
{
@@ -192,5 +196,55 @@ TypeBoundPredicateItem::needs_implementation () const
192196
return !get_raw_item ()->is_optional ();
193197
}
194198

199+
// TypeBoundsMappings
200+
201+
TypeBoundsMappings::TypeBoundsMappings (
202+
std::vector<TypeBoundPredicate> specified_bounds)
203+
: specified_bounds (specified_bounds)
204+
{}
205+
206+
std::vector<TypeBoundPredicate> &
207+
TypeBoundsMappings::get_specified_bounds ()
208+
{
209+
return specified_bounds;
210+
}
211+
212+
const std::vector<TypeBoundPredicate> &
213+
TypeBoundsMappings::get_specified_bounds () const
214+
{
215+
return specified_bounds;
216+
}
217+
218+
size_t
219+
TypeBoundsMappings::num_specified_bounds () const
220+
{
221+
return specified_bounds.size ();
222+
}
223+
224+
std::string
225+
TypeBoundsMappings::raw_bounds_as_string () const
226+
{
227+
std::string buf;
228+
for (size_t i = 0; i < specified_bounds.size (); i++)
229+
{
230+
const TypeBoundPredicate &b = specified_bounds.at (i);
231+
bool has_next = (i + 1) < specified_bounds.size ();
232+
buf += b.get_name () + (has_next ? " + " : "");
233+
}
234+
return buf;
235+
}
236+
237+
std::string
238+
TypeBoundsMappings::bounds_as_string () const
239+
{
240+
return "bounds:[" + raw_bounds_as_string () + "]";
241+
}
242+
243+
void
244+
TypeBoundsMappings::add_bound (TypeBoundPredicate predicate)
245+
{
246+
specified_bounds.push_back (predicate);
247+
}
248+
195249
} // namespace TyTy
196250
} // namespace Rust

gcc/rust/typecheck/rust-tyty.h

Lines changed: 49 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -108,92 +108,24 @@ class TypeBoundPredicateItem
108108
const Resolver::TraitItemReference *trait_item_ref;
109109
};
110110

111-
class TypeBoundPredicate
112-
{
113-
public:
114-
TypeBoundPredicate (DefId reference, Location locus)
115-
: reference (reference), locus (locus), args (nullptr)
116-
{}
117-
118-
std::string as_string () const;
119-
120-
const Resolver::TraitReference *get () const;
121-
122-
Location get_locus () const { return locus; }
123-
124-
std::string get_name () const;
125-
126-
// check that this predicate is object-safe see:
127-
// https://doc.rust-lang.org/reference/items/traits.html#object-safety
128-
bool is_object_safe (bool emit_error, Location locus) const;
129-
130-
void apply_generic_arguments (HIR::GenericArgs *generic_args);
131-
132-
bool contains_item (const std::string &search) const;
133-
134-
TypeBoundPredicateItem
135-
lookup_associated_item (const std::string &search) const;
136-
137-
HIR::GenericArgs *get_generic_args () { return args; }
138-
139-
const HIR::GenericArgs *get_generic_args () const { return args; }
140-
141-
bool has_generic_args () const
142-
{
143-
if (args == nullptr)
144-
return false;
145-
146-
return args->has_generic_args ();
147-
}
148-
149-
private:
150-
DefId reference;
151-
Location locus;
152-
HIR::GenericArgs *args;
153-
};
154-
155111
class TypeBoundsMappings
156112
{
157113
protected:
158-
TypeBoundsMappings (std::vector<TypeBoundPredicate> specified_bounds)
159-
: specified_bounds (specified_bounds)
160-
{}
114+
TypeBoundsMappings (std::vector<TypeBoundPredicate> specified_bounds);
161115

162116
public:
163-
std::vector<TypeBoundPredicate> &get_specified_bounds ()
164-
{
165-
return specified_bounds;
166-
}
117+
std::vector<TypeBoundPredicate> &get_specified_bounds ();
167118

168-
const std::vector<TypeBoundPredicate> &get_specified_bounds () const
169-
{
170-
return specified_bounds;
171-
}
119+
const std::vector<TypeBoundPredicate> &get_specified_bounds () const;
172120

173-
size_t num_specified_bounds () const { return specified_bounds.size (); }
121+
size_t num_specified_bounds () const;
174122

175-
std::string raw_bounds_as_string () const
176-
{
177-
std::string buf;
178-
for (size_t i = 0; i < specified_bounds.size (); i++)
179-
{
180-
const TypeBoundPredicate &b = specified_bounds.at (i);
181-
bool has_next = (i + 1) < specified_bounds.size ();
182-
buf += b.get_name () + (has_next ? " + " : "");
183-
}
184-
return buf;
185-
}
123+
std::string raw_bounds_as_string () const;
186124

187-
std::string bounds_as_string () const
188-
{
189-
return "bounds:[" + raw_bounds_as_string () + "]";
190-
}
125+
std::string bounds_as_string () const;
191126

192127
protected:
193-
void add_bound (TypeBoundPredicate predicate)
194-
{
195-
specified_bounds.push_back (predicate);
196-
}
128+
void add_bound (TypeBoundPredicate predicate);
197129

198130
std::vector<TypeBoundPredicate> specified_bounds;
199131
};
@@ -1007,6 +939,48 @@ class SubstitutionRef
1007939
SubstitutionArgumentMappings used_arguments;
1008940
};
1009941

942+
class TypeBoundPredicate
943+
{
944+
public:
945+
TypeBoundPredicate (DefId reference, Location locus);
946+
947+
std::string as_string () const;
948+
949+
const Resolver::TraitReference *get () const;
950+
951+
Location get_locus () const { return locus; }
952+
953+
std::string get_name () const;
954+
955+
// check that this predicate is object-safe see:
956+
// https://doc.rust-lang.org/reference/items/traits.html#object-safety
957+
bool is_object_safe (bool emit_error, Location locus) const;
958+
959+
void apply_generic_arguments (HIR::GenericArgs *generic_args);
960+
961+
bool contains_item (const std::string &search) const;
962+
963+
TypeBoundPredicateItem
964+
lookup_associated_item (const std::string &search) const;
965+
966+
HIR::GenericArgs *get_generic_args () { return args; }
967+
968+
const HIR::GenericArgs *get_generic_args () const { return args; }
969+
970+
bool has_generic_args () const
971+
{
972+
if (args == nullptr)
973+
return false;
974+
975+
return args->has_generic_args ();
976+
}
977+
978+
private:
979+
DefId reference;
980+
Location locus;
981+
HIR::GenericArgs *args;
982+
};
983+
1010984
// https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.VariantDef.html
1011985
class VariantDef
1012986
{

0 commit comments

Comments
 (0)