Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extract_hugr not removing root node ports #1239

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hugr-core/src/hugr/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use itertools::{Itertools, MapInto};
use portgraph::render::{DotFormat, MermaidFormat};
use portgraph::{multiportgraph, LinkView, PortView};

use super::internal::HugrInternals;
use super::internal::{HugrInternals, HugrMutInternals};
use super::{
Hugr, HugrError, HugrMut, NodeMetadata, NodeMetadataMap, ValidationError, DEFAULT_OPTYPE,
};
Expand Down Expand Up @@ -512,6 +512,7 @@ pub trait ExtractHugr: HugrView + Sized {
let old_root = hugr.root();
let new_root = hugr.insert_from_view(old_root, &self).new_root;
hugr.set_root(new_root);
hugr.set_num_ports(new_root, 0, 0);
Copy link
Collaborator

@doug-q doug-q Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add a test? make_module_hgr seems to provide a suitable case, the inner DFG (which has ports) should be extracted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what the descendant graph test is doing. Only SiblingGraph and DescendantGraph run this code on extraction.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test you changed:

  • extracts a node that has no ports anyway
  • does not assert that the extracted Hugr has no ports

hugr.remove_node(old_root);
hugr
}
Expand Down
19 changes: 19 additions & 0 deletions hugr-core/src/hugr/views/descendants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ where

#[cfg(test)]
pub(super) mod test {
use rstest::rstest;

use crate::extension::PRELUDE_REGISTRY;
use crate::{
builder::{Container, Dataflow, DataflowSubContainer, HugrBuilder, ModuleBuilder},
type_row,
Expand Down Expand Up @@ -269,4 +272,20 @@ pub(super) mod test {

Ok(())
}

#[rstest]
fn extract_hugr() -> Result<(), Box<dyn std::error::Error>> {
let (hugr, def, _inner) = make_module_hgr()?;

let region: DescendantsGraph = DescendantsGraph::try_new(&hugr, def)?;
let extracted = region.extract_hugr();
extracted.validate(&PRELUDE_REGISTRY)?;

let region: DescendantsGraph = DescendantsGraph::try_new(&hugr, def)?;

assert_eq!(region.node_count(), extracted.node_count());
assert_eq!(region.root_type(), extracted.root_type());

Ok(())
}
}
7 changes: 4 additions & 3 deletions hugr-core/src/hugr/views/sibling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,13 @@ mod test {

#[rstest]
fn extract_hugr() -> Result<(), Box<dyn std::error::Error>> {
let (hugr, def, _inner) = make_module_hgr()?;
let (hugr, _def, inner) = make_module_hgr()?;

let region: SiblingGraph = SiblingGraph::try_new(&hugr, def)?;
let region: SiblingGraph = SiblingGraph::try_new(&hugr, inner)?;
let extracted = region.extract_hugr();
extracted.validate(&PRELUDE_REGISTRY)?;

let region: SiblingGraph = SiblingGraph::try_new(&hugr, def)?;
let region: SiblingGraph = SiblingGraph::try_new(&hugr, inner)?;

assert_eq!(region.node_count(), extracted.node_count());
assert_eq!(region.root_type(), extracted.root_type());
aborgna-q marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading