@@ -447,23 +447,33 @@ impl<N: HugrNode> PatchHugrMut for SimpleReplacement<N> {
447447 ..
448448 } = self ;
449449
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+
450456 // 2. Insert the replacement as a whole.
451457 let InsertionResult {
452458 inserted_entrypoint : new_entrypoint,
453- node_map,
459+ mut node_map,
454460 } = h. insert_hugr ( parent, replacement) ;
455461
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) ;
460467 }
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 ( ) {
463471 h. set_parent ( child, parent) ;
464472 }
465- // remove the replacement root (which now has no children and no edges)
473+
474+ // remove the replacement entrypoint from h and node_map
466475 h. remove_node ( new_entrypoint) ;
476+ node_map. remove ( & repl_entrypoint) ;
467477
468478 // 3. Insert all boundary edges.
469479 for ( src, tgt) in boundary_edges {
@@ -529,7 +539,7 @@ pub(in crate::hugr::patch) mod test {
529539 DataflowSubContainer , HugrBuilder , ModuleBuilder ,
530540 } ;
531541 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 } ;
533543 use crate :: hugr:: patch:: { PatchVerification , ReplacementPort } ;
534544 use crate :: hugr:: views:: { HugrView , SiblingSubgraph } ;
535545 use crate :: hugr:: { Hugr , HugrMut , Patch } ;
@@ -843,7 +853,20 @@ pub(in crate::hugr::patch) mod test {
843853 nu_inp,
844854 nu_out : nu_out. into ( ) ,
845855 } ;
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+
847870 // Expect [DFG] to be replaced with:
848871 // ┌───┐┌───┐
849872 // ┤ H ├┤ H ├
0 commit comments