Skip to content

Commit c8869f8

Browse files
Add find_descendant method to MovePath
1 parent 332aad8 commit c8869f8

File tree

1 file changed

+35
-0
lines changed
  • src/librustc_mir/dataflow/move_paths

1 file changed

+35
-0
lines changed

src/librustc_mir/dataflow/move_paths/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,41 @@ impl<'tcx> MovePath<'tcx> {
7272

7373
parents
7474
}
75+
76+
/// Finds the closest descendant of `self` for which `f` returns `true` using a breadth-first
77+
/// search.
78+
///
79+
/// `f` will **not** be called on `self`.
80+
pub fn find_descendant(
81+
&self,
82+
move_paths: &IndexVec<MovePathIndex, MovePath<'_>>,
83+
f: impl Fn(MovePathIndex) -> bool,
84+
) -> Option<MovePathIndex> {
85+
let mut todo = if let Some(child) = self.first_child {
86+
vec![child]
87+
} else {
88+
return None;
89+
};
90+
91+
while let Some(mpi) = todo.pop() {
92+
if f(mpi) {
93+
return Some(mpi);
94+
}
95+
96+
let move_path = &move_paths[mpi];
97+
if let Some(child) = move_path.first_child {
98+
todo.push(child);
99+
}
100+
101+
// After we've processed the original `mpi`, we should always
102+
// traverse the siblings of any of its children.
103+
if let Some(sibling) = move_path.next_sibling {
104+
todo.push(sibling);
105+
}
106+
}
107+
108+
None
109+
}
75110
}
76111

77112
impl<'tcx> fmt::Debug for MovePath<'tcx> {

0 commit comments

Comments
 (0)