Skip to content

Commit

Permalink
Introduce ClosureSubsts rather than just having random fields in the
Browse files Browse the repository at this point in the history
TyClosure variant; thread this through wherever closure substitutions
are expected, which leads to a net simplification. Simplify trans
treatment of closures in particular.
  • Loading branch information
nikomatsakis committed Jul 24, 2015
1 parent 69d62e0 commit 1e2677b
Show file tree
Hide file tree
Showing 30 changed files with 256 additions and 159 deletions.
6 changes: 3 additions & 3 deletions src/librustc/metadata/tyencode.rs
Expand Up @@ -143,10 +143,10 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
enc_substs(w, cx, substs);
mywrite!(w, "]");
}
ty::TyClosure(def, substs, ref tys) => {
ty::TyClosure(def, ref substs) => {
mywrite!(w, "k[{}|", (cx.ds)(def));
enc_substs(w, cx, substs);
for ty in tys {
enc_substs(w, cx, &substs.func_substs);
for ty in &substs.upvar_tys {
enc_ty(w, cx, ty);
}
mywrite!(w, ".");
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/fast_reject.rs
Expand Up @@ -76,7 +76,7 @@ pub fn simplify_type(tcx: &ty::ctxt,
Err(msg) => tcx.sess.fatal(&msg),
}
}
ty::TyClosure(def_id, _, _) => {
ty::TyClosure(def_id, _) => {
Some(ClosureSimplifiedType(def_id))
}
ty::TyTuple(ref tys) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/implicator.rs
Expand Up @@ -28,7 +28,7 @@ use util::nodemap::FnvHashSet;
pub enum Implication<'tcx> {
RegionSubRegion(Option<Ty<'tcx>>, ty::Region, ty::Region),
RegionSubGeneric(Option<Ty<'tcx>>, ty::Region, GenericKind<'tcx>),
RegionSubClosure(Option<Ty<'tcx>>, ty::Region, ast::DefId, &'tcx Substs<'tcx>),
RegionSubClosure(Option<Ty<'tcx>>, ty::Region, ast::DefId, &'tcx ty::ClosureSubsts<'tcx>),
Predicate(ast::DefId, ty::Predicate<'tcx>),
}

Expand Down Expand Up @@ -96,7 +96,7 @@ impl<'a, 'tcx> Implicator<'a, 'tcx> {
// No borrowed content reachable here.
}

ty::TyClosure(def_id, substs, _) => {
ty::TyClosure(def_id, ref substs) => {
// TODO remove RegionSubClosure
let &(r_a, opt_ty) = self.stack.last().unwrap();
self.out.push(Implication::RegionSubClosure(opt_ty, r_a, def_id, substs));
Expand Down
11 changes: 5 additions & 6 deletions src/librustc/middle/infer/mod.rs
Expand Up @@ -1374,17 +1374,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}

pub fn closure_type(&self,
def_id: ast::DefId,
substs: &subst::Substs<'tcx>)
-> ty::ClosureTy<'tcx>
def_id: ast::DefId,
substs: &ty::ClosureSubsts<'tcx>)
-> ty::ClosureTy<'tcx>
{

let closure_ty = self.tables
.borrow()
.closure_tys
.get(&def_id)
.unwrap()
.subst(self.tcx, substs);
.subst(self.tcx, &substs.func_substs);

if self.normalize {
normalize_associated_type(&self.tcx, &closure_ty)
Expand All @@ -1395,7 +1394,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

pub fn closure_upvars(&self,
def_id: ast::DefId,
substs: &Substs<'tcx>)
substs: &ty::ClosureSubsts<'tcx>)
-> Option<Vec<ty::ClosureUpvar<'tcx>>>
{
let result = ty::ctxt::closure_upvars(self, def_id, substs);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/liveness.rs
Expand Up @@ -1493,7 +1493,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
fn fn_ret(&self, id: NodeId) -> ty::PolyFnOutput<'tcx> {
let fn_ty = self.ir.tcx.node_id_to_type(id);
match fn_ty.sty {
ty::TyClosure(closure_def_id, substs, _) =>
ty::TyClosure(closure_def_id, ref substs) =>
self.ir.tcx.closure_type(closure_def_id, substs).sig.output(),
_ => fn_ty.fn_ret()
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/mem_categorization.rs
Expand Up @@ -584,7 +584,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> {
def::DefUpvar(var_id, fn_node_id) => {
let ty = try!(self.node_ty(fn_node_id));
match ty.sty {
ty::TyClosure(closure_id, _, _) => {
ty::TyClosure(closure_id, _) => {
match self.typer.closure_kind(closure_id) {
Some(kind) => {
self.cat_upvar(id, span, var_id, fn_node_id, kind)
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/middle/traits/mod.rs
Expand Up @@ -270,8 +270,7 @@ pub struct VtableImplData<'tcx, N> {
#[derive(Clone, PartialEq, Eq)]
pub struct VtableClosureData<'tcx, N> {
pub closure_def_id: ast::DefId,
pub substs: subst::Substs<'tcx>,
pub upvar_tys: Vec<Ty<'tcx>>,
pub substs: ty::ClosureSubsts<'tcx>,
/// Nested obligations. This can be non-empty if the closure
/// signature contains associated types.
pub nested: Vec<N>
Expand Down Expand Up @@ -550,7 +549,6 @@ impl<'tcx, N> Vtable<'tcx, N> {
closure_def_id: c.closure_def_id,
substs: c.substs,
nested: c.nested.into_iter().map(f).collect(),
upvar_tys: c.upvar_tys,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/traits/project.rs
Expand Up @@ -154,7 +154,7 @@ fn consider_unification_despite_ambiguity<'cx,'tcx>(selcx: &mut SelectionContext
debug!("consider_unification_despite_ambiguity: self_ty.sty={:?}",
self_ty.sty);
match self_ty.sty {
ty::TyClosure(closure_def_id, substs, _) => {
ty::TyClosure(closure_def_id, ref substs) => {
let closure_typer = selcx.closure_typer();
let closure_type = closure_typer.closure_type(closure_def_id, substs);
let ty::Binder((_, ret_type)) =
Expand Down
27 changes: 12 additions & 15 deletions src/librustc/middle/traits/select.rs
Expand Up @@ -201,7 +201,7 @@ enum SelectionCandidate<'tcx> {

/// Implementation of a `Fn`-family trait by one of the
/// anonymous types generated for a `||` expression.
ClosureCandidate(/* closure */ ast::DefId, &'tcx Substs<'tcx>, &'tcx Vec<Ty<'tcx>>),
ClosureCandidate(/* closure */ ast::DefId, &'tcx ty::ClosureSubsts<'tcx>),

/// Implementation of a `Fn`-family trait by one of the anonymous
/// types generated for a fn pointer type (e.g., `fn(int)->int`)
Expand Down Expand Up @@ -348,7 +348,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// lifetimes can appear inside the self-type.
let self_ty = self.infcx.shallow_resolve(*obligation.self_ty().skip_binder());
let (closure_def_id, substs) = match self_ty.sty {
ty::TyClosure(id, ref substs, _) => (id, substs.clone()),
ty::TyClosure(id, ref substs) => (id, substs),
_ => { return; }
};
assert!(!substs.has_escaping_regions());
Expand Down Expand Up @@ -1142,8 +1142,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// touch bound regions, they just capture the in-scope
// type/region parameters
let self_ty = self.infcx.shallow_resolve(*obligation.self_ty().skip_binder());
let (closure_def_id, substs, upvar_tys) = match self_ty.sty {
ty::TyClosure(id, substs, ref upvar_tys) => (id, substs, upvar_tys),
let (closure_def_id, substs) = match self_ty.sty {
ty::TyClosure(id, ref substs) => (id, substs),
ty::TyInfer(ty::TyVar(_)) => {
debug!("assemble_unboxed_closure_candidates: ambiguous self-type");
candidates.ambiguous = true;
Expand All @@ -1161,7 +1161,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
Some(closure_kind) => {
debug!("assemble_unboxed_candidates: closure_kind = {:?}", closure_kind);
if closure_kind.extends(kind) {
candidates.vec.push(ClosureCandidate(closure_def_id, substs, upvar_tys));
candidates.vec.push(ClosureCandidate(closure_def_id, substs));
}
}
None => {
Expand Down Expand Up @@ -1703,7 +1703,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
ty::TyTuple(ref tys) => ok_if(tys.clone()),

ty::TyClosure(def_id, substs, _) => {
ty::TyClosure(def_id, ref substs) => {
// FIXME -- This case is tricky. In the case of by-ref
// closures particularly, we need the results of
// inference to decide how to reflect the type of each
Expand Down Expand Up @@ -1865,7 +1865,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
Some(tys.clone())
}

ty::TyClosure(def_id, substs, _) => {
ty::TyClosure(def_id, ref substs) => {
assert_eq!(def_id.krate, ast::LOCAL_CRATE);

// TODO
Expand Down Expand Up @@ -2015,10 +2015,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
Ok(VtableImpl(vtable_impl))
}

ClosureCandidate(closure_def_id, substs, upvar_tys) => {
ClosureCandidate(closure_def_id, substs) => {
let vtable_closure =
try!(self.confirm_closure_candidate(obligation, closure_def_id,
&substs, upvar_tys));
try!(self.confirm_closure_candidate(obligation, closure_def_id, substs));
Ok(VtableClosure(vtable_closure))
}

Expand Down Expand Up @@ -2367,8 +2366,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
fn confirm_closure_candidate(&mut self,
obligation: &TraitObligation<'tcx>,
closure_def_id: ast::DefId,
substs: &Substs<'tcx>,
upvar_tys: &'tcx Vec<Ty<'tcx>>)
substs: &ty::ClosureSubsts<'tcx>)
-> Result<VtableClosureData<'tcx, PredicateObligation<'tcx>>,
SelectionError<'tcx>>
{
Expand All @@ -2394,7 +2392,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
Ok(VtableClosureData {
closure_def_id: closure_def_id,
substs: substs.clone(),
upvar_tys: upvar_tys.clone(),
nested: obligations
})
}
Expand Down Expand Up @@ -2856,7 +2853,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
fn closure_trait_ref_unnormalized(&mut self,
obligation: &TraitObligation<'tcx>,
closure_def_id: ast::DefId,
substs: &Substs<'tcx>)
substs: &ty::ClosureSubsts<'tcx>)
-> ty::PolyTraitRef<'tcx>
{
let closure_type = self.infcx.closure_type(closure_def_id, substs);
Expand All @@ -2878,7 +2875,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
fn closure_trait_ref(&mut self,
obligation: &TraitObligation<'tcx>,
closure_def_id: ast::DefId,
substs: &Substs<'tcx>)
substs: &ty::ClosureSubsts<'tcx>)
-> Normalized<'tcx, ty::PolyTraitRef<'tcx>>
{
let trait_ref = self.closure_trait_ref_unnormalized(
Expand Down

0 comments on commit 1e2677b

Please sign in to comment.