Skip to content

Commit 0915d55

Browse files
committed
Wrap more into into closure_typeinfo query.
1 parent 7dcc74e commit 0915d55

File tree

9 files changed

+49
-56
lines changed

9 files changed

+49
-56
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::{
66
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
77
};
88
use rustc_hir as hir;
9-
use rustc_hir::def::Res;
9+
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
1111
use rustc_hir::{AsyncGeneratorKind, GeneratorKind, LangItem};
1212
use rustc_infer::infer::TyCtxtInferExt;
@@ -236,10 +236,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
236236
let ty = used_place.ty(self.body, self.infcx.tcx).ty;
237237
let needs_note = match ty.kind() {
238238
ty::Closure(id, _) => {
239-
let tables = self.infcx.tcx.typeck(id.expect_local());
240-
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(id.expect_local());
241-
242-
tables.closure_kind_origins().get(hir_id).is_none()
239+
self.infcx.tcx.closure_kind_origin(id.expect_local()).is_none()
243240
}
244241
_ => true,
245242
};
@@ -1670,27 +1667,19 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16701667
format!("`{}` would have to be valid for `{}`...", name, region_name),
16711668
);
16721669

1673-
let fn_hir_id = self.mir_hir_id();
16741670
err.span_label(
16751671
drop_span,
16761672
format!(
16771673
"...but `{}` will be dropped here, when the {} returns",
16781674
name,
16791675
self.infcx
16801676
.tcx
1681-
.hir()
1682-
.opt_name(fn_hir_id)
1677+
.opt_item_name(self.mir_def_id().to_def_id())
16831678
.map(|name| format!("function `{}`", name))
16841679
.unwrap_or_else(|| {
1685-
match &self
1686-
.infcx
1687-
.tcx
1688-
.typeck(self.mir_def_id())
1689-
.node_type(fn_hir_id)
1690-
.kind()
1691-
{
1692-
ty::Closure(..) => "enclosing closure",
1693-
ty::Generator(..) => "enclosing generator",
1680+
match &self.infcx.tcx.def_kind(self.mir_def_id()) {
1681+
DefKind::Closure => "enclosing closure",
1682+
DefKind::Generator => "enclosing generator",
16941683
kind => bug!("expected closure or generator, found {:?}", kind),
16951684
}
16961685
.to_string()

compiler/rustc_borrowck/src/diagnostics/mod.rs

+5-19
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
115115
debug!("add_moved_or_invoked_closure_note: closure={:?}", closure);
116116
if let ty::Closure(did, _) = self.body.local_decls[closure].ty.kind() {
117117
let did = did.expect_local();
118-
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did);
119-
120-
if let Some((span, hir_place)) =
121-
self.infcx.tcx.typeck(did).closure_kind_origins().get(hir_id)
122-
{
118+
if let Some((span, hir_place)) = self.infcx.tcx.closure_kind_origin(did) {
123119
diag.span_note(
124120
*span,
125121
&format!(
@@ -139,11 +135,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
139135
if let Some(target) = target {
140136
if let ty::Closure(did, _) = self.body.local_decls[target].ty.kind() {
141137
let did = did.expect_local();
142-
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did);
143-
144-
if let Some((span, hir_place)) =
145-
self.infcx.tcx.typeck(did).closure_kind_origins().get(hir_id)
146-
{
138+
if let Some((span, hir_place)) = self.infcx.tcx.closure_kind_origin(did) {
147139
diag.span_note(
148140
*span,
149141
&format!(
@@ -373,14 +365,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
373365
//
374366
// We know the field exists so it's safe to call operator[] and `unwrap` here.
375367
let def_id = def_id.expect_local();
376-
let var_id = self
377-
.infcx
378-
.tcx
379-
.typeck(def_id)
380-
.closure_min_captures_flattened(def_id)
381-
.nth(field.index())
382-
.unwrap()
383-
.get_root_variable();
368+
let var_id =
369+
self.infcx.tcx.closure_captures(def_id)[field.index()].get_root_variable();
384370

385371
Some(self.infcx.tcx.hir().name(var_id).to_string())
386372
}
@@ -987,7 +973,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
987973
debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
988974
if let hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, .. }) = expr {
989975
for (captured_place, place) in
990-
self.infcx.tcx.typeck(def_id).closure_min_captures_flattened(def_id).zip(places)
976+
self.infcx.tcx.closure_captures(def_id).iter().zip(places)
991977
{
992978
match place {
993979
Operand::Copy(place) | Operand::Move(place)

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -901,10 +901,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
901901
err: &mut Diagnostic,
902902
) {
903903
let tables = tcx.typeck(closure_local_def_id);
904-
let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_local_def_id);
905-
if let Some((span, closure_kind_origin)) =
906-
&tables.closure_kind_origins().get(closure_hir_id)
907-
{
904+
if let Some((span, closure_kind_origin)) = tcx.closure_kind_origin(closure_local_def_id) {
908905
let reason = if let PlaceBase::Upvar(upvar_id) = closure_kind_origin.base {
909906
let upvar = ty::place_to_string_for_capture(tcx, closure_kind_origin);
910907
let root_hir_id = upvar_id.var_path.hir_id;

compiler/rustc_borrowck/src/type_check/input_output.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
2626
if !self.tcx().is_closure(mir_def_id.to_def_id()) {
2727
return;
2828
}
29-
let Some(user_provided_poly_sig) =
30-
self.tcx().typeck(mir_def_id).user_provided_sigs.get(&mir_def_id)
31-
else {
32-
return;
33-
};
29+
let user_provided_poly_sig = self.tcx().closure_user_provided_sig(mir_def_id);
3430

3531
// Instantiate the canonicalized variables from user-provided signature
3632
// (e.g., the `_` in the code above) with fresh variables.

compiler/rustc_middle/src/arena.rs

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ macro_rules! arena_types {
116116
[] bit_set_u32: rustc_index::bit_set::BitSet<u32>,
117117
[] external_constraints: rustc_middle::traits::solve::ExternalConstraintsData<'tcx>,
118118
[decode] doc_link_resolutions: rustc_hir::def::DocLinkResMap,
119+
[] closure_kind_origin: (rustc_span::Span, rustc_middle::hir::place::Place<'tcx>),
119120
]);
120121
)
121122
}

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ rustc_queries! {
475475
}
476476
}
477477

478-
query closure_captures(key: LocalDefId) -> &'tcx [&'tcx ty::CapturedPlace<'tcx>] {
478+
query closure_typeinfo(key: LocalDefId) -> ty::ClosureTypeInfo<'tcx> {
479479
desc {
480480
|tcx| "finding symbols for captures of closure `{}`",
481481
tcx.def_path_str(key.to_def_id())

compiler/rustc_middle/src/ty/closure.rs

+32-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::{mir, ty};
66
use std::fmt::Write;
77

88
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
9-
use rustc_hir::def::DefKind;
109
use rustc_hir::def_id::{DefId, LocalDefId};
1110
use rustc_hir::{self as hir, LangItem};
1211
use rustc_span::symbol::Ident;
@@ -234,14 +233,39 @@ impl<'tcx> CapturedPlace<'tcx> {
234233
}
235234
}
236235

237-
fn closure_captures<'tcx>(
238-
tcx: TyCtxt<'tcx>,
239-
def: LocalDefId,
240-
) -> &'tcx [&'tcx ty::CapturedPlace<'tcx>] {
241-
let (DefKind::Closure | DefKind::Generator) = tcx.def_kind(def) else { return &[] };
236+
#[derive(Copy, Clone, Debug, HashStable)]
237+
pub struct ClosureTypeInfo<'tcx> {
238+
user_provided_sig: ty::CanonicalPolyFnSig<'tcx>,
239+
captures: &'tcx [&'tcx ty::CapturedPlace<'tcx>],
240+
kind_origin: Option<&'tcx (Span, HirPlace<'tcx>)>,
241+
}
242+
243+
fn closure_typeinfo<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> ClosureTypeInfo<'tcx> {
244+
debug_assert!(tcx.is_closure(def.to_def_id()));
242245
let typeck_results = tcx.typeck(def);
246+
let user_provided_sig = typeck_results.user_provided_sigs[&def];
243247
let captures = typeck_results.closure_min_captures_flattened(def);
244-
tcx.arena.alloc_from_iter(captures)
248+
let captures = tcx.arena.alloc_from_iter(captures);
249+
let hir_id = tcx.hir().local_def_id_to_hir_id(def);
250+
let kind_origin = typeck_results.closure_kind_origins().get(hir_id);
251+
ClosureTypeInfo { user_provided_sig, captures, kind_origin }
252+
}
253+
254+
impl<'tcx> TyCtxt<'tcx> {
255+
pub fn closure_kind_origin(self, def_id: LocalDefId) -> Option<&'tcx (Span, HirPlace<'tcx>)> {
256+
self.closure_typeinfo(def_id).kind_origin
257+
}
258+
259+
pub fn closure_user_provided_sig(self, def_id: LocalDefId) -> ty::CanonicalPolyFnSig<'tcx> {
260+
self.closure_typeinfo(def_id).user_provided_sig
261+
}
262+
263+
pub fn closure_captures(self, def_id: LocalDefId) -> &'tcx [&'tcx ty::CapturedPlace<'tcx>] {
264+
if !self.is_closure(def_id.to_def_id()) {
265+
return &[];
266+
};
267+
self.closure_typeinfo(def_id).captures
268+
}
245269
}
246270

247271
/// Return true if the `proj_possible_ancestor` represents an ancestor path
@@ -434,5 +458,5 @@ impl BorrowKind {
434458
}
435459

436460
pub fn provide(providers: &mut ty::query::Providers) {
437-
*providers = ty::query::Providers { closure_captures, ..*providers }
461+
*providers = ty::query::Providers { closure_typeinfo, ..*providers }
438462
}

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub use self::binding::BindingMode;
7373
pub use self::binding::BindingMode::*;
7474
pub use self::closure::{
7575
is_ancestor_or_same_capture, place_to_string_for_capture, BorrowKind, CaptureInfo,
76-
CapturedPlace, ClosureKind, MinCaptureInformationMap, MinCaptureList,
76+
CapturedPlace, ClosureKind, ClosureTypeInfo, MinCaptureInformationMap, MinCaptureList,
7777
RootVariableMinCaptureList, UpvarCapture, UpvarCaptureMap, UpvarId, UpvarListMap, UpvarPath,
7878
CAPTURE_STRUCT_LOCAL,
7979
};

compiler/rustc_middle/src/ty/typeck_results.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ impl<'a, V> LocalTableInContext<'a, V> {
569569
self.data.contains_key(&id.local_id)
570570
}
571571

572-
pub fn get(&self, id: hir::HirId) -> Option<&V> {
572+
pub fn get(&self, id: hir::HirId) -> Option<&'a V> {
573573
validate_hir_id_for_typeck_results(self.hir_owner, id);
574574
self.data.get(&id.local_id)
575575
}

0 commit comments

Comments
 (0)