Skip to content

Commit cca66d9

Browse files
authored
fix!: Use unique region_portgraph in convexity check (#2192)
Previously, `region_portgraph` was called twice: once to construct the convexity checker (e.g. in `try_from_nodes`), and once to actually construct the subgraph (in `try_new_with_checker`). As a result, if the results from `region_portgraph` were not deterministic, the checker would not be valid and the convexity check might fail. I've had to narrow down the argument type, but there is currently only one type of convexity checker used, so this should not impact anyone. BREAKING CHANGE: `SiblingSubgraph` only accepts `TopoConvexChecker` as convexity checker type
1 parent 2bfeaae commit cca66d9

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

hugr-core/src/hugr/views/sibling_subgraph.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,12 @@ impl<N: HugrNode> SiblingSubgraph<N> {
178178
inputs: IncomingPorts<N>,
179179
outputs: OutgoingPorts<N>,
180180
hugr: &H,
181-
checker: &impl ConvexChecker,
181+
checker: &TopoConvexChecker<H>,
182182
) -> Result<Self, InvalidSubgraph<N>> {
183-
let parent = pick_parent(hugr, &inputs, &outputs)?;
184-
let (region, node_map) = hugr.region_portgraph(parent);
183+
let (region, node_map) = checker.region_portgraph();
185184

186185
// Ordering of the edges here is preserved and becomes ordering of the signature.
187-
let boundary = make_boundary::<H>(&region, &node_map, &inputs, &outputs);
186+
let boundary = make_boundary::<H>(&region, node_map, &inputs, &outputs);
188187
let subpg = Subgraph::new_subgraph(region, boundary);
189188
let nodes = subpg
190189
.nodes_iter()
@@ -242,10 +241,10 @@ impl<N: HugrNode> SiblingSubgraph<N> {
242241
///
243242
/// Refer to [`SiblingSubgraph::try_from_nodes`] for the full
244243
/// documentation.
245-
pub fn try_from_nodes_with_checker<'c, 'h: 'c>(
244+
pub fn try_from_nodes_with_checker<H: HugrView<Node = N>>(
246245
nodes: impl Into<Vec<N>>,
247-
hugr: &'h impl HugrView<Node = N>,
248-
checker: &impl ConvexChecker,
246+
hugr: &H,
247+
checker: &TopoConvexChecker<H>,
249248
) -> Result<Self, InvalidSubgraph<N>> {
250249
let nodes = nodes.into();
251250

@@ -610,6 +609,12 @@ impl<'g, Base: HugrView> TopoConvexChecker<'g, Base> {
610609
&self.init_checker().0
611610
}
612611

612+
/// Return the portgraph and node map on which convexity queries are performed.
613+
fn region_portgraph(&self) -> (CheckerRegion<'g, Base>, &Base::RegionPortgraphNodes) {
614+
let (checker, node_map) = self.init_checker();
615+
(checker.graph(), node_map)
616+
}
617+
613618
/// Returns the node map from the region to the base HUGR.
614619
#[expect(dead_code)]
615620
fn get_node_map(&self) -> &Base::RegionPortgraphNodes {

0 commit comments

Comments
 (0)