-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make function bodies with &! arguments unreachable
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |