Skip to content

Commit b5e3892

Browse files
bors[bot]philberty
andauthored
Merge #740
740: Add boiler plate for TyTy::ClosureType r=philberty a=philberty This simply adds in the boilerplate for closure types for type-checking. The closure branch is blocked until we get lang-item support in next. Addresses #195 Co-authored-by: Philip Herron <[email protected]>
2 parents eb29257 + b7bb6a9 commit b5e3892

12 files changed

+329
-0
lines changed

gcc/rust/backend/rust-compile-context.h

+2
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ class TyTyResolveCompile : public TyTy::TyVisitor
661661
ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type);
662662
}
663663

664+
void visit (TyTy::ClosureType &type) override { gcc_unreachable (); }
665+
664666
private:
665667
TyTyResolveCompile (Context *ctx, bool trait_object_mode)
666668
: ctx (ctx), trait_object_mode (trait_object_mode), translated (nullptr)

gcc/rust/backend/rust-compile-tyty.h

+2
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ class TyTyCompile : public TyTy::TyVisitor
239239

240240
void visit (TyTy::DynamicObjectType &) override { gcc_unreachable (); }
241241

242+
void visit (TyTy::ClosureType &) override { gcc_unreachable (); }
243+
242244
private:
243245
TyTyCompile (::Backend *backend)
244246
: backend (backend), translated (nullptr),

gcc/rust/typecheck/rust-hir-const-fold.h

+2
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ class ConstFoldType : public TyTy::TyVisitor
193193

194194
void visit (TyTy::DynamicObjectType &) override { gcc_unreachable (); }
195195

196+
void visit (TyTy::ClosureType &) override { gcc_unreachable (); }
197+
196198
private:
197199
ConstFoldType (::Backend *backend)
198200
: backend (backend), translated (backend->error_type ())

gcc/rust/typecheck/rust-substitution-mapper.h

+19
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class SubstMapper : public TyTy::TyVisitor
138138
void visit (TyTy::StrType &) override { gcc_unreachable (); }
139139
void visit (TyTy::NeverType &) override { gcc_unreachable (); }
140140
void visit (TyTy::DynamicObjectType &) override { gcc_unreachable (); }
141+
void visit (TyTy::ClosureType &) override { gcc_unreachable (); }
141142

142143
private:
143144
SubstMapper (HirId ref, HIR::GenericArgs *generics, Location locus)
@@ -217,6 +218,11 @@ class SubstMapperInternal : public TyTy::TyVisitor
217218
resolved = type.handle_substitions (mappings);
218219
}
219220

221+
void visit (TyTy::ClosureType &type) override
222+
{
223+
resolved = type.handle_substitions (mappings);
224+
}
225+
220226
// nothing to do for these
221227
void visit (TyTy::InferType &) override { gcc_unreachable (); }
222228
void visit (TyTy::FnPtr &) override { gcc_unreachable (); }
@@ -271,6 +277,14 @@ class SubstMapperFromExisting : public TyTy::TyVisitor
271277
resolved = to_sub->handle_substitions (type.get_substitution_arguments ());
272278
}
273279

280+
void visit (TyTy::ClosureType &type) override
281+
{
282+
rust_assert (type.was_substituted ());
283+
284+
TyTy::ClosureType *to_sub = static_cast<TyTy::ClosureType *> (receiver);
285+
resolved = to_sub->handle_substitions (type.get_substitution_arguments ());
286+
}
287+
274288
void visit (TyTy::InferType &) override { gcc_unreachable (); }
275289
void visit (TyTy::TupleType &) override { gcc_unreachable (); }
276290
void visit (TyTy::FnPtr &) override { gcc_unreachable (); }
@@ -323,6 +337,11 @@ class GetUsedSubstArgs : public TyTy::TyVisitor
323337
args = type.get_substitution_arguments ();
324338
}
325339

340+
void visit (TyTy::ClosureType &type) override
341+
{
342+
args = type.get_substitution_arguments ();
343+
}
344+
326345
void visit (TyTy::InferType &) override {}
327346
void visit (TyTy::TupleType &) override {}
328347
void visit (TyTy::FnPtr &) override {}

gcc/rust/typecheck/rust-tyty-call.h

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class TypeCheckCallExpr : private TyVisitor
6565
// call fns
6666
void visit (FnType &type) override;
6767
void visit (FnPtr &type) override;
68+
void visit (ClosureType &type) override { gcc_unreachable (); }
6869

6970
private:
7071
TypeCheckCallExpr (HIR::CallExpr &c, Resolver::TypeCheckContext *context)
@@ -116,6 +117,7 @@ class TypeCheckMethodCallExpr : private TyVisitor
116117

117118
// call fns
118119
void visit (FnType &type) override;
120+
void visit (ClosureType &type) override { gcc_unreachable (); }
119121

120122
private:
121123
TypeCheckMethodCallExpr (HIR::MethodCallExpr &c,

gcc/rust/typecheck/rust-tyty-cast.h

+39
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,17 @@ class BaseCastRules : public TyVisitor
329329
type.as_string ().c_str ());
330330
}
331331

332+
virtual void visit (ClosureType &type) override
333+
{
334+
Location ref_locus = mappings->lookup_location (type.get_ref ());
335+
Location base_locus = mappings->lookup_location (get_base ()->get_ref ());
336+
RichLocation r (ref_locus);
337+
r.add_range (base_locus);
338+
rust_error_at (r, "invalid cast [%s] to [%s]",
339+
get_base ()->as_string ().c_str (),
340+
type.as_string ().c_str ());
341+
}
342+
332343
protected:
333344
BaseCastRules (BaseType *base)
334345
: mappings (Analysis::Mappings::get ()),
@@ -590,6 +601,19 @@ class InferCastRules : public BaseCastRules
590601
BaseCastRules::visit (type);
591602
}
592603

604+
void visit (ClosureType &type) override
605+
{
606+
bool is_valid
607+
= (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
608+
if (is_valid)
609+
{
610+
resolved = type.clone ();
611+
return;
612+
}
613+
614+
BaseCastRules::visit (type);
615+
}
616+
593617
private:
594618
BaseType *get_base () override { return base; }
595619

@@ -749,6 +773,21 @@ class FnptrCastRules : public BaseCastRules
749773
FnPtr *base;
750774
};
751775

776+
class ClosureCastRules : public BaseCastRules
777+
{
778+
using Rust::TyTy::BaseCastRules::visit;
779+
780+
public:
781+
ClosureCastRules (ClosureType *base) : BaseCastRules (base), base (base) {}
782+
783+
// TODO
784+
785+
private:
786+
BaseType *get_base () override { return base; }
787+
788+
ClosureType *base;
789+
};
790+
752791
class ArrayCastRules : public BaseCastRules
753792
{
754793
using Rust::TyTy::BaseCastRules::visit;

gcc/rust/typecheck/rust-tyty-cmp.h

+43
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,22 @@ class BaseCmp : public TyConstVisitor
391391
}
392392
}
393393

394+
virtual void visit (const ClosureType &type) override
395+
{
396+
ok = false;
397+
if (emit_error_flag)
398+
{
399+
Location ref_locus = mappings->lookup_location (type.get_ref ());
400+
Location base_locus
401+
= mappings->lookup_location (get_base ()->get_ref ());
402+
RichLocation r (ref_locus);
403+
r.add_range (base_locus);
404+
rust_error_at (r, "expected [%s] got [%s]",
405+
get_base ()->as_string ().c_str (),
406+
type.as_string ().c_str ());
407+
}
408+
}
409+
394410
protected:
395411
BaseCmp (const BaseType *base, bool emit_errors)
396412
: mappings (Analysis::Mappings::get ()),
@@ -651,6 +667,19 @@ class InferCmp : public BaseCmp
651667
BaseCmp::visit (type);
652668
}
653669

670+
void visit (const ClosureType &type) override
671+
{
672+
bool is_valid
673+
= (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
674+
if (is_valid)
675+
{
676+
ok = true;
677+
return;
678+
}
679+
680+
BaseCmp::visit (type);
681+
}
682+
654683
private:
655684
const BaseType *get_base () const override { return base; }
656685
const InferType *base;
@@ -792,6 +821,20 @@ class FnptrCmp : public BaseCmp
792821
const FnPtr *base;
793822
};
794823

824+
class ClosureCmp : public BaseCmp
825+
{
826+
using Rust::TyTy::BaseCmp::visit;
827+
828+
public:
829+
ClosureCmp (const ClosureType *base, bool emit_errors)
830+
: BaseCmp (base, emit_errors), base (base)
831+
{}
832+
833+
private:
834+
const BaseType *get_base () const override { return base; }
835+
const ClosureType *base;
836+
};
837+
795838
class ArrayCmp : public BaseCmp
796839
{
797840
using Rust::TyTy::BaseCmp::visit;

gcc/rust/typecheck/rust-tyty-coercion.h

+41
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,17 @@ class BaseCoercionRules : public TyVisitor
343343
type.as_string ().c_str ());
344344
}
345345

346+
virtual void visit (ClosureType &type) override
347+
{
348+
Location ref_locus = mappings->lookup_location (type.get_ref ());
349+
Location base_locus = mappings->lookup_location (get_base ()->get_ref ());
350+
RichLocation r (ref_locus);
351+
r.add_range (base_locus);
352+
rust_error_at (r, "expected [%s] got [%s]",
353+
get_base ()->as_string ().c_str (),
354+
type.as_string ().c_str ());
355+
}
356+
346357
protected:
347358
BaseCoercionRules (BaseType *base)
348359
: mappings (Analysis::Mappings::get ()),
@@ -605,6 +616,19 @@ class InferCoercionRules : public BaseCoercionRules
605616
BaseCoercionRules::visit (type);
606617
}
607618

619+
void visit (ClosureType &type) override
620+
{
621+
bool is_valid
622+
= (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
623+
if (is_valid)
624+
{
625+
resolved = type.clone ();
626+
return;
627+
}
628+
629+
BaseCoercionRules::visit (type);
630+
}
631+
608632
private:
609633
BaseType *get_base () override { return base; }
610634

@@ -764,6 +788,23 @@ class FnptrCoercionRules : public BaseCoercionRules
764788
FnPtr *base;
765789
};
766790

791+
class ClosureCoercionRules : public BaseCoercionRules
792+
{
793+
using Rust::TyTy::BaseCoercionRules::visit;
794+
795+
public:
796+
ClosureCoercionRules (ClosureType *base)
797+
: BaseCoercionRules (base), base (base)
798+
{}
799+
800+
// TODO
801+
802+
private:
803+
BaseType *get_base () override { return base; }
804+
805+
ClosureType *base;
806+
};
807+
767808
class ArrayCoercionRules : public BaseCoercionRules
768809
{
769810
using Rust::TyTy::BaseCoercionRules::visit;

gcc/rust/typecheck/rust-tyty-rules.h

+39
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,17 @@ class BaseRules : public TyVisitor
364364
type.as_string ().c_str ());
365365
}
366366

367+
virtual void visit (ClosureType &type) override
368+
{
369+
Location ref_locus = mappings->lookup_location (type.get_ref ());
370+
Location base_locus = mappings->lookup_location (get_base ()->get_ref ());
371+
RichLocation r (ref_locus);
372+
r.add_range (base_locus);
373+
rust_error_at (r, "expected [%s] got [%s]",
374+
get_base ()->as_string ().c_str (),
375+
type.as_string ().c_str ());
376+
}
377+
367378
protected:
368379
BaseRules (BaseType *base)
369380
: mappings (Analysis::Mappings::get ()),
@@ -625,6 +636,19 @@ class InferRules : public BaseRules
625636
BaseRules::visit (type);
626637
}
627638

639+
void visit (ClosureType &type) override
640+
{
641+
bool is_valid
642+
= (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
643+
if (is_valid)
644+
{
645+
resolved = type.clone ();
646+
return;
647+
}
648+
649+
BaseRules::visit (type);
650+
}
651+
628652
private:
629653
BaseType *get_base () override { return base; }
630654

@@ -784,6 +808,21 @@ class FnptrRules : public BaseRules
784808
FnPtr *base;
785809
};
786810

811+
class ClosureRules : public BaseRules
812+
{
813+
using Rust::TyTy::BaseRules::visit;
814+
815+
public:
816+
ClosureRules (ClosureType *base) : BaseRules (base), base (base) {}
817+
818+
// TODO
819+
820+
private:
821+
BaseType *get_base () override { return base; }
822+
823+
ClosureType *base;
824+
};
825+
787826
class ArrayRules : public BaseRules
788827
{
789828
using Rust::TyTy::BaseRules::visit;

gcc/rust/typecheck/rust-tyty-visitor.h

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TyVisitor
4949
virtual void visit (PlaceholderType &type) = 0;
5050
virtual void visit (ProjectionType &type) = 0;
5151
virtual void visit (DynamicObjectType &type) = 0;
52+
virtual void visit (ClosureType &type) = 0;
5253
};
5354

5455
class TyConstVisitor
@@ -76,6 +77,7 @@ class TyConstVisitor
7677
virtual void visit (const PlaceholderType &type) = 0;
7778
virtual void visit (const ProjectionType &type) = 0;
7879
virtual void visit (const DynamicObjectType &type) = 0;
80+
virtual void visit (const ClosureType &type) = 0;
7981
};
8082

8183
} // namespace TyTy

0 commit comments

Comments
 (0)