Skip to content

Commit

Permalink
Record synthetic MIR bodies in mir_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Sep 11, 2024
1 parent 140aa73 commit 4af4369
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
.set_some(def_id.index, coroutine_for_closure.into());

// If this async closure has a by-move body, record it too.
if tcx.needs_coroutine_by_move_body_def_id(coroutine_for_closure) {
if tcx.needs_coroutine_by_move_body_def_id(coroutine_for_closure.expect_local()) {
self.tables.coroutine_by_move_body_def_id.set_some(
coroutine_for_closure.index,
self.tcx.coroutine_by_move_body_def_id(coroutine_for_closure).into(),
Expand Down
36 changes: 20 additions & 16 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ use rustc_const_eval::util;
use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::steal::Steal;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def::{CtorKind, DefKind};
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_index::IndexVec;
use rustc_middle::mir::{
AnalysisPhase, Body, CallSource, ClearCrossCrate, ConstOperand, ConstQualifs, LocalDecl,
Expand Down Expand Up @@ -224,26 +223,31 @@ fn is_mir_available(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
/// MIR associated with them.
fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {
// All body-owners have MIR associated with them.
let set: FxIndexSet<_> = tcx.hir().body_owners().collect();
let mut set: FxIndexSet<_> = tcx.hir().body_owners().collect();

// Additionally, tuple struct/variant constructors have MIR, but
// they don't have a BodyId, so we need to build them separately.
struct GatherCtors {
set: FxIndexSet<LocalDefId>,
// Coroutine-closures (e.g. async closures) have an additional by-move MIR
// body that isn't in the HIR.
for body_owner in tcx.hir().body_owners() {
if let DefKind::Closure = tcx.def_kind(body_owner)
&& tcx.needs_coroutine_by_move_body_def_id(body_owner)
{
set.insert(tcx.coroutine_by_move_body_def_id(body_owner).expect_local());
}
}
impl<'tcx> Visitor<'tcx> for GatherCtors {
fn visit_variant_data(&mut self, v: &'tcx hir::VariantData<'tcx>) {
if let hir::VariantData::Tuple(_, _, def_id) = *v {
self.set.insert(def_id);

// tuple struct/variant constructors have MIR, but they don't have a BodyId,
// so we need to build them separately.
for item in tcx.hir_crate_items(()).free_items() {
if let DefKind::Struct | DefKind::Enum = tcx.def_kind(item.owner_id) {
for variant in tcx.adt_def(item.owner_id).variants() {
if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor {
set.insert(ctor_def_id.expect_local());
}
}
intravisit::walk_struct_def(self, v)
}
}

let mut gather_ctors = GatherCtors { set };
tcx.hir().visit_all_item_likes_in_crate(&mut gather_ctors);

gather_ctors.set
set
}

fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
Expand Down

0 comments on commit 4af4369

Please sign in to comment.