@@ -447,23 +447,33 @@ impl<N: HugrNode> PatchHugrMut for SimpleReplacement<N> {
447
447
..
448
448
} = self ;
449
449
450
+ // Nodes to remove from the replacement hugr
451
+ let repl_io = replacement
452
+ . get_io ( replacement. entrypoint ( ) )
453
+ . expect ( "replacement is DFG-rooted" ) ;
454
+ let repl_entrypoint = replacement. entrypoint ( ) ;
455
+
450
456
// 2. Insert the replacement as a whole.
451
457
let InsertionResult {
452
458
inserted_entrypoint : new_entrypoint,
453
- node_map,
459
+ mut node_map,
454
460
} = h. insert_hugr ( parent, replacement) ;
455
461
456
- // remove the Input and Output nodes from the replacement graph
457
- let replace_children = h. children ( new_entrypoint) . collect :: < Vec < N > > ( ) ;
458
- for & io in & replace_children[ ..2 ] {
459
- h. remove_node ( io) ;
462
+ // remove the Input and Output from h and node_map
463
+ for node in repl_io {
464
+ let node_h = node_map[ & node] ;
465
+ h. remove_node ( node_h) ;
466
+ node_map. remove ( & node) ;
460
467
}
461
- // make all replacement top level children children of the parent
462
- for & child in & replace_children[ 2 ..] {
468
+
469
+ // make all (remaining) replacement top level children children of the parent
470
+ for child in h. children ( new_entrypoint) . collect_vec ( ) {
463
471
h. set_parent ( child, parent) ;
464
472
}
465
- // remove the replacement root (which now has no children and no edges)
473
+
474
+ // remove the replacement entrypoint from h and node_map
466
475
h. remove_node ( new_entrypoint) ;
476
+ node_map. remove ( & repl_entrypoint) ;
467
477
468
478
// 3. Insert all boundary edges.
469
479
for ( src, tgt) in boundary_edges {
@@ -529,7 +539,7 @@ pub(in crate::hugr::patch) mod test {
529
539
DataflowSubContainer , HugrBuilder , ModuleBuilder ,
530
540
} ;
531
541
use crate :: extension:: prelude:: { bool_t, qb_t} ;
532
- use crate :: hugr:: patch:: simple_replace:: OutputBoundaryMap ;
542
+ use crate :: hugr:: patch:: simple_replace:: { Outcome , OutputBoundaryMap } ;
533
543
use crate :: hugr:: patch:: { PatchVerification , ReplacementPort } ;
534
544
use crate :: hugr:: views:: { HugrView , SiblingSubgraph } ;
535
545
use crate :: hugr:: { Hugr , HugrMut , Patch } ;
@@ -843,7 +853,20 @@ pub(in crate::hugr::patch) mod test {
843
853
nu_inp,
844
854
nu_out : nu_out. into ( ) ,
845
855
} ;
846
- h. apply_patch ( r) . unwrap ( ) ;
856
+ let Outcome {
857
+ node_map,
858
+ removed_nodes,
859
+ } = h. apply_patch ( r) . unwrap ( ) ;
860
+
861
+ assert_eq ! (
862
+ node_map. into_keys( ) . collect:: <HashSet <_>>( ) ,
863
+ [ n_node_h] . into_iter( ) . collect:: <HashSet <_>>( ) ,
864
+ ) ;
865
+ assert_eq ! (
866
+ removed_nodes. into_keys( ) . collect:: <HashSet <_>>( ) ,
867
+ [ h_node_cx] . into_iter( ) . collect:: <HashSet <_>>( ) ,
868
+ ) ;
869
+
847
870
// Expect [DFG] to be replaced with:
848
871
// ┌───┐┌───┐
849
872
// ┤ H ├┤ H ├
0 commit comments