Skip to content

Commit 7d5b2e4

Browse files
committed
Make closure_saved_names_of_captured_variables a query.
1 parent 18a6d91 commit 7d5b2e4

File tree

13 files changed

+71
-39
lines changed

13 files changed

+71
-39
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ fn build_upvar_field_di_nodes<'ll, 'tcx>(
10311031
build_field_di_node(
10321032
cx,
10331033
closure_or_generator_di_node,
1034-
capture_name,
1034+
capture_name.as_str(),
10351035
cx.size_and_align_of(up_var_ty),
10361036
layout.fields.offset(index),
10371037
DIFlags::FlagZero,

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ pub fn build_generator_variant_struct_type_di_node<'ll, 'tcx>(
324324
generator_type_di_node: &'ll DIType,
325325
generator_layout: &GeneratorLayout<'tcx>,
326326
state_specific_upvar_names: &IndexSlice<GeneratorSavedLocal, Option<Symbol>>,
327-
common_upvar_names: &[String],
327+
common_upvar_names: &IndexSlice<FieldIdx, Symbol>,
328328
) -> &'ll DIType {
329329
let variant_name = GeneratorSubsts::variant_name(variant_index);
330330
let unique_type_id = UniqueTypeId::for_enum_variant_struct_type(
@@ -380,12 +380,13 @@ pub fn build_generator_variant_struct_type_di_node<'ll, 'tcx>(
380380
// Fields that are common to all states
381381
let common_fields: SmallVec<_> = generator_substs
382382
.prefix_tys()
383+
.zip(common_upvar_names)
383384
.enumerate()
384-
.map(|(index, upvar_ty)| {
385+
.map(|(index, (upvar_ty, upvar_name))| {
385386
build_field_di_node(
386387
cx,
387388
variant_struct_type_di_node,
388-
&common_upvar_names[index],
389+
upvar_name.as_str(),
389390
cx.size_and_align_of(upvar_ty),
390391
generator_type_and_layout.fields.offset(index),
391392
DIFlags::FlagZero,

compiler/rustc_data_structures/src/stable_hasher.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::sip128::SipHasher128;
22
use rustc_index::bit_set::{self, BitSet};
3-
use rustc_index::{Idx, IndexVec};
3+
use rustc_index::{Idx, IndexSlice, IndexVec};
44
use smallvec::SmallVec;
55
use std::fmt;
66
use std::hash::{BuildHasher, Hash, Hasher};
@@ -597,6 +597,18 @@ where
597597
}
598598
}
599599

600+
impl<I: Idx, T, CTX> HashStable<CTX> for IndexSlice<I, T>
601+
where
602+
T: HashStable<CTX>,
603+
{
604+
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
605+
self.len().hash_stable(ctx, hasher);
606+
for v in &self.raw {
607+
v.hash_stable(ctx, hasher);
608+
}
609+
}
610+
}
611+
600612
impl<I: Idx, T, CTX> HashStable<CTX> for IndexVec<I, T>
601613
where
602614
T: HashStable<CTX>,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ provide! { tcx, def_id, other, cdata,
218218
thir_abstract_const => { table }
219219
optimized_mir => { table }
220220
mir_for_ctfe => { table }
221+
closure_saved_names_of_captured_variables => { table }
221222
mir_generator_witnesses => { table }
222223
promoted_mir => { table }
223224
def_span => { table }

compiler/rustc_metadata/src/rmeta/encoder.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15201520
debug!("EntryBuilder::encode_mir({:?})", def_id);
15211521
if encode_opt {
15221522
record!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id));
1523+
record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()]
1524+
<- tcx.closure_saved_names_of_captured_variables(def_id));
15231525

15241526
if tcx.sess.opts.unstable_opts.drop_tracking_mir
15251527
&& let DefKind::Generator = self.tcx.def_kind(def_id)

compiler/rustc_metadata/src/rmeta/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_span::edition::Edition;
3232
use rustc_span::hygiene::{ExpnIndex, MacroKind};
3333
use rustc_span::symbol::{Ident, Symbol};
3434
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Span};
35-
use rustc_target::abi::VariantIdx;
35+
use rustc_target::abi::{FieldIdx, VariantIdx};
3636
use rustc_target::spec::{PanicStrategy, TargetTriple};
3737

3838
use std::marker::PhantomData;
@@ -416,6 +416,7 @@ define_tables! {
416416
object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>,
417417
optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>,
418418
mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'static>>>,
419+
closure_saved_names_of_captured_variables: Table<DefIndex, LazyValue<IndexVec<FieldIdx, Symbol>>>,
419420
mir_generator_witnesses: Table<DefIndex, LazyValue<mir::GeneratorLayout<'static>>>,
420421
promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
421422
thir_abstract_const: Table<DefIndex, LazyValue<ty::EarlyBinder<ty::Const<'static>>>>,

compiler/rustc_middle/src/arena.rs

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ macro_rules! arena_types {
2727
rustc_middle::mir::Promoted,
2828
rustc_middle::mir::Body<'tcx>
2929
>,
30+
[decode] closure_debuginfo:
31+
rustc_index::IndexVec<
32+
rustc_target::abi::FieldIdx,
33+
rustc_span::symbol::Symbol,
34+
>,
3035
[decode] typeck_results: rustc_middle::ty::TypeckResults<'tcx>,
3136
[decode] borrowck_result:
3237
rustc_middle::mir::BorrowCheckResult<'tcx>,

compiler/rustc_middle/src/query/erase.rs

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ impl<T> EraseType for &'_ ty::List<T> {
5555
type Result = [u8; size_of::<*const ()>()];
5656
}
5757

58+
impl<I: rustc_index::Idx, T> EraseType for &'_ rustc_index::IndexSlice<I, T> {
59+
type Result = [u8; size_of::<&'static rustc_index::IndexSlice<u32, ()>>()];
60+
}
61+
5862
impl<T> EraseType for Result<&'_ T, traits::query::NoSolution> {
5963
type Result = [u8; size_of::<Result<&'static (), traits::query::NoSolution>>()];
6064
}

compiler/rustc_middle/src/query/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,19 @@ rustc_queries! {
531531
}
532532
}
533533

534+
/// Returns names of captured upvars for closures and generators.
535+
///
536+
/// Here are some examples:
537+
/// - `name__field1__field2` when the upvar is captured by value.
538+
/// - `_ref__name__field` when the upvar is captured by reference.
539+
///
540+
/// For generators this only contains upvars that are shared by all states.
541+
query closure_saved_names_of_captured_variables(def_id: DefId) -> &'tcx IndexVec<abi::FieldIdx, Symbol> {
542+
arena_cache
543+
desc { |tcx| "computing debuginfo for closure `{}`", tcx.def_path_str(def_id) }
544+
separate_provide_extern
545+
}
546+
534547
query mir_generator_witnesses(key: DefId) -> &'tcx Option<mir::GeneratorLayout<'tcx>> {
535548
arena_cache
536549
desc { |tcx| "generator witness types for `{}`", tcx.def_path_str(key) }

compiler/rustc_middle/src/ty/util.rs

-32
Original file line numberDiff line numberDiff line change
@@ -738,38 +738,6 @@ impl<'tcx> TyCtxt<'tcx> {
738738
if visitor.found_recursion { Err(expanded_type) } else { Ok(expanded_type) }
739739
}
740740

741-
/// Returns names of captured upvars for closures and generators.
742-
///
743-
/// Here are some examples:
744-
/// - `name__field1__field2` when the upvar is captured by value.
745-
/// - `_ref__name__field` when the upvar is captured by reference.
746-
///
747-
/// For generators this only contains upvars that are shared by all states.
748-
pub fn closure_saved_names_of_captured_variables(
749-
self,
750-
def_id: DefId,
751-
) -> SmallVec<[String; 16]> {
752-
let body = self.optimized_mir(def_id);
753-
754-
body.var_debug_info
755-
.iter()
756-
.filter_map(|var| {
757-
let is_ref = match var.value {
758-
mir::VarDebugInfoContents::Place(place)
759-
if place.local == mir::Local::new(1) =>
760-
{
761-
// The projection is either `[.., Field, Deref]` or `[.., Field]`. It
762-
// implies whether the variable is captured by value or by reference.
763-
matches!(place.projection.last().unwrap(), mir::ProjectionElem::Deref)
764-
}
765-
_ => return None,
766-
};
767-
let prefix = if is_ref { "_ref__" } else { "" };
768-
Some(prefix.to_owned() + var.name.as_str())
769-
})
770-
.collect()
771-
}
772-
773741
// FIXME(eddyb) maybe precompute this? Right now it's computed once
774742
// per generator monomorphization, but it doesn't depend on substs.
775743
pub fn generator_layout_and_saved_local_names(

compiler/rustc_mir_build/src/build/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ pub(crate) fn mir_built(
3636
tcx.alloc_steal_mir(mir_build(tcx, def))
3737
}
3838

39+
/// Returns names of captured upvars for closures and generators.
40+
///
41+
/// Here are some examples:
42+
/// - `name__field1__field2` when the upvar is captured by value.
43+
/// - `_ref__name__field` when the upvar is captured by reference.
44+
///
45+
/// For generators this only contains upvars that are shared by all states.
46+
pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
47+
tcx: TyCtxt<'tcx>,
48+
def_id: LocalDefId,
49+
) -> IndexVec<FieldIdx, Symbol> {
50+
tcx.closure_captures(def_id)
51+
.iter()
52+
.map(|captured_place| {
53+
let name = captured_place.to_symbol();
54+
match captured_place.info.capture_kind {
55+
ty::UpvarCapture::ByValue => name,
56+
ty::UpvarCapture::ByRef(..) => Symbol::intern(&format!("_ref__{name}")),
57+
}
58+
})
59+
.collect()
60+
}
61+
3962
/// Construct the MIR for a given `DefId`.
4063
fn mir_build(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
4164
// Ensure unsafeck and abstract const building is ran before we steal the THIR.

compiler/rustc_mir_build/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub fn provide(providers: &mut Providers) {
3333
providers.check_match = thir::pattern::check_match;
3434
providers.lit_to_const = thir::constant::lit_to_const;
3535
providers.mir_built = build::mir_built;
36+
providers.closure_saved_names_of_captured_variables =
37+
build::closure_saved_names_of_captured_variables;
3638
providers.thir_check_unsafety = check_unsafety::thir_check_unsafety;
3739
providers.thir_body = thir::cx::thir_body;
3840
providers.thir_tree = thir::print::thir_tree;

compiler/rustc_ty_utils/src/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ fn variant_info_for_generator<'tcx>(
959959
upvars_size = upvars_size.max(offset + field_layout.size);
960960
FieldInfo {
961961
kind: FieldKind::Upvar,
962-
name: Symbol::intern(&name),
962+
name: *name,
963963
offset: offset.bytes(),
964964
size: field_layout.size.bytes(),
965965
align: field_layout.align.abi.bytes(),

0 commit comments

Comments
 (0)