Skip to content

Commit

Permalink
fix: HugrView::get_function_type (#507)
Browse files Browse the repository at this point in the history
Previously this was implemented separately for Hugr (correctly,
according to the doc), and for the other two HugrViews
SiblingGraph/DescendantsGraph (by deferring to the get_function_type of
the whole Hugr).

This is incorrect - the latter two should (surely?) return the function
type of the portion of the Hugr which they view.

However, we can both fix this and delete code, by moving the impl-Hugr
version to be the trait default, and removing the other two. See test
added (previously both were returning None).
  • Loading branch information
acl-cqc authored Sep 8, 2023
1 parent 681db93 commit e23323d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
19 changes: 9 additions & 10 deletions src/hugr/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,15 @@ pub trait HugrView: sealed::HugrInternals {

/// For function-like HUGRs (DFG, FuncDefn, FuncDecl), report the function
/// type. Otherwise return None.
fn get_function_type(&self) -> Option<&FunctionType>;
fn get_function_type(&self) -> Option<&FunctionType> {
let op = self.get_nodetype(self.root());
match &op.op {
OpType::DFG(DFG { signature })
| OpType::FuncDecl(FuncDecl { signature, .. })
| OpType::FuncDefn(FuncDefn { signature, .. }) => Some(signature),
_ => None,
}
}

/// Return a wrapper over the view that can be used in petgraph algorithms.
#[inline]
Expand Down Expand Up @@ -379,15 +387,6 @@ where
}
}

fn get_function_type(&self) -> Option<&FunctionType> {
let op = self.get_nodetype(self.root());
match &op.op {
OpType::DFG(DFG { signature })
| OpType::FuncDecl(FuncDecl { signature, .. })
| OpType::FuncDefn(FuncDefn { signature, .. }) => Some(signature),
_ => None,
}
}
#[inline]
fn get_metadata(&self, node: Node) -> &NodeMetadata {
self.as_ref().metadata.get(node.index)
Expand Down
14 changes: 10 additions & 4 deletions src/hugr/views/descendants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ where
fn get_io(&self, node: Node) -> Option<[Node; 2]> {
self.base_hugr().get_io(node)
}

fn get_function_type(&self) -> Option<&crate::types::FunctionType> {
self.base_hugr().get_function_type()
}
}

impl<'a, Root, Base> HierarchyView<'a> for DescendantsGraph<'a, Root, Base>
Expand Down Expand Up @@ -311,6 +307,16 @@ pub(super) mod test {
|| hugr.get_parent(n) == Some(inner)));
assert_eq!(region.children(inner).count(), 2);

assert_eq!(
region.get_function_type(),
Some(&FunctionType::new(type_row![NAT, QB], type_row![NAT, QB]))
);
let inner_region: DescendantsGraph = DescendantsGraph::new(&hugr, inner);
assert_eq!(
inner_region.get_function_type(),
Some(&FunctionType::new(type_row![NAT], type_row![NAT]))
);

Ok(())
}
}
4 changes: 0 additions & 4 deletions src/hugr/views/sibling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,6 @@ where
None
}
}

fn get_function_type(&self) -> Option<&crate::types::FunctionType> {
self.base_hugr().get_function_type()
}
}

impl<'a, Root, Base> HierarchyView<'a> for SiblingGraph<'a, Root, Base>
Expand Down

0 comments on commit e23323d

Please sign in to comment.