Skip to content

Commit

Permalink
make get_entry_exit panic if the entry/exit nodes don't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed Jun 18, 2024
1 parent 32ffe25 commit 74deefc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 0 additions & 2 deletions src/emit/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ impl<'c, H: HugrView> EmitFuncContext<'c, H> {
/// Create a new basic block. When `before` is `Some` the block will be
/// created immediately before that block, otherwise at the end of the
/// function.
///
/// TODO I think this will be needed for emitting CFGs.
pub(crate) fn new_basic_block(
&mut self,
name: impl AsRef<str>,
Expand Down
2 changes: 1 addition & 1 deletion src/emit/ops/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'c, 'd, H: HugrView> CfgEmitter<'c, 'd, H> {
}
})
.collect::<Result<HashMap<_, _>>>()?;
let (entry_node, exit_node) = node.get_entry_exit().unwrap();
let (entry_node, exit_node) = node.get_entry_exit();
Ok(CfgEmitter {
context,
bbs,
Expand Down
19 changes: 10 additions & 9 deletions src/fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,22 @@ impl<'c, OT, H: HugrView + ?Sized> FatNode<'c, OT, H> {
}

impl<'c, H: HugrView> FatNode<'c, CFG, H> {
/// TODO it would be reasonable to remove Option and panic on failure here
pub fn get_entry_exit(
&self,
) -> Option<(FatNode<'c, DataflowBlock, H>, FatNode<'c, ExitBlock, H>)> {
/// Returns the entry and exit nodes of a CFG.
///
/// These are guaranteed to exist the `Hugr` is valid. Panics if they do not
/// exist.
pub fn get_entry_exit(&self) -> (FatNode<'c, DataflowBlock, H>, FatNode<'c, ExitBlock, H>) {
let [i, o] = self
.hugr
.children(self.node)
.take(2)
.collect_vec()
.try_into()
.ok()?;
Some((
FatNode::try_new(self.hugr, i)?,
FatNode::try_new(self.hugr, o)?,
))
.unwrap();
(
FatNode::try_new(self.hugr, i).unwrap(),
FatNode::try_new(self.hugr, o).unwrap(),
)
}
}

Expand Down

0 comments on commit 74deefc

Please sign in to comment.