Skip to content

Commit

Permalink
Add capture tracking to the type info for closures
Browse files Browse the repository at this point in the history
  • Loading branch information
philberty committed Dec 1, 2022
1 parent 122631c commit 93ddab3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 3 additions & 1 deletion gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1492,8 +1492,10 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr)
expr.get_locus ());

// generate the closure type
NodeId closure_node_id = expr.get_mappings ().get_nodeid ();
const std::set<NodeId> &captures = resolver->get_captures (closure_node_id);
infered = new TyTy::ClosureType (ref, id, ident, closure_args, result_type,
subst_refs);
subst_refs, captures);

// FIXME
// all closures automatically inherit the appropriate fn trait. Lets just
Expand Down
5 changes: 2 additions & 3 deletions gcc/rust/typecheck/rust-tyty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1675,8 +1675,7 @@ std::string
ClosureType::as_string () const
{
std::string params_buf = parameters->as_string ();
return "|" + params_buf + "| {" + result_type.get_tyty ()->as_string ()
+ "} {" + raw_bounds_as_string () + "}";
return "|" + params_buf + "| {" + result_type.get_tyty ()->as_string () + "}";
}

BaseType *
Expand Down Expand Up @@ -1714,7 +1713,7 @@ ClosureType::clone () const
{
return new ClosureType (get_ref (), get_ty_ref (), ident, id,
(TyTy::TupleType *) parameters->clone (), result_type,
clone_substs (), get_combined_refs (),
clone_substs (), captures, get_combined_refs (),
specified_bounds);
}

Expand Down
11 changes: 9 additions & 2 deletions gcc/rust/typecheck/rust-tyty.h
Original file line number Diff line number Diff line change
Expand Up @@ -1628,13 +1628,15 @@ class ClosureType : public BaseType, public SubstitutionRef
ClosureType (HirId ref, DefId id, RustIdent ident,
TyTy::TupleType *parameters, TyVar result_type,
std::vector<SubstitutionParamMapping> subst_refs,
std::set<NodeId> captures,
std::set<HirId> refs = std::set<HirId> (),
std::vector<TypeBoundPredicate> specified_bounds
= std::vector<TypeBoundPredicate> ())
: BaseType (ref, ref, TypeKind::CLOSURE, ident, refs),
SubstitutionRef (std::move (subst_refs),
SubstitutionArgumentMappings::error ()),
parameters (parameters), result_type (std::move (result_type)), id (id)
parameters (parameters), result_type (std::move (result_type)), id (id),
captures (captures)
{
LocalDefId local_def_id = id.localDefId;
rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
Expand All @@ -1644,13 +1646,15 @@ class ClosureType : public BaseType, public SubstitutionRef
ClosureType (HirId ref, HirId ty_ref, RustIdent ident, DefId id,
TyTy::TupleType *parameters, TyVar result_type,
std::vector<SubstitutionParamMapping> subst_refs,
std::set<NodeId> captures,
std::set<HirId> refs = std::set<HirId> (),
std::vector<TypeBoundPredicate> specified_bounds
= std::vector<TypeBoundPredicate> ())
: BaseType (ref, ty_ref, TypeKind::CLOSURE, ident, refs),
SubstitutionRef (std::move (subst_refs),
SubstitutionArgumentMappings::error ()),
parameters (parameters), result_type (std::move (result_type)), id (id)
parameters (parameters), result_type (std::move (result_type)), id (id),
captures (captures)
{
LocalDefId local_def_id = id.localDefId;
rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
Expand Down Expand Up @@ -1699,10 +1703,13 @@ class ClosureType : public BaseType, public SubstitutionRef

void setup_fn_once_output () const;

const std::set<NodeId> &get_captures () const { return captures; }

private:
TyTy::TupleType *parameters;
TyVar result_type;
DefId id;
std::set<NodeId> captures;
};

class ArrayType : public BaseType
Expand Down

0 comments on commit 93ddab3

Please sign in to comment.