Skip to content

Commit 1796345

Browse files
Don't pass body into check_closure and child functions
1 parent ab5b2e8 commit 1796345

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

compiler/rustc_hir_typeck/src/closure.rs

+46-50
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6060
}
6161
None => (None, None),
6262
};
63-
let body = self.tcx.hir().body(closure.body);
64-
self.check_closure(closure, expr_span, expected_kind, body, expected_sig)
63+
64+
self.check_closure(closure, expr_span, expected_kind, expected_sig)
6565
}
6666

67-
#[instrument(skip(self, closure, body), level = "debug", ret)]
67+
#[instrument(skip(self, closure), level = "debug", ret)]
6868
fn check_closure(
6969
&self,
7070
closure: &hir::Closure<'tcx>,
7171
expr_span: Span,
7272
opt_kind: Option<ty::ClosureKind>,
73-
body: &'tcx hir::Body<'tcx>,
7473
expected_sig: Option<ExpectedSig<'tcx>>,
7574
) -> Ty<'tcx> {
75+
let body = self.tcx.hir().body(closure.body);
76+
7677
trace!("decl = {:#?}", closure.fn_decl);
7778
let expr_def_id = closure.def_id;
7879
debug!(?expr_def_id);
7980

8081
let ClosureSignatures { bound_sig, liberated_sig } =
81-
self.sig_of_closure(expr_def_id, closure.fn_decl, body, expected_sig);
82+
self.sig_of_closure(expr_def_id, closure.fn_decl, body.coroutine_kind, expected_sig);
8283

8384
debug!(?bound_sig, ?liberated_sig);
8485

@@ -351,28 +352,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
351352
&self,
352353
expr_def_id: LocalDefId,
353354
decl: &hir::FnDecl<'_>,
354-
body: &hir::Body<'_>,
355+
coroutine_kind: Option<hir::CoroutineKind>,
355356
expected_sig: Option<ExpectedSig<'tcx>>,
356357
) -> ClosureSignatures<'tcx> {
357358
if let Some(e) = expected_sig {
358-
self.sig_of_closure_with_expectation(expr_def_id, decl, body, e)
359+
self.sig_of_closure_with_expectation(expr_def_id, decl, coroutine_kind, e)
359360
} else {
360-
self.sig_of_closure_no_expectation(expr_def_id, decl, body)
361+
self.sig_of_closure_no_expectation(expr_def_id, decl, coroutine_kind)
361362
}
362363
}
363364

364365
/// If there is no expected signature, then we will convert the
365366
/// types that the user gave into a signature.
366-
#[instrument(skip(self, expr_def_id, decl, body), level = "debug")]
367+
#[instrument(skip(self, expr_def_id, decl), level = "debug")]
367368
fn sig_of_closure_no_expectation(
368369
&self,
369370
expr_def_id: LocalDefId,
370371
decl: &hir::FnDecl<'_>,
371-
body: &hir::Body<'_>,
372+
coroutine_kind: Option<hir::CoroutineKind>,
372373
) -> ClosureSignatures<'tcx> {
373-
let bound_sig = self.supplied_sig_of_closure(expr_def_id, decl, body);
374+
let bound_sig = self.supplied_sig_of_closure(expr_def_id, decl, coroutine_kind);
374375

375-
self.closure_sigs(expr_def_id, body, bound_sig)
376+
self.closure_sigs(expr_def_id, bound_sig)
376377
}
377378

378379
/// Invoked to compute the signature of a closure expression. This
@@ -422,24 +423,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
422423
/// - `expected_sig`: the expected signature (if any). Note that
423424
/// this is missing a binder: that is, there may be late-bound
424425
/// regions with depth 1, which are bound then by the closure.
425-
#[instrument(skip(self, expr_def_id, decl, body), level = "debug")]
426+
#[instrument(skip(self, expr_def_id, decl), level = "debug")]
426427
fn sig_of_closure_with_expectation(
427428
&self,
428429
expr_def_id: LocalDefId,
429430
decl: &hir::FnDecl<'_>,
430-
body: &hir::Body<'_>,
431+
coroutine_kind: Option<hir::CoroutineKind>,
431432
expected_sig: ExpectedSig<'tcx>,
432433
) -> ClosureSignatures<'tcx> {
433434
// Watch out for some surprises and just ignore the
434435
// expectation if things don't see to match up with what we
435436
// expect.
436437
if expected_sig.sig.c_variadic() != decl.c_variadic {
437-
return self.sig_of_closure_no_expectation(expr_def_id, decl, body);
438+
return self.sig_of_closure_no_expectation(expr_def_id, decl, coroutine_kind);
438439
} else if expected_sig.sig.skip_binder().inputs_and_output.len() != decl.inputs.len() + 1 {
439440
return self.sig_of_closure_with_mismatched_number_of_arguments(
440441
expr_def_id,
441442
decl,
442-
body,
443443
expected_sig,
444444
);
445445
}
@@ -463,24 +463,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
463463
// anonymize away, so as not to confuse the user.
464464
let bound_sig = self.tcx.anonymize_bound_vars(bound_sig);
465465

466-
let closure_sigs = self.closure_sigs(expr_def_id, body, bound_sig);
466+
let closure_sigs = self.closure_sigs(expr_def_id, bound_sig);
467467

468468
// Up till this point, we have ignored the annotations that the user
469469
// gave. This function will check that they unify successfully.
470470
// Along the way, it also writes out entries for types that the user
471471
// wrote into our typeck results, which are then later used by the privacy
472472
// check.
473-
match self.merge_supplied_sig_with_expectation(expr_def_id, decl, body, closure_sigs) {
473+
match self.merge_supplied_sig_with_expectation(
474+
expr_def_id,
475+
decl,
476+
coroutine_kind,
477+
closure_sigs,
478+
) {
474479
Ok(infer_ok) => self.register_infer_ok_obligations(infer_ok),
475-
Err(_) => self.sig_of_closure_no_expectation(expr_def_id, decl, body),
480+
Err(_) => self.sig_of_closure_no_expectation(expr_def_id, decl, coroutine_kind),
476481
}
477482
}
478483

479484
fn sig_of_closure_with_mismatched_number_of_arguments(
480485
&self,
481486
expr_def_id: LocalDefId,
482487
decl: &hir::FnDecl<'_>,
483-
body: &hir::Body<'_>,
484488
expected_sig: ExpectedSig<'tcx>,
485489
) -> ClosureSignatures<'tcx> {
486490
let expr_map_node = self.tcx.hir_node_by_def_id(expr_def_id);
@@ -511,25 +515,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
511515

512516
let error_sig = self.error_sig_of_closure(decl, guar);
513517

514-
self.closure_sigs(expr_def_id, body, error_sig)
518+
self.closure_sigs(expr_def_id, error_sig)
515519
}
516520

517521
/// Enforce the user's types against the expectation. See
518522
/// `sig_of_closure_with_expectation` for details on the overall
519523
/// strategy.
520-
#[instrument(level = "debug", skip(self, expr_def_id, decl, body, expected_sigs))]
524+
#[instrument(level = "debug", skip(self, expr_def_id, decl, expected_sigs))]
521525
fn merge_supplied_sig_with_expectation(
522526
&self,
523527
expr_def_id: LocalDefId,
524528
decl: &hir::FnDecl<'_>,
525-
body: &hir::Body<'_>,
529+
coroutine_kind: Option<hir::CoroutineKind>,
526530
mut expected_sigs: ClosureSignatures<'tcx>,
527531
) -> InferResult<'tcx, ClosureSignatures<'tcx>> {
528532
// Get the signature S that the user gave.
529533
//
530534
// (See comment on `sig_of_closure_with_expectation` for the
531535
// meaning of these letters.)
532-
let supplied_sig = self.supplied_sig_of_closure(expr_def_id, decl, body);
536+
let supplied_sig = self.supplied_sig_of_closure(expr_def_id, decl, coroutine_kind);
533537

534538
debug!(?supplied_sig);
535539

@@ -611,17 +615,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
611615
/// types that the user gave into a signature.
612616
///
613617
/// Also, record this closure signature for later.
614-
#[instrument(skip(self, decl, body), level = "debug", ret)]
618+
#[instrument(skip(self, decl), level = "debug", ret)]
615619
fn supplied_sig_of_closure(
616620
&self,
617621
expr_def_id: LocalDefId,
618622
decl: &hir::FnDecl<'_>,
619-
body: &hir::Body<'_>,
623+
coroutine_kind: Option<hir::CoroutineKind>,
620624
) -> ty::PolyFnSig<'tcx> {
621625
let astconv: &dyn AstConv<'_> = self;
622626

623627
trace!("decl = {:#?}", decl);
624-
debug!(?body.coroutine_kind);
628+
debug!(?coroutine_kind);
625629

626630
let hir_id = self.tcx.local_def_id_to_hir_id(expr_def_id);
627631
let bound_vars = self.tcx.late_bound_vars(hir_id);
@@ -630,7 +634,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
630634
let supplied_arguments = decl.inputs.iter().map(|a| astconv.ast_ty_to_ty(a));
631635
let supplied_return = match decl.output {
632636
hir::FnRetTy::Return(ref output) => astconv.ast_ty_to_ty(output),
633-
hir::FnRetTy::DefaultReturn(_) => match body.coroutine_kind {
637+
hir::FnRetTy::DefaultReturn(_) => match coroutine_kind {
634638
// In the case of the async block that we create for a function body,
635639
// we expect the return type of the block to match that of the enclosing
636640
// function.
@@ -639,19 +643,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
639643
hir::CoroutineSource::Fn,
640644
)) => {
641645
debug!("closure is async fn body");
642-
let def_id = self.tcx.hir().body_owner_def_id(body.id());
643-
self.deduce_future_output_from_obligations(expr_def_id, def_id).unwrap_or_else(
644-
|| {
645-
// AFAIK, deducing the future output
646-
// always succeeds *except* in error cases
647-
// like #65159. I'd like to return Error
648-
// here, but I can't because I can't
649-
// easily (and locally) prove that we
650-
// *have* reported an
651-
// error. --nikomatsakis
652-
astconv.ty_infer(None, decl.output.span())
653-
},
654-
)
646+
self.deduce_future_output_from_obligations(expr_def_id).unwrap_or_else(|| {
647+
// AFAIK, deducing the future output
648+
// always succeeds *except* in error cases
649+
// like #65159. I'd like to return Error
650+
// here, but I can't because I can't
651+
// easily (and locally) prove that we
652+
// *have* reported an
653+
// error. --nikomatsakis
654+
astconv.ty_infer(None, decl.output.span())
655+
})
655656
}
656657
// All `gen {}` and `async gen {}` must return unit.
657658
Some(
@@ -688,16 +689,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
688689
/// Future<Output = T>`, so we do this by searching through the
689690
/// obligations to extract the `T`.
690691
#[instrument(skip(self), level = "debug", ret)]
691-
fn deduce_future_output_from_obligations(
692-
&self,
693-
expr_def_id: LocalDefId,
694-
body_def_id: LocalDefId,
695-
) -> Option<Ty<'tcx>> {
692+
fn deduce_future_output_from_obligations(&self, body_def_id: LocalDefId) -> Option<Ty<'tcx>> {
696693
let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
697-
span_bug!(self.tcx.def_span(expr_def_id), "async fn coroutine outside of a fn")
694+
span_bug!(self.tcx.def_span(body_def_id), "async fn coroutine outside of a fn")
698695
});
699696

700-
let closure_span = self.tcx.def_span(expr_def_id);
697+
let closure_span = self.tcx.def_span(body_def_id);
701698
let ret_ty = ret_coercion.borrow().expected_ty();
702699
let ret_ty = self.try_structurally_resolve_type(closure_span, ret_ty);
703700

@@ -842,12 +839,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
842839
fn closure_sigs(
843840
&self,
844841
expr_def_id: LocalDefId,
845-
body: &hir::Body<'_>,
846842
bound_sig: ty::PolyFnSig<'tcx>,
847843
) -> ClosureSignatures<'tcx> {
848844
let liberated_sig =
849845
self.tcx().liberate_late_bound_regions(expr_def_id.to_def_id(), bound_sig);
850-
let liberated_sig = self.normalize(body.value.span, liberated_sig);
846+
let liberated_sig = self.normalize(self.tcx.def_span(expr_def_id), liberated_sig);
851847
ClosureSignatures { bound_sig, liberated_sig }
852848
}
853849
}

0 commit comments

Comments
 (0)