Skip to content

Commit e0db21d

Browse files
committed
liveness: Use upvars instead of FnKind to check for closures
Avoiding FnKind will make it easier to run liveness analysis on all bodies in the future, not just fn-like things.
1 parent e3319e7 commit e0db21d

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

compiler/rustc_passes/src/liveness.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ fn visit_fn<'tcx>(
355355

356356
// compute liveness
357357
let mut lsets = Liveness::new(&mut fn_maps, def_id);
358-
let entry_ln = lsets.compute(fk, &body, sp, id);
358+
let entry_ln = lsets.compute(&body, sp, id);
359359
lsets.log_liveness(entry_ln, id);
360360

361361
// check for various error conditions
@@ -862,13 +862,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
862862
self.rwu_table.assign_unpacked(idx, rwu);
863863
}
864864

865-
fn compute(
866-
&mut self,
867-
fk: FnKind<'_>,
868-
body: &hir::Body<'_>,
869-
span: Span,
870-
id: hir::HirId,
871-
) -> LiveNode {
865+
fn compute(&mut self, body: &hir::Body<'_>, span: Span, id: hir::HirId) -> LiveNode {
872866
debug!("compute: using id for body, {:?}", body.value);
873867

874868
// # Liveness of captured variables
@@ -887,7 +881,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
887881
// if they are live on the entry to the closure, since only the closure
888882
// itself can access them on subsequent calls.
889883

890-
if let Some(upvars) = self.ir.tcx.upvars_mentioned(self.body_owner) {
884+
let upvars = self.ir.tcx.upvars_mentioned(self.body_owner);
885+
if let Some(upvars) = upvars {
891886
// Mark upvars captured by reference as used after closure exits.
892887
for (&var_hir_id, upvar) in upvars.iter().rev() {
893888
let upvar_id = ty::UpvarId {
@@ -906,9 +901,11 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
906901

907902
let succ = self.propagate_through_expr(&body.value, self.exit_ln);
908903

909-
match fk {
910-
FnKind::Method(..) | FnKind::ItemFn(..) => return succ,
911-
FnKind::Closure(..) => {}
904+
if upvars.is_none() {
905+
// Either not a closure, or closure without any captured variables.
906+
// No need to determine liveness of captured variables, since there
907+
// are none.
908+
return succ;
912909
}
913910

914911
let ty = self.typeck_results.node_type(id);
@@ -920,7 +917,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
920917
},
921918
ty::Generator(..) => return succ,
922919
_ => {
923-
span_bug!(span, "type of closure expr {:?} is not a closure {:?}", id, ty,);
920+
span_bug!(span, "{} has upvars so it should have a closure type: {:?}", id, ty);
924921
}
925922
};
926923

0 commit comments

Comments
 (0)