Skip to content

Commit 4ccf5f7

Browse files
committed
Auto merge of #77430 - ecstatic-morse:mir-source-in-body, r=lcnr
Store a `MirSource` inside every `Body` Resolves #77427. r? `@ghost`
2 parents a835b48 + 606655e commit 4ccf5f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+327
-380
lines changed

compiler/rustc_middle/src/mir/mod.rs

+42-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ use crate::ty::codec::{TyDecoder, TyEncoder};
1010
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1111
use crate::ty::print::{FmtPrinter, Printer};
1212
use crate::ty::subst::{Subst, SubstsRef};
13-
use crate::ty::{
14-
self, AdtDef, CanonicalUserTypeAnnotations, List, Region, Ty, TyCtxt, UserTypeAnnotationIndex,
15-
};
13+
use crate::ty::{self, List, Ty, TyCtxt};
14+
use crate::ty::{AdtDef, InstanceDef, Region, UserTypeAnnotationIndex};
1615
use rustc_hir as hir;
1716
use rustc_hir::def::{CtorKind, Namespace};
18-
use rustc_hir::def_id::DefId;
17+
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
1918
use rustc_hir::{self, GeneratorKind};
2019
use rustc_target::abi::VariantIdx;
2120

@@ -112,6 +111,38 @@ impl MirPhase {
112111
}
113112
}
114113

114+
/// Where a specific `mir::Body` comes from.
115+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
116+
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable)]
117+
pub struct MirSource<'tcx> {
118+
pub instance: InstanceDef<'tcx>,
119+
120+
/// If `Some`, this is a promoted rvalue within the parent function.
121+
pub promoted: Option<Promoted>,
122+
}
123+
124+
impl<'tcx> MirSource<'tcx> {
125+
pub fn item(def_id: DefId) -> Self {
126+
MirSource {
127+
instance: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)),
128+
promoted: None,
129+
}
130+
}
131+
132+
pub fn from_instance(instance: InstanceDef<'tcx>) -> Self {
133+
MirSource { instance, promoted: None }
134+
}
135+
136+
pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> {
137+
self.instance.with_opt_param()
138+
}
139+
140+
#[inline]
141+
pub fn def_id(&self) -> DefId {
142+
self.instance.def_id()
143+
}
144+
}
145+
115146
/// The lowered representation of a single function.
116147
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
117148
pub struct Body<'tcx> {
@@ -126,6 +157,8 @@ pub struct Body<'tcx> {
126157
/// us to see the difference and forego optimization on the inlined promoted items.
127158
pub phase: MirPhase,
128159

160+
pub source: MirSource<'tcx>,
161+
129162
/// A list of source scopes; these are referenced by statements
130163
/// and used for debuginfo. Indexed by a `SourceScope`.
131164
pub source_scopes: IndexVec<SourceScope, SourceScopeData>,
@@ -151,7 +184,7 @@ pub struct Body<'tcx> {
151184
pub local_decls: LocalDecls<'tcx>,
152185

153186
/// User type annotations.
154-
pub user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
187+
pub user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
155188

156189
/// The number of arguments this function takes.
157190
///
@@ -209,10 +242,11 @@ pub struct Body<'tcx> {
209242

210243
impl<'tcx> Body<'tcx> {
211244
pub fn new(
245+
source: MirSource<'tcx>,
212246
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
213247
source_scopes: IndexVec<SourceScope, SourceScopeData>,
214248
local_decls: LocalDecls<'tcx>,
215-
user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
249+
user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
216250
arg_count: usize,
217251
var_debug_info: Vec<VarDebugInfo<'tcx>>,
218252
span: Span,
@@ -228,6 +262,7 @@ impl<'tcx> Body<'tcx> {
228262

229263
let mut body = Body {
230264
phase: MirPhase::Build,
265+
source,
231266
basic_blocks,
232267
source_scopes,
233268
yield_ty: None,
@@ -257,6 +292,7 @@ impl<'tcx> Body<'tcx> {
257292
pub fn new_cfg_only(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
258293
let mut body = Body {
259294
phase: MirPhase::Build,
295+
source: MirSource::item(DefId::local(CRATE_DEF_INDEX)),
260296
basic_blocks,
261297
source_scopes: IndexVec::new(),
262298
yield_ty: None,

compiler/rustc_middle/src/ty/instance.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pub struct Instance<'tcx> {
2222
pub substs: SubstsRef<'tcx>,
2323
}
2424

25-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable, HashStable)]
25+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
26+
#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable)]
2627
pub enum InstanceDef<'tcx> {
2728
/// A user-defined callable item.
2829
///

compiler/rustc_mir/src/borrow_check/mod.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind
1717
use rustc_middle::mir::{Field, ProjectionElem, Promoted, Rvalue, Statement, StatementKind};
1818
use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
1919
use rustc_middle::ty::query::Providers;
20-
use rustc_middle::ty::{self, InstanceDef, ParamEnv, RegionVid, TyCtxt};
20+
use rustc_middle::ty::{self, ParamEnv, RegionVid, TyCtxt};
2121
use rustc_session::lint::builtin::{MUTABLE_BORROW_RESERVATION_CONFLICT, UNUSED_MUT};
2222
use rustc_span::{Span, Symbol, DUMMY_SP};
2323

@@ -36,7 +36,6 @@ use crate::dataflow::indexes::{BorrowIndex, InitIndex, MoveOutIndex, MovePathInd
3636
use crate::dataflow::move_paths::{InitLocation, LookupResult, MoveData, MoveError};
3737
use crate::dataflow::MoveDataParamEnv;
3838
use crate::dataflow::{Analysis, BorrowckFlowState as Flows, BorrowckResults};
39-
use crate::transform::MirSource;
4039

4140
use self::diagnostics::{AccessKind, RegionName};
4241
use self::location::LocationTable;
@@ -236,13 +235,7 @@ fn do_mir_borrowck<'a, 'tcx>(
236235

237236
// Dump MIR results into a file, if that is enabled. This let us
238237
// write unit-tests, as well as helping with debugging.
239-
nll::dump_mir_results(
240-
infcx,
241-
MirSource { instance: InstanceDef::Item(def.to_global()), promoted: None },
242-
&body,
243-
&regioncx,
244-
&opt_closure_req,
245-
);
238+
nll::dump_mir_results(infcx, &body, &regioncx, &opt_closure_req);
246239

247240
// We also have a `#[rustc_regions]` annotation that causes us to dump
248241
// information.

compiler/rustc_mir/src/borrow_check/nll.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::mir::{
99
BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location,
1010
Promoted,
1111
};
12-
use rustc_middle::ty::{self, InstanceDef, RegionKind, RegionVid};
12+
use rustc_middle::ty::{self, RegionKind, RegionVid};
1313
use rustc_span::symbol::sym;
1414
use std::env;
1515
use std::fmt::Debug;
@@ -24,7 +24,6 @@ use polonius_engine::{Algorithm, Output};
2424
use crate::dataflow::impls::MaybeInitializedPlaces;
2525
use crate::dataflow::move_paths::{InitKind, InitLocation, MoveData};
2626
use crate::dataflow::ResultsCursor;
27-
use crate::transform::MirSource;
2827
use crate::util as mir_util;
2928
use crate::util::pretty;
3029

@@ -72,8 +71,7 @@ pub(in crate::borrow_check) fn replace_regions_in_mir<'cx, 'tcx>(
7271
// Replace all remaining regions with fresh inference variables.
7372
renumber::renumber_mir(infcx, body, promoted);
7473

75-
let source = MirSource { instance: InstanceDef::Item(def.to_global()), promoted: None };
76-
mir_util::dump_mir(infcx.tcx, None, "renumber", &0, source, body, |_, _| Ok(()));
74+
mir_util::dump_mir(infcx.tcx, None, "renumber", &0, body, |_, _| Ok(()));
7775

7876
universal_regions
7977
}
@@ -315,16 +313,15 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
315313

316314
pub(super) fn dump_mir_results<'a, 'tcx>(
317315
infcx: &InferCtxt<'a, 'tcx>,
318-
source: MirSource<'tcx>,
319316
body: &Body<'tcx>,
320317
regioncx: &RegionInferenceContext<'tcx>,
321318
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
322319
) {
323-
if !mir_util::dump_enabled(infcx.tcx, "nll", source.def_id()) {
320+
if !mir_util::dump_enabled(infcx.tcx, "nll", body.source.def_id()) {
324321
return;
325322
}
326323

327-
mir_util::dump_mir(infcx.tcx, None, "nll", &0, source, body, |pass_where, out| {
324+
mir_util::dump_mir(infcx.tcx, None, "nll", &0, body, |pass_where, out| {
328325
match pass_where {
329326
// Before the CFG, dump out the values for each region variable.
330327
PassWhere::BeforeCFG => {
@@ -352,14 +349,14 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
352349
// Also dump the inference graph constraints as a graphviz file.
353350
let _: io::Result<()> = try {
354351
let mut file =
355-
pretty::create_dump_file(infcx.tcx, "regioncx.all.dot", None, "nll", &0, source)?;
352+
pretty::create_dump_file(infcx.tcx, "regioncx.all.dot", None, "nll", &0, body.source)?;
356353
regioncx.dump_graphviz_raw_constraints(&mut file)?;
357354
};
358355

359356
// Also dump the inference graph constraints as a graphviz file.
360357
let _: io::Result<()> = try {
361358
let mut file =
362-
pretty::create_dump_file(infcx.tcx, "regioncx.scc.dot", None, "nll", &0, source)?;
359+
pretty::create_dump_file(infcx.tcx, "regioncx.scc.dot", None, "nll", &0, body.source)?;
363360
regioncx.dump_graphviz_scc_constraints(&mut file)?;
364361
};
365362
}

compiler/rustc_mir/src/shim.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
7878
run_passes(
7979
tcx,
8080
&mut result,
81-
instance,
82-
None,
8381
MirPhase::Const,
8482
&[&[
8583
&add_moves_for_packed_drops::AddMovesForPackedDrops,
@@ -163,7 +161,9 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
163161
block(&mut blocks, TerminatorKind::Goto { target: return_block });
164162
block(&mut blocks, TerminatorKind::Return);
165163

166-
let mut body = new_body(blocks, local_decls_for_sig(&sig, span), sig.inputs().len(), span);
164+
let source = MirSource::from_instance(ty::InstanceDef::DropGlue(def_id, ty));
165+
let mut body =
166+
new_body(source, blocks, local_decls_for_sig(&sig, span), sig.inputs().len(), span);
167167

168168
if let Some(..) = ty {
169169
// The first argument (index 0), but add 1 for the return value.
@@ -202,12 +202,14 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
202202
}
203203

204204
fn new_body<'tcx>(
205+
source: MirSource<'tcx>,
205206
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
206207
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
207208
arg_count: usize,
208209
span: Span,
209210
) -> Body<'tcx> {
210211
Body::new(
212+
source,
211213
basic_blocks,
212214
IndexVec::from_elem_n(
213215
SourceScopeData { span, parent_scope: None, local_data: ClearCrossCrate::Clear },
@@ -344,7 +346,11 @@ impl CloneShimBuilder<'tcx> {
344346
}
345347

346348
fn into_mir(self) -> Body<'tcx> {
347-
new_body(self.blocks, self.local_decls, self.sig.inputs().len(), self.span)
349+
let source = MirSource::from_instance(ty::InstanceDef::CloneShim(
350+
self.def_id,
351+
self.sig.inputs_and_output[0],
352+
));
353+
new_body(source, self.blocks, self.local_decls, self.sig.inputs().len(), self.span)
348354
}
349355

350356
fn source_info(&self) -> SourceInfo {
@@ -834,7 +840,8 @@ fn build_call_shim<'tcx>(
834840
block(&mut blocks, vec![], TerminatorKind::Resume, true);
835841
}
836842

837-
let mut body = new_body(blocks, local_decls, sig.inputs().len(), span);
843+
let mut body =
844+
new_body(MirSource::from_instance(instance), blocks, local_decls, sig.inputs().len(), span);
838845

839846
if let Abi::RustCall = sig.abi {
840847
body.spread_arg = Some(Local::new(sig.inputs().len()));
@@ -897,18 +904,16 @@ pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
897904
is_cleanup: false,
898905
};
899906

900-
let body =
901-
new_body(IndexVec::from_elem_n(start_block, 1), local_decls, sig.inputs().len(), span);
902-
903-
crate::util::dump_mir(
904-
tcx,
905-
None,
906-
"mir_map",
907-
&0,
908-
crate::transform::MirSource::item(ctor_id),
909-
&body,
910-
|_, _| Ok(()),
907+
let source = MirSource::item(ctor_id);
908+
let body = new_body(
909+
source,
910+
IndexVec::from_elem_n(start_block, 1),
911+
local_decls,
912+
sig.inputs().len(),
913+
span,
911914
);
912915

916+
crate::util::dump_mir(tcx, None, "mir_map", &0, &body, |_, _| Ok(()));
917+
913918
body
914919
}

compiler/rustc_mir/src/transform/add_call_guards.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::transform::{MirPass, MirSource};
1+
use crate::transform::MirPass;
22
use rustc_index::vec::{Idx, IndexVec};
33
use rustc_middle::mir::*;
44
use rustc_middle::ty::TyCtxt;
@@ -31,7 +31,7 @@ pub use self::AddCallGuards::*;
3131
*/
3232

3333
impl<'tcx> MirPass<'tcx> for AddCallGuards {
34-
fn run_pass(&self, _tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut Body<'tcx>) {
34+
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3535
self.add_call_guards(body);
3636
}
3737
}

compiler/rustc_mir/src/transform/add_moves_for_packed_drops.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_hir::def_id::DefId;
22
use rustc_middle::mir::*;
33
use rustc_middle::ty::TyCtxt;
44

5-
use crate::transform::{MirPass, MirSource};
5+
use crate::transform::MirPass;
66
use crate::util;
77
use crate::util::patch::MirPatch;
88

@@ -40,9 +40,9 @@ use crate::util::patch::MirPatch;
4040
pub struct AddMovesForPackedDrops;
4141

4242
impl<'tcx> MirPass<'tcx> for AddMovesForPackedDrops {
43-
fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx>) {
44-
debug!("add_moves_for_packed_drops({:?} @ {:?})", src, body.span);
45-
add_moves_for_packed_drops(tcx, body, src.def_id());
43+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
44+
debug!("add_moves_for_packed_drops({:?} @ {:?})", body.source, body.span);
45+
add_moves_for_packed_drops(tcx, body, body.source.def_id());
4646
}
4747
}
4848

compiler/rustc_mir/src/transform/add_retag.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! of MIR building, and only after this pass we think of the program has having the
55
//! normal MIR semantics.
66
7-
use crate::transform::{MirPass, MirSource};
7+
use crate::transform::MirPass;
88
use rustc_middle::mir::*;
99
use rustc_middle::ty::{self, Ty, TyCtxt};
1010

@@ -58,13 +58,13 @@ fn may_be_reference(ty: Ty<'tcx>) -> bool {
5858
}
5959

6060
impl<'tcx> MirPass<'tcx> for AddRetag {
61-
fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx>) {
61+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
6262
if !tcx.sess.opts.debugging_opts.mir_emit_retag {
6363
return;
6464
}
6565

6666
// We need an `AllCallEdges` pass before we can do any work.
67-
super::add_call_guards::AllCallEdges.run_pass(tcx, src, body);
67+
super::add_call_guards::AllCallEdges.run_pass(tcx, body);
6868

6969
let (span, arg_count) = (body.span, body.arg_count);
7070
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();

compiler/rustc_mir/src/transform/check_const_item_mutation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use rustc_middle::ty::TyCtxt;
66
use rustc_session::lint::builtin::CONST_ITEM_MUTATION;
77
use rustc_span::def_id::DefId;
88

9-
use crate::transform::{MirPass, MirSource};
9+
use crate::transform::MirPass;
1010

1111
pub struct CheckConstItemMutation;
1212

1313
impl<'tcx> MirPass<'tcx> for CheckConstItemMutation {
14-
fn run_pass(&self, tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut Body<'tcx>) {
14+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1515
let mut checker = ConstMutationChecker { body, tcx, target_local: None };
1616
checker.visit_body(&body);
1717
}

compiler/rustc_mir/src/transform/check_packed_ref.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use rustc_middle::mir::*;
33
use rustc_middle::ty::{self, TyCtxt};
44
use rustc_session::lint::builtin::UNALIGNED_REFERENCES;
55

6-
use crate::transform::{MirPass, MirSource};
6+
use crate::transform::MirPass;
77
use crate::util;
88

99
pub struct CheckPackedRef;
1010

1111
impl<'tcx> MirPass<'tcx> for CheckPackedRef {
12-
fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx>) {
13-
let param_env = tcx.param_env(src.instance.def_id());
12+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
13+
let param_env = tcx.param_env(body.source.def_id());
1414
let source_info = SourceInfo::outermost(body.span);
1515
let mut checker = PackedRefChecker { body, tcx, param_env, source_info };
1616
checker.visit_body(&body);

0 commit comments

Comments
 (0)