Skip to content

Commit ea4db35

Browse files
committed
move ty_of_closure into check/closure.rs, its only caller
1 parent 11118dc commit ea4db35

File tree

2 files changed

+69
-64
lines changed

2 files changed

+69
-64
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,65 +1206,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
12061206
bare_fn_ty
12071207
}
12081208

1209-
pub fn ty_of_closure(&self,
1210-
unsafety: hir::Unsafety,
1211-
decl: &hir::FnDecl,
1212-
abi: abi::Abi,
1213-
expected_sig: Option<ty::FnSig<'tcx>>)
1214-
-> ty::PolyFnSig<'tcx>
1215-
{
1216-
debug!("ty_of_closure(expected_sig={:?})",
1217-
expected_sig);
1218-
1219-
let input_tys = decl.inputs.iter().enumerate().map(|(i, a)| {
1220-
let expected_arg_ty = expected_sig.as_ref().and_then(|e| {
1221-
// no guarantee that the correct number of expected args
1222-
// were supplied
1223-
if i < e.inputs().len() {
1224-
Some(e.inputs()[i])
1225-
} else {
1226-
None
1227-
}
1228-
});
1229-
1230-
let input_ty = self.ty_of_arg(a, expected_arg_ty);
1231-
debug!("ty_of_closure: i={} input_ty={:?} expected_arg_ty={:?}",
1232-
i, input_ty, expected_arg_ty);
1233-
1234-
input_ty
1235-
});
1236-
1237-
let expected_ret_ty = expected_sig.as_ref().map(|e| e.output());
1238-
1239-
let output_ty = match decl.output {
1240-
hir::Return(ref output) => {
1241-
if let (&hir::TyInfer, Some(expected_ret_ty)) = (&output.node, expected_ret_ty) {
1242-
self.record_ty(output.hir_id, expected_ret_ty, output.span);
1243-
expected_ret_ty
1244-
} else {
1245-
self.ast_ty_to_ty(&output)
1246-
}
1247-
}
1248-
hir::DefaultReturn(span) => {
1249-
if let Some(expected_ret_ty) = expected_ret_ty {
1250-
expected_ret_ty
1251-
} else {
1252-
self.ty_infer(span)
1253-
}
1254-
}
1255-
};
1256-
1257-
debug!("ty_of_closure: output_ty={:?}", output_ty);
1258-
1259-
ty::Binder(self.tcx().mk_fn_sig(
1260-
input_tys,
1261-
output_ty,
1262-
decl.variadic,
1263-
unsafety,
1264-
abi
1265-
))
1266-
}
1267-
12681209
/// Given the bounds on an object, determines what single region bound (if any) we can
12691210
/// use to summarize this type. The basic idea is that we will use the bound the user
12701211
/// provided, if they provided one, and otherwise search the supertypes of trait bounds

src/librustc_typeck/check/closure.rs

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5656
expected_sig);
5757

5858
let expr_def_id = self.tcx.hir.local_def_id(expr.id);
59-
let sig = AstConv::ty_of_closure(self,
60-
hir::Unsafety::Normal,
61-
decl,
62-
Abi::RustCall,
63-
expected_sig);
59+
let sig = self.ty_of_closure(decl, expected_sig);
6460

6561
debug!("check_closure: ty_of_closure returns {:?}", sig);
6662

@@ -274,4 +270,72 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
274270
_ => None,
275271
}
276272
}
273+
274+
/// Invoked to compute the signature of a closure expression. This
275+
/// combines any user-provided type annotations (e.g., `|x: u32|
276+
/// -> u32 { .. }`) with the expected signature.
277+
///
278+
/// The arguments here are a bit odd-ball:
279+
///
280+
/// - `decl`: the HIR declaration of the closure
281+
/// - `expected_sig`: the expected signature (if any). Note that
282+
/// this is missing a binder: that is, there may be late-bound
283+
/// regions with depth 1, which are bound then by the closure.
284+
fn ty_of_closure(&self,
285+
decl: &hir::FnDecl,
286+
expected_sig: Option<ty::FnSig<'tcx>>)
287+
-> ty::PolyFnSig<'tcx>
288+
{
289+
let astconv: &AstConv = self;
290+
291+
debug!("ty_of_closure(expected_sig={:?})",
292+
expected_sig);
293+
294+
let input_tys = decl.inputs.iter().enumerate().map(|(i, a)| {
295+
let expected_arg_ty = expected_sig.as_ref().and_then(|e| {
296+
// no guarantee that the correct number of expected args
297+
// were supplied
298+
if i < e.inputs().len() {
299+
Some(e.inputs()[i])
300+
} else {
301+
None
302+
}
303+
});
304+
305+
let input_ty = astconv.ty_of_arg(a, expected_arg_ty);
306+
debug!("ty_of_closure: i={} input_ty={:?} expected_arg_ty={:?}",
307+
i, input_ty, expected_arg_ty);
308+
309+
input_ty
310+
});
311+
312+
let expected_ret_ty = expected_sig.as_ref().map(|e| e.output());
313+
314+
let output_ty = match decl.output {
315+
hir::Return(ref output) => {
316+
if let (&hir::TyInfer, Some(expected_ret_ty)) = (&output.node, expected_ret_ty) {
317+
astconv.record_ty(output.hir_id, expected_ret_ty, output.span);
318+
expected_ret_ty
319+
} else {
320+
astconv.ast_ty_to_ty(&output)
321+
}
322+
}
323+
hir::DefaultReturn(span) => {
324+
if let Some(expected_ret_ty) = expected_ret_ty {
325+
expected_ret_ty
326+
} else {
327+
astconv.ty_infer(span)
328+
}
329+
}
330+
};
331+
332+
debug!("ty_of_closure: output_ty={:?}", output_ty);
333+
334+
ty::Binder(self.tcx.mk_fn_sig(
335+
input_tys,
336+
output_ty,
337+
decl.variadic,
338+
hir::Unsafety::Normal,
339+
Abi::RustCall))
340+
}
277341
}

0 commit comments

Comments
 (0)