@@ -826,7 +826,7 @@ impl<'a> Context<'a> {
826826 }
827827
828828 let region_target_types = ( || {
829- let [ _, region_targets] = self . get_func_type (
829+ let [ _, region_targets] = self . get_ctrl_type (
830830 region_data
831831 . signature
832832 . ok_or_else ( || error_uninferred ! ( "region signature" ) ) ?,
@@ -860,25 +860,18 @@ impl<'a> Context<'a> {
860860 } ;
861861
862862 // The entry node in core control flow regions is identified by being
863- // the first child node of the CFG node. We therefore import the entry
864- // node first and follow it up by every other node.
863+ // the first child node of the CFG node. We therefore import the entry node first.
865864 self . import_node ( entry_node, node) ?;
866865
867- for child in region_data. children {
868- if * child != entry_node {
869- self . import_node ( * child, node) ?;
870- }
871- }
872-
873- // Create the exit node for the control flow region.
866+ // Create the exit node for the control flow region. This always needs
867+ // to be second in the node list.
874868 {
875869 let cfg_outputs = {
876- let [ ctrl_type ] = region_target_types. as_slice ( ) else {
870+ let [ target_types ] = region_target_types. as_slice ( ) else {
877871 return Err ( error_invalid ! ( "cfg region expects a single target" ) ) ;
878872 } ;
879873
880- let [ types] = self . expect_symbol ( * ctrl_type, model:: CORE_CTRL ) ?;
881- self . import_type_row ( types) ?
874+ self . import_type_row ( * target_types) ?
882875 } ;
883876
884877 let exit = self
@@ -887,6 +880,13 @@ impl<'a> Context<'a> {
887880 self . record_links ( exit, Direction :: Incoming , region_data. targets ) ;
888881 }
889882
883+ // Finally we import all other nodes.
884+ for child in region_data. children {
885+ if * child != entry_node {
886+ self . import_node ( * child, node) ?;
887+ }
888+ }
889+
890890 for meta_item in region_data. meta {
891891 self . import_node_metadata ( node, * meta_item)
892892 . map_err ( |err| error_context ! ( err, "node metadata" ) ) ?;
@@ -1245,13 +1245,6 @@ impl<'a> Context<'a> {
12451245 return Err ( error_unsupported ! ( "`{}` as `TypeParam`" , model:: CORE_CONST ) ) ;
12461246 }
12471247
1248- if let Some ( [ ] ) = self . match_symbol ( term_id, model:: CORE_CTRL_TYPE ) ? {
1249- return Err ( error_unsupported ! (
1250- "`{}` as `TypeParam`" ,
1251- model:: CORE_CTRL_TYPE
1252- ) ) ;
1253- }
1254-
12551248 if let Some ( [ item_type] ) = self . match_symbol ( term_id, model:: CORE_LIST_TYPE ) ? {
12561249 // At present `hugr-model` has no way to express that the item
12571250 // type of a list must be copyable. Therefore we import it as `Any`.
@@ -1339,13 +1332,6 @@ impl<'a> Context<'a> {
13391332 return Err ( error_unsupported ! ( "`{}` as `TypeArg`" , model:: CORE_STATIC ) ) ;
13401333 }
13411334
1342- if let Some ( [ ] ) = self . match_symbol ( term_id, model:: CORE_CTRL_TYPE ) ? {
1343- return Err ( error_unsupported ! (
1344- "`{}` as `TypeArg`" ,
1345- model:: CORE_CTRL_TYPE
1346- ) ) ;
1347- }
1348-
13491335 if let Some ( [ ] ) = self . match_symbol ( term_id, model:: CORE_CONST ) ? {
13501336 return Err ( error_unsupported ! ( "`{}` as `TypeArg`" , model:: CORE_CONST ) ) ;
13511337 }
@@ -1510,6 +1496,11 @@ impl<'a> Context<'a> {
15101496 . ok_or ( error_invalid ! ( "expected a function type" ) )
15111497 }
15121498
1499+ fn get_ctrl_type ( & mut self , term_id : table:: TermId ) -> Result < [ table:: TermId ; 2 ] , ImportError > {
1500+ self . match_symbol ( term_id, model:: CORE_CTRL ) ?
1501+ . ok_or ( error_invalid ! ( "expected a control type" ) )
1502+ }
1503+
15131504 fn import_func_type < RV : MaybeRV > (
15141505 & mut self ,
15151506 term_id : table:: TermId ,
0 commit comments