Skip to content

Commit cf5eda1

Browse files
committed
Add a query for CapturedPlace::to_symbol
1 parent 0cb6f07 commit cf5eda1

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

compiler/rustc_middle/src/dep_graph/dep_node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
285285
// required that their size stay the same, but we don't want to change
286286
// it inadvertently. This assert just ensures we're aware of any change.
287287
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
288-
static_assert_size!(DepNode, 17);
288+
static_assert_size!(DepNode, 18);
289289

290290
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
291291
static_assert_size!(DepNode, 24);

compiler/rustc_middle/src/query/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ rustc_queries! {
329329
}
330330
}
331331

332+
query symbols_for_closure_captures(
333+
key: (LocalDefId, DefId)
334+
) -> Vec<rustc_span::Symbol> {
335+
desc {
336+
|tcx| "symbols for captures of closure `{}` in `{}`",
337+
tcx.def_path_str(key.1),
338+
tcx.def_path_str(key.0.to_def_id())
339+
}
340+
}
341+
332342
/// MIR after our optimization passes have run. This is MIR that is ready
333343
/// for codegen. This is also the only query that can fetch non-local MIR, at present.
334344
query optimized_mir(key: DefId) -> &'tcx mir::Body<'tcx> {

compiler/rustc_middle/src/ty/closure.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl CapturedPlace<'tcx> {
162162
}
163163

164164
/// Returns a symbol of the captured upvar, which looks like `name__field1__field2`.
165-
pub fn to_symbol(&self, tcx: TyCtxt<'tcx>) -> Symbol {
165+
fn to_symbol(&self, tcx: TyCtxt<'tcx>) -> Symbol {
166166
let hir_id = match self.place.base {
167167
HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
168168
base => bug!("Expected an upvar, found {:?}", base),
@@ -248,6 +248,15 @@ impl CapturedPlace<'tcx> {
248248
}
249249
}
250250

251+
fn symbols_for_closure_captures<'tcx>(
252+
tcx: TyCtxt<'tcx>,
253+
def_id: (LocalDefId, DefId),
254+
) -> Vec<Symbol> {
255+
let typeck_results = tcx.typeck(def_id.0);
256+
let captures = typeck_results.closure_min_captures_flattened(def_id.1);
257+
captures.into_iter().map(|captured_place| captured_place.to_symbol(tcx)).collect()
258+
}
259+
251260
/// Return true if the `proj_possible_ancestor` represents an ancestor path
252261
/// to `proj_capture` or `proj_possible_ancestor` is same as `proj_capture`,
253262
/// assuming they both start off of the same root variable.
@@ -432,3 +441,7 @@ impl BorrowKind {
432441
}
433442
}
434443
}
444+
445+
pub fn provide(providers: &mut ty::query::Providers) {
446+
*providers = ty::query::Providers { symbols_for_closure_captures, ..*providers }
447+
}

compiler/rustc_middle/src/ty/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub use self::IntVarValue::*;
1616
pub use self::Variance::*;
1717
pub use adt::*;
1818
pub use assoc::*;
19-
pub use closure::*;
2019
pub use generics::*;
2120
pub use vtable::*;
2221

@@ -55,6 +54,12 @@ pub use rustc_type_ir::*;
5554

5655
pub use self::binding::BindingMode;
5756
pub use self::binding::BindingMode::*;
57+
pub use self::closure::{
58+
is_ancestor_or_same_capture, place_to_string_for_capture, BorrowKind, CaptureInfo,
59+
CapturedPlace, ClosureKind, MinCaptureInformationMap, MinCaptureList,
60+
RootVariableMinCaptureList, UpvarBorrow, UpvarCapture, UpvarCaptureMap, UpvarId, UpvarListMap,
61+
UpvarPath, CAPTURE_STRUCT_LOCAL,
62+
};
5863
pub use self::consts::{Const, ConstInt, ConstKind, InferConst, ScalarInt, Unevaluated, ValTree};
5964
pub use self::context::{
6065
tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
@@ -1979,6 +1984,7 @@ pub fn ast_uint_ty(uty: UintTy) -> ast::UintTy {
19791984
}
19801985

19811986
pub fn provide(providers: &mut ty::query::Providers) {
1987+
closure::provide(providers);
19821988
context::provide(providers);
19831989
erase_regions::provide(providers);
19841990
layout::provide(providers);

compiler/rustc_mir_build/src/build/mod.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -959,13 +959,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
959959
ty::Generator(_, substs, _) => ty::UpvarSubsts::Generator(substs),
960960
_ => span_bug!(self.fn_span, "upvars with non-closure env ty {:?}", closure_ty),
961961
};
962+
let def_id = self.def_id.as_local().unwrap();
963+
let capture_syms = tcx.symbols_for_closure_captures((def_id, fn_def_id));
962964
let capture_tys = upvar_substs.upvar_tys();
963-
let captures_with_tys =
964-
hir_typeck_results.closure_min_captures_flattened(fn_def_id).zip(capture_tys);
965+
let captures_with_tys = hir_typeck_results
966+
.closure_min_captures_flattened(fn_def_id)
967+
.zip(capture_tys.zip(capture_syms));
965968

966969
self.upvar_mutbls = captures_with_tys
967970
.enumerate()
968-
.map(|(i, (captured_place, ty))| {
971+
.map(|(i, (captured_place, (ty, sym)))| {
969972
let capture = captured_place.info.capture_kind;
970973
let var_id = match captured_place.place.base {
971974
HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
@@ -974,8 +977,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
974977

975978
let mutability = captured_place.mutability;
976979

977-
let name = captured_place.to_symbol(tcx);
978-
979980
let mut projs = closure_env_projs.clone();
980981
projs.push(ProjectionElem::Field(Field::new(i), ty));
981982
match capture {
@@ -986,7 +987,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
986987
};
987988

988989
self.var_debug_info.push(VarDebugInfo {
989-
name,
990+
name: sym,
990991
source_info: SourceInfo::outermost(tcx_hir.span(var_id)),
991992
value: VarDebugInfoContents::Place(Place {
992993
local: ty::CAPTURE_STRUCT_LOCAL,

0 commit comments

Comments
 (0)