Skip to content

Commit

Permalink
Make function bodies with &! arguments unreachable
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Jun 7, 2023
1 parent 408bbd0 commit dedc3b3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ mod multiple_return_terminators;
mod normalize_array_len;
mod nrvo;
mod prettify;
mod ralf_refs;
mod ref_prop;
mod remove_noop_landing_pads;
mod remove_storage_markers;
Expand Down Expand Up @@ -545,6 +546,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&check_alignment::CheckAlignment,
&reveal_all::RevealAll, // has to be done before inlining, since inlined code is in RevealAll mode.
&lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first
&ralf_refs::RalfRefs,
&unreachable_prop::UnreachablePropagation,
&uninhabited_enum_branching::UninhabitedEnumBranching,
&o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching),
Expand Down
38 changes: 38 additions & 0 deletions compiler/rustc_mir_transform/src/ralf_refs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::MirPass;
use rustc_middle::mir::*;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::TyCtxt;

pub struct RalfRefs;

impl<'tcx> MirPass<'tcx> for RalfRefs {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 1
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let def_id = body.source.def_id();
if tcx.type_of(body.source.def_id()).subst_identity().is_generator() {
return;
}
let param_env = tcx.param_env_reveal_all_normalized(def_id);
for arg in body.local_decls.iter().skip(1).take(body.arg_count) {
let ty = arg.ty.peel_refs();
let Ok(layout) = tcx.layout_of(param_env.and(ty)) else { continue; };
if layout.abi.is_uninhabited() {
debug!(
"Deleting {}::{}",
tcx.crate_name(def_id.krate),
with_no_trimmed_paths!(tcx.def_path_str(def_id))
);
let blocks = body.basic_blocks.as_mut();
blocks.truncate(1);
let block = &mut blocks[START_BLOCK];
block.statements.clear();
block.terminator.as_mut().unwrap().kind = TerminatorKind::Unreachable;
block.is_cleanup = false;
return;
}
}
}
}

0 comments on commit dedc3b3

Please sign in to comment.