1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use super::{check_fn, Expectation, FnCtxt};
use astconv;
use middle::region;
use middle::subst;
use middle::ty::{self, ToPolyTraitRef, Ty};
use std::cmp;
use syntax::abi;
use syntax::ast;
use syntax::ast_util;
use util::ppaux::Repr;
pub fn check_expr_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
expr: &ast::Expr,
_capture: ast::CaptureClause,
decl: &'tcx ast::FnDecl,
body: &'tcx ast::Block,
expected: Expectation<'tcx>) {
debug!("check_expr_closure(expr={},expected={})",
expr.repr(fcx.tcx()),
expected.repr(fcx.tcx()));
let (expected_sig,expected_kind) = match expected.to_option(fcx) {
Some(ty) => deduce_expectations_from_expected_type(fcx, ty),
None => (None, None)
};
check_closure(fcx, expr, expected_kind, decl, body, expected_sig)
}
fn check_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
expr: &ast::Expr,
opt_kind: Option<ty::ClosureKind>,
decl: &'tcx ast::FnDecl,
body: &'tcx ast::Block,
expected_sig: Option<ty::FnSig<'tcx>>) {
let expr_def_id = ast_util::local_def(expr.id);
debug!("check_closure opt_kind={:?} expected_sig={}",
opt_kind,
expected_sig.repr(fcx.tcx()));
let mut fn_ty = astconv::ty_of_closure(
fcx,
ast::Unsafety::Normal,
decl,
abi::RustCall,
expected_sig);
let closure_type = ty::mk_closure(fcx.ccx.tcx,
expr_def_id,
fcx.ccx.tcx.mk_substs(
fcx.inh.param_env.free_substs.clone()));
fcx.write_ty(expr.id, closure_type);
let fn_sig =
ty::liberate_late_bound_regions(fcx.tcx(),
region::DestructionScopeData::new(body.id),
&fn_ty.sig);
check_fn(fcx.ccx,
ast::Unsafety::Normal,
expr.id,
&fn_sig,
decl,
expr.id,
&*body,
fcx.inh);
fn_ty.sig.0.inputs = vec![ty::mk_tup(fcx.tcx(), fn_ty.sig.0.inputs)];
debug!("closure for {} --> sig={} opt_kind={:?}",
expr_def_id.repr(fcx.tcx()),
fn_ty.sig.repr(fcx.tcx()),
opt_kind);
fcx.inh.closure_tys.borrow_mut().insert(expr_def_id, fn_ty);
match opt_kind {
Some(kind) => { fcx.inh.closure_kinds.borrow_mut().insert(expr_def_id, kind); }
None => { }
}
}
fn deduce_expectations_from_expected_type<'a,'tcx>(
fcx: &FnCtxt<'a,'tcx>,
expected_ty: Ty<'tcx>)
-> (Option<ty::FnSig<'tcx>>,Option<ty::ClosureKind>)
{
debug!("deduce_expectations_from_expected_type(expected_ty={})",
expected_ty.repr(fcx.tcx()));
match expected_ty.sty {
ty::ty_trait(ref object_type) => {
let proj_bounds = object_type.projection_bounds_with_self_ty(fcx.tcx(),
fcx.tcx().types.err);
let sig = proj_bounds.iter()
.filter_map(|pb| deduce_sig_from_projection(fcx, pb))
.next();
let kind = fcx.tcx().lang_items.fn_trait_kind(object_type.principal_def_id());
(sig, kind)
}
ty::ty_infer(ty::TyVar(vid)) => {
deduce_expectations_from_obligations(fcx, vid)
}
_ => {
(None, None)
}
}
}
fn deduce_expectations_from_obligations<'a,'tcx>(
fcx: &FnCtxt<'a,'tcx>,
expected_vid: ty::TyVid)
-> (Option<ty::FnSig<'tcx>>, Option<ty::ClosureKind>)
{
let fulfillment_cx = fcx.inh.fulfillment_cx.borrow();
let expected_sig =
fulfillment_cx
.pending_obligations()
.iter()
.filter_map(|obligation| {
debug!("deduce_expectations_from_obligations: obligation.predicate={}",
obligation.predicate.repr(fcx.tcx()));
match obligation.predicate {
ty::Predicate::Projection(ref proj_predicate) => {
let trait_ref = proj_predicate.to_poly_trait_ref();
self_type_matches_expected_vid(fcx, trait_ref, expected_vid)
.and_then(|_| deduce_sig_from_projection(fcx, proj_predicate))
}
_ => {
None
}
}
})
.next();
let expected_kind =
fulfillment_cx
.pending_obligations()
.iter()
.filter_map(|obligation| {
let opt_trait_ref = match obligation.predicate {
ty::Predicate::Projection(ref data) => Some(data.to_poly_trait_ref()),
ty::Predicate::Trait(ref data) => Some(data.to_poly_trait_ref()),
ty::Predicate::Equate(..) => None,
ty::Predicate::RegionOutlives(..) => None,
ty::Predicate::TypeOutlives(..) => None,
};
opt_trait_ref
.and_then(|trait_ref| self_type_matches_expected_vid(fcx, trait_ref, expected_vid))
.and_then(|trait_ref| fcx.tcx().lang_items.fn_trait_kind(trait_ref.def_id()))
})
.fold(None, pick_most_restrictive_closure_kind);
(expected_sig, expected_kind)
}
fn pick_most_restrictive_closure_kind(best: Option<ty::ClosureKind>,
cur: ty::ClosureKind)
-> Option<ty::ClosureKind>
{
match best {
None => Some(cur),
Some(best) => Some(cmp::min(best, cur))
}
}
fn deduce_sig_from_projection<'a,'tcx>(
fcx: &FnCtxt<'a,'tcx>,
projection: &ty::PolyProjectionPredicate<'tcx>)
-> Option<ty::FnSig<'tcx>>
{
let tcx = fcx.tcx();
debug!("deduce_sig_from_projection({})",
projection.repr(tcx));
let trait_ref = projection.to_poly_trait_ref();
if tcx.lang_items.fn_trait_kind(trait_ref.def_id()).is_none() {
return None;
}
let arg_param_ty = *trait_ref.substs().types.get(subst::TypeSpace, 0);
let arg_param_ty = fcx.infcx().resolve_type_vars_if_possible(&arg_param_ty);
debug!("deduce_sig_from_projection: arg_param_ty {}", arg_param_ty.repr(tcx));
let input_tys = match arg_param_ty.sty {
ty::ty_tup(ref tys) => { (*tys).clone() }
_ => { return None; }
};
debug!("deduce_sig_from_projection: input_tys {}", input_tys.repr(tcx));
let ret_param_ty = projection.0.ty;
let ret_param_ty = fcx.infcx().resolve_type_vars_if_possible(&ret_param_ty);
debug!("deduce_sig_from_projection: ret_param_ty {}", ret_param_ty.repr(tcx));
let fn_sig = ty::FnSig {
inputs: input_tys,
output: ty::FnConverging(ret_param_ty),
variadic: false
};
debug!("deduce_sig_from_projection: fn_sig {}", fn_sig.repr(tcx));
Some(fn_sig)
}
fn self_type_matches_expected_vid<'a,'tcx>(
fcx: &FnCtxt<'a,'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
expected_vid: ty::TyVid)
-> Option<ty::PolyTraitRef<'tcx>>
{
let self_ty = fcx.infcx().shallow_resolve(trait_ref.self_ty());
debug!("self_type_matches_expected_vid(trait_ref={}, self_ty={})",
trait_ref.repr(fcx.tcx()),
self_ty.repr(fcx.tcx()));
match self_ty.sty {
ty::ty_infer(ty::TyVar(v)) if expected_vid == v => Some(trait_ref),
_ => None,
}
}