Skip to content

Commit 31a7b44

Browse files
committed
Rename entry_points<->roots, use extend + assert
1 parent 73f65ee commit 31a7b44

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

hugr-passes/src/call_graph.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,24 @@ impl CallGraph {
7777
fn reachable_funcs<'a>(
7878
cg: &'a CallGraph,
7979
h: &impl HugrView,
80-
roots: impl IntoIterator<Item = Node>,
80+
entry_points: impl IntoIterator<Item = Node>,
8181
) -> impl Iterator<Item = Node> + 'a {
82-
let mut entry_points = roots.into_iter().collect_vec();
82+
let mut roots = entry_points.into_iter().collect_vec();
8383
let mut b = if h.get_optype(h.root()).is_module() {
84-
if entry_points.is_empty() {
85-
entry_points.push(
86-
h.children(h.root())
87-
.filter(|n| {
88-
h.get_optype(*n)
89-
.as_func_defn()
90-
.is_some_and(|fd| fd.name == "main")
91-
})
92-
.exactly_one()
93-
.ok()
94-
.expect("No entry_points provided, Module must contain `main`"),
95-
);
84+
if roots.is_empty() {
85+
roots.extend(h.children(h.root()).filter(|n| {
86+
h.get_optype(*n)
87+
.as_func_defn()
88+
.is_some_and(|fd| fd.name == "main")
89+
}));
90+
assert!(!roots.is_empty(), "No entry_points for Module and no `main`");
9691
}
97-
let mut entry_points = entry_points
98-
.into_iter()
99-
.map(|i| cg.node_to_g.get(&i).unwrap());
100-
let mut b = Bfs::new(&cg.g, *entry_points.next().unwrap());
101-
b.stack.extend(entry_points);
92+
let mut roots = roots.into_iter().map(|i| cg.node_to_g.get(&i).unwrap());
93+
let mut b = Bfs::new(&cg.g, *roots.next().unwrap());
94+
b.stack.extend(roots);
10295
b
10396
} else {
104-
assert!(entry_points.is_empty());
97+
assert!(roots.is_empty());
10598
Bfs::new(&cg.g, *cg.node_to_g.get(&h.root()).unwrap())
10699
};
107100
std::iter::from_fn(move || b.next(&cg.g)).map(|i| *cg.g.node_weight(i).unwrap())

0 commit comments

Comments
 (0)