Skip to content

Commit d3e2c93

Browse files
committed
Use trait object references for closures.
The dynamic dispatch cost doesn't matter for MIR dumping, which is perf-insensitive. And it's necessary for the next commit, which will store some `extra_data` closures in a struct.
1 parent d7faa56 commit d3e2c93

File tree

10 files changed

+39
-59
lines changed

10 files changed

+39
-59
lines changed

compiler/rustc_borrowck/src/nll.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub(crate) fn replace_regions_in_mir<'tcx>(
6868
// Replace all remaining regions with fresh inference variables.
6969
renumber::renumber_mir(infcx, body, promoted);
7070

71-
dump_mir(infcx.tcx, false, "renumber", &0, body, |_, _| Ok(()));
71+
dump_mir(infcx.tcx, false, "renumber", &0, body, &|_, _| Ok(()));
7272

7373
universal_regions
7474
}
@@ -194,7 +194,7 @@ pub(super) fn dump_nll_mir<'tcx>(
194194
"nll",
195195
&0,
196196
body,
197-
|pass_where, out| {
197+
&|pass_where, out| {
198198
emit_nll_mir(tcx, regioncx, closure_region_requirements, borrow_set, pass_where, out)
199199
},
200200
options,

compiler/rustc_borrowck/src/polonius/dump.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ fn emit_html_mir<'tcx>(
175175
&0,
176176
body,
177177
&mut buffer,
178-
|pass_where, out| {
178+
&|pass_where, out| {
179179
emit_polonius_mir(
180180
tcx,
181181
regioncx,

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,14 @@ impl PrettyPrintMirOptions {
8383
/// - `foo & nll | bar & typeck` == match if `foo` and `nll` both appear in the name
8484
/// or `typeck` and `bar` both appear in the name.
8585
#[inline]
86-
pub fn dump_mir<'tcx, F>(
86+
pub fn dump_mir<'tcx>(
8787
tcx: TyCtxt<'tcx>,
8888
pass_num: bool,
8989
pass_name: &str,
9090
disambiguator: &dyn Display,
9191
body: &Body<'tcx>,
92-
extra_data: F,
93-
) where
94-
F: Fn(PassWhere, &mut dyn io::Write) -> io::Result<()>,
95-
{
92+
extra_data: &dyn Fn(PassWhere, &mut dyn io::Write) -> io::Result<()>,
93+
) {
9694
dump_mir_with_options(
9795
tcx,
9896
pass_num,
@@ -110,17 +108,15 @@ pub fn dump_mir<'tcx, F>(
110108
/// See [`dump_mir`] for more details.
111109
///
112110
#[inline]
113-
pub fn dump_mir_with_options<'tcx, F>(
111+
pub fn dump_mir_with_options<'tcx>(
114112
tcx: TyCtxt<'tcx>,
115113
pass_num: bool,
116114
pass_name: &str,
117115
disambiguator: &dyn Display,
118116
body: &Body<'tcx>,
119-
extra_data: F,
117+
extra_data: &dyn Fn(PassWhere, &mut dyn io::Write) -> io::Result<()>,
120118
options: PrettyPrintMirOptions,
121-
) where
122-
F: Fn(PassWhere, &mut dyn io::Write) -> io::Result<()>,
123-
{
119+
) {
124120
if !dump_enabled(tcx, pass_name, body.source.def_id()) {
125121
return;
126122
}
@@ -165,18 +161,15 @@ pub fn dump_enabled(tcx: TyCtxt<'_>, pass_name: &str, def_id: DefId) -> bool {
165161
/// most of the MIR dumping occurs, if one needs to export it to a file they have created with
166162
/// [create_dump_file], rather than to a new file created as part of [dump_mir], or to stdout/stderr
167163
/// for debugging purposes.
168-
pub fn dump_mir_to_writer<'tcx, F>(
164+
pub fn dump_mir_to_writer<'tcx>(
169165
tcx: TyCtxt<'tcx>,
170166
pass_name: &str,
171167
disambiguator: &dyn Display,
172168
body: &Body<'tcx>,
173169
w: &mut dyn io::Write,
174-
extra_data: F,
170+
extra_data: &dyn Fn(PassWhere, &mut dyn io::Write) -> io::Result<()>,
175171
options: PrettyPrintMirOptions,
176-
) -> io::Result<()>
177-
where
178-
F: Fn(PassWhere, &mut dyn io::Write) -> io::Result<()>,
179-
{
172+
) -> io::Result<()> {
180173
// see notes on #41697 above
181174
let def_path =
182175
ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
@@ -193,7 +186,7 @@ where
193186
writeln!(w)?;
194187
extra_data(PassWhere::BeforeCFG, w)?;
195188
write_user_type_annotations(tcx, body, w)?;
196-
write_mir_fn(tcx, body, &extra_data, w, options)?;
189+
write_mir_fn(tcx, body, extra_data, w, options)?;
197190
extra_data(PassWhere::AfterCFG, w)
198191
}
199192

@@ -369,16 +362,13 @@ pub fn write_mir_pretty<'tcx>(
369362
}
370363

371364
/// Write out a human-readable textual representation for the given function.
372-
pub fn write_mir_fn<'tcx, F>(
365+
pub fn write_mir_fn<'tcx>(
373366
tcx: TyCtxt<'tcx>,
374367
body: &Body<'tcx>,
375-
extra_data: &F,
368+
extra_data: &dyn Fn(PassWhere, &mut dyn io::Write) -> io::Result<()>,
376369
w: &mut dyn io::Write,
377370
options: PrettyPrintMirOptions,
378-
) -> io::Result<()>
379-
where
380-
F: Fn(PassWhere, &mut dyn io::Write) -> io::Result<()>,
381-
{
371+
) -> io::Result<()> {
382372
write_mir_intro(tcx, body, w, options)?;
383373
for block in body.basic_blocks.indices() {
384374
extra_data(PassWhere::BeforeBlock(block), w)?;
@@ -706,17 +696,14 @@ pub fn dump_mir_def_ids(tcx: TyCtxt<'_>, single: Option<DefId>) -> Vec<DefId> {
706696
// Basic blocks and their parts (statements, terminators, ...)
707697

708698
/// Write out a human-readable textual representation for the given basic block.
709-
fn write_basic_block<'tcx, F>(
699+
fn write_basic_block<'tcx>(
710700
tcx: TyCtxt<'tcx>,
711701
block: BasicBlock,
712702
body: &Body<'tcx>,
713-
extra_data: &F,
703+
extra_data: &dyn Fn(PassWhere, &mut dyn io::Write) -> io::Result<()>,
714704
w: &mut dyn io::Write,
715705
options: PrettyPrintMirOptions,
716-
) -> io::Result<()>
717-
where
718-
F: Fn(PassWhere, &mut dyn io::Write) -> io::Result<()>,
719-
{
706+
) -> io::Result<()> {
720707
let data = &body[block];
721708

722709
// Basic block label at the top.
@@ -748,9 +735,7 @@ where
748735
write_extra(
749736
tcx,
750737
w,
751-
|visitor| {
752-
visitor.visit_statement(statement, current_location);
753-
},
738+
&|visitor| visitor.visit_statement(statement, current_location),
754739
options,
755740
)?;
756741

@@ -783,9 +768,7 @@ where
783768
write_extra(
784769
tcx,
785770
w,
786-
|visitor| {
787-
visitor.visit_terminator(data.terminator(), current_location);
788-
},
771+
&|visitor| visitor.visit_terminator(data.terminator(), current_location),
789772
options,
790773
)?;
791774
}
@@ -1360,15 +1343,12 @@ fn post_fmt_projection(projection: &[PlaceElem<'_>], fmt: &mut Formatter<'_>) ->
13601343
/// After we print the main statement, we sometimes dump extra
13611344
/// information. There's often a lot of little things "nuzzled up" in
13621345
/// a statement.
1363-
fn write_extra<'tcx, F>(
1346+
fn write_extra<'tcx>(
13641347
tcx: TyCtxt<'tcx>,
13651348
write: &mut dyn io::Write,
1366-
visit_op: F,
1349+
visit_op: &dyn Fn(&mut ExtraComments<'tcx>),
13671350
options: PrettyPrintMirOptions,
1368-
) -> io::Result<()>
1369-
where
1370-
F: Fn(&mut ExtraComments<'tcx>),
1371-
{
1351+
) -> io::Result<()> {
13721352
if options.include_extra_comments {
13731353
let mut extra_comments = ExtraComments { tcx, comments: vec![] };
13741354
visit_op(&mut extra_comments);

compiler/rustc_mir_transform/src/coroutine.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ fn create_coroutine_resume_function<'tcx>(
12941294

12951295
pm::run_passes_no_validate(tcx, body, &[&abort_unwinding_calls::AbortUnwindingCalls], None);
12961296

1297-
dump_mir(tcx, false, "coroutine_resume", &0, body, |_, _| Ok(()));
1297+
dump_mir(tcx, false, "coroutine_resume", &0, body, &|_, _| Ok(()));
12981298
}
12991299

13001300
/// An operation that can be performed on a coroutine.
@@ -1446,7 +1446,7 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
14461446

14471447
assert!(body.coroutine_drop().is_none() && body.coroutine_drop_async().is_none());
14481448

1449-
dump_mir(tcx, false, "coroutine_before", &0, body, |_, _| Ok(()));
1449+
dump_mir(tcx, false, "coroutine_before", &0, body, &|_, _| Ok(()));
14501450

14511451
// The first argument is the coroutine type passed by value
14521452
let coroutine_ty = body.local_decls.raw[1].ty;
@@ -1506,7 +1506,7 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
15061506
) {
15071507
let context_mut_ref = transform_async_context(tcx, body);
15081508
expand_async_drops(tcx, body, context_mut_ref, coroutine_kind, coroutine_ty);
1509-
dump_mir(tcx, false, "coroutine_async_drop_expand", &0, body, |_, _| Ok(()));
1509+
dump_mir(tcx, false, "coroutine_async_drop_expand", &0, body, &|_, _| Ok(()));
15101510
} else {
15111511
cleanup_async_drops(body);
15121512
}
@@ -1605,14 +1605,14 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
16051605
// This is expanded to a drop ladder in `elaborate_coroutine_drops`.
16061606
let drop_clean = insert_clean_drop(tcx, body, has_async_drops);
16071607

1608-
dump_mir(tcx, false, "coroutine_pre-elab", &0, body, |_, _| Ok(()));
1608+
dump_mir(tcx, false, "coroutine_pre-elab", &0, body, &|_, _| Ok(()));
16091609

16101610
// Expand `drop(coroutine_struct)` to a drop ladder which destroys upvars.
16111611
// If any upvars are moved out of, drop elaboration will handle upvar destruction.
16121612
// However we need to also elaborate the code generated by `insert_clean_drop`.
16131613
elaborate_coroutine_drops(tcx, body);
16141614

1615-
dump_mir(tcx, false, "coroutine_post-transform", &0, body, |_, _| Ok(()));
1615+
dump_mir(tcx, false, "coroutine_post-transform", &0, body, &|_, _| Ok(()));
16161616

16171617
let can_unwind = can_unwind(tcx, body);
16181618

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ pub(crate) fn coroutine_by_move_body_def_id<'tcx>(
225225
);
226226
by_move_body.source =
227227
mir::MirSource::from_instance(InstanceKind::Item(body_def.def_id().to_def_id()));
228-
dump_mir(tcx, false, "built", &"after", &by_move_body, |_, _| Ok(()));
228+
dump_mir(tcx, false, "built", &"after", &by_move_body, &|_, _| Ok(()));
229229

230230
// Feed HIR because we try to access this body's attrs in the inliner.
231231
body_def.feed_hir();

compiler/rustc_mir_transform/src/coroutine/drop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ pub(super) fn create_coroutine_drop_shim<'tcx>(
605605
// Temporary change MirSource to coroutine's instance so that dump_mir produces more sensible
606606
// filename.
607607
body.source.instance = coroutine_instance;
608-
dump_mir(tcx, false, "coroutine_drop", &0, &body, |_, _| Ok(()));
608+
dump_mir(tcx, false, "coroutine_drop", &0, &body, &|_, _| Ok(()));
609609
body.source.instance = drop_instance;
610610

611611
// Creating a coroutine drop shim happens on `Analysis(PostCleanup) -> Runtime(Initial)`
@@ -696,7 +696,7 @@ pub(super) fn create_coroutine_drop_shim_async<'tcx>(
696696
None,
697697
);
698698

699-
dump_mir(tcx, false, "coroutine_drop_async", &0, &body, |_, _| Ok(()));
699+
dump_mir(tcx, false, "coroutine_drop_async", &0, &body, &|_, _| Ok(()));
700700

701701
body
702702
}
@@ -741,7 +741,7 @@ pub(super) fn create_coroutine_drop_shim_proxy_async<'tcx>(
741741
};
742742
body.basic_blocks_mut()[call_bb].terminator = Some(Terminator { source_info, kind });
743743

744-
dump_mir(tcx, false, "coroutine_drop_proxy_async", &0, &body, |_, _| Ok(()));
744+
dump_mir(tcx, false, "coroutine_drop_proxy_async", &0, &body, &|_, _| Ok(()));
745745

746746
body
747747
}

compiler/rustc_mir_transform/src/dest_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ fn dest_prop_mir_dump<'tcx>(
810810
let location = points.point_from_location(location);
811811
live.rows().filter(|&r| live.contains(r, location)).collect::<Vec<_>>()
812812
};
813-
dump_mir(tcx, false, "DestinationPropagation-dataflow", &round, body, |pass_where, w| {
813+
dump_mir(tcx, false, "DestinationPropagation-dataflow", &round, body, &|pass_where, w| {
814814
if let PassWhere::BeforeLocation(loc) = pass_where {
815815
writeln!(w, " // live: {:?}", locals_live_at(loc))?;
816816
}

compiler/rustc_mir_transform/src/lint_tail_expr_drop_order.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<
227227
return;
228228
}
229229

230-
dump_mir(tcx, false, "lint_tail_expr_drop_order", &0 as _, body, |_, _| Ok(()));
230+
dump_mir(tcx, false, "lint_tail_expr_drop_order", &0 as _, body, &|_, _| Ok(()));
231231
let locals_with_user_names = collect_user_names(body);
232232
let is_closure_like = tcx.is_closure_like(def_id.to_def_id());
233233

compiler/rustc_mir_transform/src/pass_manager.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ fn run_passes_inner<'tcx>(
291291
let dump_enabled = pass.is_mir_dump_enabled();
292292

293293
if dump_enabled {
294-
mir::dump_mir(tcx, pass_num, pass_name, &"before", body, |_, _| Ok(()));
294+
mir::dump_mir(tcx, pass_num, pass_name, &"before", body, &|_, _| Ok(()));
295295
}
296296

297297
if let Some(prof_arg) = &prof_arg {
@@ -304,7 +304,7 @@ fn run_passes_inner<'tcx>(
304304
}
305305

306306
if dump_enabled {
307-
mir::dump_mir(tcx, pass_num, pass_name, &"after", body, |_, _| Ok(()));
307+
mir::dump_mir(tcx, pass_num, pass_name, &"after", body, &|_, _| Ok(()));
308308
}
309309
if validate {
310310
validate_body(tcx, body, format!("after pass {pass_name}"));
@@ -348,5 +348,5 @@ pub(super) fn validate_body<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, when
348348

349349
pub(super) fn dump_mir_for_phase_change<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
350350
assert_eq!(body.pass_count, 0);
351-
mir::dump_mir(tcx, true, body.phase.name(), &"after", body, |_, _| Ok(()))
351+
mir::dump_mir(tcx, true, body.phase.name(), &"after", body, &|_, _| Ok(()))
352352
}

compiler/rustc_mir_transform/src/shim.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ fn build_construct_coroutine_by_move_shim<'tcx>(
12481248
if receiver_by_ref { "coroutine_closure_by_ref" } else { "coroutine_closure_by_move" },
12491249
&0,
12501250
&body,
1251-
|_, _| Ok(()),
1251+
&|_, _| Ok(()),
12521252
);
12531253

12541254
body

0 commit comments

Comments
 (0)