Skip to content

Commit e82c08f

Browse files
committed
Refactor rustc_hir_typeck::closure.
1 parent 2c4b0b2 commit e82c08f

File tree

5 files changed

+58
-74
lines changed

5 files changed

+58
-74
lines changed

compiler/rustc_hir_typeck/src/check.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ pub(super) fn check_fn<'a, 'tcx>(
2626
param_env: ty::ParamEnv<'tcx>,
2727
fn_sig: ty::FnSig<'tcx>,
2828
decl: &'tcx hir::FnDecl<'tcx>,
29-
fn_id: hir::HirId,
29+
fn_def_id: LocalDefId,
3030
body: &'tcx hir::Body<'tcx>,
3131
can_be_generator: Option<hir::Movability>,
3232
) -> (FnCtxt<'a, 'tcx>, Option<GeneratorTypes<'tcx>>) {
33+
let fn_id = inherited.tcx.hir().local_def_id_to_hir_id(fn_def_id);
34+
3335
// Create the function context. This is either derived from scratch or,
3436
// in the case of closures, based on the outer context.
3537
let mut fcx = FnCtxt::new(inherited, param_env, body.value.hir_id);

compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 44 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{check_fn, Expectation, FnCtxt, GeneratorTypes};
44

55
use hir::def::DefKind;
66
use rustc_hir as hir;
7-
use rustc_hir::def_id::DefId;
7+
use rustc_hir::def_id::LocalDefId;
88
use rustc_hir::lang_items::LangItem;
99
use rustc_hir_analysis::astconv::AstConv;
1010
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
@@ -41,18 +41,14 @@ struct ClosureSignatures<'tcx> {
4141
}
4242

4343
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
44-
#[instrument(skip(self, expr, _capture, decl, body_id), level = "debug")]
44+
#[instrument(skip(self, closure), level = "debug")]
4545
pub fn check_expr_closure(
4646
&self,
47-
expr: &hir::Expr<'_>,
48-
_capture: hir::CaptureBy,
49-
decl: &'tcx hir::FnDecl<'tcx>,
50-
body_id: hir::BodyId,
51-
gen: Option<hir::Movability>,
47+
closure: &hir::Closure<'tcx>,
48+
expr_span: Span,
5249
expected: Expectation<'tcx>,
5350
) -> Ty<'tcx> {
54-
trace!("decl = {:#?}", decl);
55-
trace!("expr = {:#?}", expr);
51+
trace!("decl = {:#?}", closure.fn_decl);
5652

5753
// It's always helpful for inference if we know the kind of
5854
// closure sooner rather than later, so first examine the expected
@@ -61,37 +57,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6157
Some(ty) => self.deduce_expectations_from_expected_type(ty),
6258
None => (None, None),
6359
};
64-
let body = self.tcx.hir().body(body_id);
65-
self.check_closure(expr, expected_kind, decl, body, gen, expected_sig)
60+
let body = self.tcx.hir().body(closure.body);
61+
self.check_closure(closure, expr_span, expected_kind, body, expected_sig)
6662
}
6763

68-
#[instrument(skip(self, expr, body, decl), level = "debug", ret)]
64+
#[instrument(skip(self, closure, body), level = "debug", ret)]
6965
fn check_closure(
7066
&self,
71-
expr: &hir::Expr<'_>,
67+
closure: &hir::Closure<'tcx>,
68+
expr_span: Span,
7269
opt_kind: Option<ty::ClosureKind>,
73-
decl: &'tcx hir::FnDecl<'tcx>,
7470
body: &'tcx hir::Body<'tcx>,
75-
gen: Option<hir::Movability>,
7671
expected_sig: Option<ExpectedSig<'tcx>>,
7772
) -> Ty<'tcx> {
78-
trace!("decl = {:#?}", decl);
79-
let expr_def_id = self.tcx.hir().local_def_id(expr.hir_id);
73+
trace!("decl = {:#?}", closure.fn_decl);
74+
let expr_def_id = closure.def_id;
8075
debug!(?expr_def_id);
8176

8277
let ClosureSignatures { bound_sig, liberated_sig } =
83-
self.sig_of_closure(expr.hir_id, expr_def_id.to_def_id(), decl, body, expected_sig);
78+
self.sig_of_closure(expr_def_id, closure.fn_decl, body, expected_sig);
8479

8580
debug!(?bound_sig, ?liberated_sig);
8681

8782
let generator_types = check_fn(
8883
self,
8984
self.param_env.without_const(),
9085
liberated_sig,
91-
decl,
92-
expr.hir_id,
86+
closure.fn_decl,
87+
expr_def_id,
9388
body,
94-
gen,
89+
closure.movability,
9590
)
9691
.1;
9792

@@ -102,7 +97,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10297

10398
let tupled_upvars_ty = self.next_ty_var(TypeVariableOrigin {
10499
kind: TypeVariableOriginKind::ClosureSynthetic,
105-
span: self.tcx.hir().span(expr.hir_id),
100+
span: self.tcx.def_span(expr_def_id),
106101
});
107102

108103
if let Some(GeneratorTypes { resume_ty, yield_ty, interior, movability }) = generator_types
@@ -148,7 +143,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
148143
None => self.next_ty_var(TypeVariableOrigin {
149144
// FIXME(eddyb) distinguish closure kind inference variables from the rest.
150145
kind: TypeVariableOriginKind::ClosureSynthetic,
151-
span: expr.span,
146+
span: expr_span,
152147
}),
153148
};
154149

@@ -342,30 +337,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
342337

343338
fn sig_of_closure(
344339
&self,
345-
hir_id: hir::HirId,
346-
expr_def_id: DefId,
340+
expr_def_id: LocalDefId,
347341
decl: &hir::FnDecl<'_>,
348342
body: &hir::Body<'_>,
349343
expected_sig: Option<ExpectedSig<'tcx>>,
350344
) -> ClosureSignatures<'tcx> {
351345
if let Some(e) = expected_sig {
352-
self.sig_of_closure_with_expectation(hir_id, expr_def_id, decl, body, e)
346+
self.sig_of_closure_with_expectation(expr_def_id, decl, body, e)
353347
} else {
354-
self.sig_of_closure_no_expectation(hir_id, expr_def_id, decl, body)
348+
self.sig_of_closure_no_expectation(expr_def_id, decl, body)
355349
}
356350
}
357351

358352
/// If there is no expected signature, then we will convert the
359353
/// types that the user gave into a signature.
360-
#[instrument(skip(self, hir_id, expr_def_id, decl, body), level = "debug")]
354+
#[instrument(skip(self, expr_def_id, decl, body), level = "debug")]
361355
fn sig_of_closure_no_expectation(
362356
&self,
363-
hir_id: hir::HirId,
364-
expr_def_id: DefId,
357+
expr_def_id: LocalDefId,
365358
decl: &hir::FnDecl<'_>,
366359
body: &hir::Body<'_>,
367360
) -> ClosureSignatures<'tcx> {
368-
let bound_sig = self.supplied_sig_of_closure(hir_id, expr_def_id, decl, body);
361+
let bound_sig = self.supplied_sig_of_closure(expr_def_id, decl, body);
369362

370363
self.closure_sigs(expr_def_id, body, bound_sig)
371364
}
@@ -411,17 +404,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
411404
///
412405
/// # Arguments
413406
///
414-
/// - `expr_def_id`: the `DefId` of the closure expression
407+
/// - `expr_def_id`: the `LocalDefId` of the closure expression
415408
/// - `decl`: the HIR declaration of the closure
416409
/// - `body`: the body of the closure
417410
/// - `expected_sig`: the expected signature (if any). Note that
418411
/// this is missing a binder: that is, there may be late-bound
419412
/// regions with depth 1, which are bound then by the closure.
420-
#[instrument(skip(self, hir_id, expr_def_id, decl, body), level = "debug")]
413+
#[instrument(skip(self, expr_def_id, decl, body), level = "debug")]
421414
fn sig_of_closure_with_expectation(
422415
&self,
423-
hir_id: hir::HirId,
424-
expr_def_id: DefId,
416+
expr_def_id: LocalDefId,
425417
decl: &hir::FnDecl<'_>,
426418
body: &hir::Body<'_>,
427419
expected_sig: ExpectedSig<'tcx>,
@@ -430,7 +422,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
430422
// expectation if things don't see to match up with what we
431423
// expect.
432424
if expected_sig.sig.c_variadic() != decl.c_variadic {
433-
return self.sig_of_closure_no_expectation(hir_id, expr_def_id, decl, body);
425+
return self.sig_of_closure_no_expectation(expr_def_id, decl, body);
434426
} else if expected_sig.sig.skip_binder().inputs_and_output.len() != decl.inputs.len() + 1 {
435427
return self.sig_of_closure_with_mismatched_number_of_arguments(
436428
expr_def_id,
@@ -466,27 +458,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
466458
// Along the way, it also writes out entries for types that the user
467459
// wrote into our typeck results, which are then later used by the privacy
468460
// check.
469-
match self.merge_supplied_sig_with_expectation(
470-
hir_id,
471-
expr_def_id,
472-
decl,
473-
body,
474-
closure_sigs,
475-
) {
461+
match self.merge_supplied_sig_with_expectation(expr_def_id, decl, body, closure_sigs) {
476462
Ok(infer_ok) => self.register_infer_ok_obligations(infer_ok),
477-
Err(_) => self.sig_of_closure_no_expectation(hir_id, expr_def_id, decl, body),
463+
Err(_) => self.sig_of_closure_no_expectation(expr_def_id, decl, body),
478464
}
479465
}
480466

481467
fn sig_of_closure_with_mismatched_number_of_arguments(
482468
&self,
483-
expr_def_id: DefId,
469+
expr_def_id: LocalDefId,
484470
decl: &hir::FnDecl<'_>,
485471
body: &hir::Body<'_>,
486472
expected_sig: ExpectedSig<'tcx>,
487473
) -> ClosureSignatures<'tcx> {
488474
let hir = self.tcx.hir();
489-
let expr_map_node = hir.get_if_local(expr_def_id).unwrap();
475+
let expr_map_node = hir.get_by_def_id(expr_def_id);
490476
let expected_args: Vec<_> = expected_sig
491477
.sig
492478
.skip_binder()
@@ -499,7 +485,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
499485
None => (None, Vec::new()),
500486
};
501487
let expected_span =
502-
expected_sig.cause_span.unwrap_or_else(|| hir.span_if_local(expr_def_id).unwrap());
488+
expected_sig.cause_span.unwrap_or_else(|| self.tcx.def_span(expr_def_id));
503489
self.report_arg_count_mismatch(
504490
expected_span,
505491
closure_span,
@@ -517,11 +503,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
517503
/// Enforce the user's types against the expectation. See
518504
/// `sig_of_closure_with_expectation` for details on the overall
519505
/// strategy.
520-
#[instrument(level = "debug", skip(self, hir_id, expr_def_id, decl, body, expected_sigs))]
506+
#[instrument(level = "debug", skip(self, expr_def_id, decl, body, expected_sigs))]
521507
fn merge_supplied_sig_with_expectation(
522508
&self,
523-
hir_id: hir::HirId,
524-
expr_def_id: DefId,
509+
expr_def_id: LocalDefId,
525510
decl: &hir::FnDecl<'_>,
526511
body: &hir::Body<'_>,
527512
mut expected_sigs: ClosureSignatures<'tcx>,
@@ -530,7 +515,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
530515
//
531516
// (See comment on `sig_of_closure_with_expectation` for the
532517
// meaning of these letters.)
533-
let supplied_sig = self.supplied_sig_of_closure(hir_id, expr_def_id, decl, body);
518+
let supplied_sig = self.supplied_sig_of_closure(expr_def_id, decl, body);
534519

535520
debug!(?supplied_sig);
536521

@@ -610,8 +595,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
610595
#[instrument(skip(self, decl, body), level = "debug", ret)]
611596
fn supplied_sig_of_closure(
612597
&self,
613-
hir_id: hir::HirId,
614-
expr_def_id: DefId,
598+
expr_def_id: LocalDefId,
615599
decl: &hir::FnDecl<'_>,
616600
body: &hir::Body<'_>,
617601
) -> ty::PolyFnSig<'tcx> {
@@ -620,6 +604,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
620604
trace!("decl = {:#?}", decl);
621605
debug!(?body.generator_kind);
622606

607+
let hir_id = self.tcx.hir().local_def_id_to_hir_id(expr_def_id);
623608
let bound_vars = self.tcx.late_bound_vars(hir_id);
624609

625610
// First, convert the types that the user supplied (if any).
@@ -664,10 +649,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
664649
let result = self.normalize_associated_types_in(self.tcx.hir().span(hir_id), result);
665650

666651
let c_result = self.inh.infcx.canonicalize_response(result);
667-
self.typeck_results
668-
.borrow_mut()
669-
.user_provided_sigs
670-
.insert(expr_def_id.expect_local(), c_result);
652+
self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result);
671653

672654
result
673655
}
@@ -681,7 +663,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
681663
#[instrument(skip(self), level = "debug", ret)]
682664
fn deduce_future_output_from_obligations(
683665
&self,
684-
expr_def_id: DefId,
666+
expr_def_id: LocalDefId,
685667
body_id: hir::HirId,
686668
) -> Option<Ty<'tcx>> {
687669
let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
@@ -830,14 +812,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
830812

831813
fn closure_sigs(
832814
&self,
833-
expr_def_id: DefId,
815+
expr_def_id: LocalDefId,
834816
body: &hir::Body<'_>,
835817
bound_sig: ty::PolyFnSig<'tcx>,
836818
) -> ClosureSignatures<'tcx> {
837-
let liberated_sig = self.tcx().liberate_late_bound_regions(expr_def_id, bound_sig);
819+
let liberated_sig =
820+
self.tcx().liberate_late_bound_regions(expr_def_id.to_def_id(), bound_sig);
838821
let liberated_sig = self.inh.normalize_associated_types_in(
839822
body.value.span,
840-
body.value.hir_id,
823+
self.tcx.hir().local_def_id_to_hir_id(expr_def_id),
841824
self.param_env,
842825
liberated_sig,
843826
);

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
3030
use rustc_hir::def_id::DefId;
3131
use rustc_hir::intravisit::Visitor;
3232
use rustc_hir::lang_items::LangItem;
33-
use rustc_hir::{Closure, ExprKind, HirId, QPath};
33+
use rustc_hir::{ExprKind, HirId, QPath};
3434
use rustc_hir_analysis::astconv::AstConv as _;
3535
use rustc_hir_analysis::check::ty_kind_suggestion;
3636
use rustc_infer::infer;
@@ -324,9 +324,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
324324
ExprKind::Match(discrim, arms, match_src) => {
325325
self.check_match(expr, &discrim, arms, expected, match_src)
326326
}
327-
ExprKind::Closure(&Closure { capture_clause, fn_decl, body, movability, .. }) => {
328-
self.check_expr_closure(expr, capture_clause, &fn_decl, body, movability, expected)
329-
}
327+
ExprKind::Closure(closure) => self.check_expr_closure(closure, expr.span, expected),
330328
ExprKind::Block(body, _) => self.check_block_with_expected(&body, expected),
331329
ExprKind::Call(callee, args) => self.check_call(expr, &callee, args, expected),
332330
ExprKind::MethodCall(segment, receiver, args, _) => {

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
352352
self.consume_expr(base);
353353
}
354354

355-
hir::ExprKind::Closure { .. } => {
356-
self.walk_captures(expr);
355+
hir::ExprKind::Closure(closure) => {
356+
self.walk_captures(closure);
357357
}
358358

359359
hir::ExprKind::Box(ref base) => {
@@ -745,7 +745,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
745745
///
746746
/// - When reporting the Place back to the Delegate, ensure that the UpvarId uses the enclosing
747747
/// closure as the DefId.
748-
fn walk_captures(&mut self, closure_expr: &hir::Expr<'_>) {
748+
fn walk_captures(&mut self, closure_expr: &hir::Closure<'_>) {
749749
fn upvar_is_local_variable<'tcx>(
750750
upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
751751
upvar_id: hir::HirId,
@@ -757,7 +757,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
757757
debug!("walk_captures({:?})", closure_expr);
758758

759759
let tcx = self.tcx();
760-
let closure_def_id = tcx.hir().local_def_id(closure_expr.hir_id);
760+
let closure_def_id = closure_expr.def_id;
761761
let upvars = tcx.upvars_mentioned(self.body_owner);
762762

763763
// For purposes of this function, generator and closures are equivalent.
@@ -829,10 +829,11 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
829829
// be a local variable
830830
PlaceBase::Local(*var_hir_id)
831831
};
832+
let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id);
832833
let place_with_id = PlaceWithHirId::new(
833-
capture_info.path_expr_id.unwrap_or(
834-
capture_info.capture_kind_expr_id.unwrap_or(closure_expr.hir_id),
835-
),
834+
capture_info
835+
.path_expr_id
836+
.unwrap_or(capture_info.capture_kind_expr_id.unwrap_or(closure_hir_id)),
836837
place.base_ty,
837838
place_base,
838839
place.projections.clone(),

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ fn typeck_with_fallback<'tcx>(
251251
param_env,
252252
fn_sig,
253253
);
254-
check_fn(&inh, param_env, fn_sig, decl, id, body, None).0
254+
check_fn(&inh, param_env, fn_sig, decl, def_id, body, None).0
255255
} else {
256256
let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
257257
let expected_type = body_ty

0 commit comments

Comments
 (0)