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/replace bad edge nums #753

Merged
merged 3 commits into from
Dec 20, 2023
Merged
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
20 changes: 10 additions & 10 deletions src/hugr/rewrite/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub struct Replacement {
}

impl NewEdgeSpec {
fn check_src(&self, h: &impl HugrView) -> Result<(), ReplaceError> {
fn check_src(&self, h: &impl HugrView, err_spec: &NewEdgeSpec) -> Result<(), ReplaceError> {
let optype = h.get_optype(self.src);
let ok = match self.kind {
NewEdgeKind::Order => optype.other_output() == Some(EdgeKind::StateOrder),
Expand All @@ -100,9 +100,9 @@ impl NewEdgeSpec {
}
};
ok.then_some(())
.ok_or(ReplaceError::BadEdgeKind(Direction::Outgoing, self.clone()))
.ok_or_else(|| ReplaceError::BadEdgeKind(Direction::Outgoing, err_spec.clone()))
}
fn check_tgt(&self, h: &impl HugrView) -> Result<(), ReplaceError> {
fn check_tgt(&self, h: &impl HugrView, err_spec: &NewEdgeSpec) -> Result<(), ReplaceError> {
let optype = h.get_optype(self.tgt);
let ok = match self.kind {
NewEdgeKind::Order => optype.other_input() == Some(EdgeKind::StateOrder),
Expand All @@ -118,7 +118,7 @@ impl NewEdgeSpec {
),
};
ok.then_some(())
.ok_or(ReplaceError::BadEdgeKind(Direction::Incoming, self.clone()))
.ok_or_else(|| ReplaceError::BadEdgeKind(Direction::Incoming, err_spec.clone()))
}

fn check_existing_edge(
Expand Down Expand Up @@ -233,20 +233,20 @@ impl Rewrite for Replacement {
e.clone(),
));
}
e.check_src(h)?;
e.check_src(h, &e)?;
}
self.mu_out.iter().try_for_each(|e| {
self.replacement.valid_non_root(e.src).map_err(|_| {
ReplaceError::BadEdgeSpec(Direction::Outgoing, WhichHugr::Replacement, e.clone())
})?;
e.check_src(&self.replacement)
e.check_src(&self.replacement, &e)
})?;
// Edge targets...
self.mu_inp.iter().try_for_each(|e| {
self.replacement.valid_non_root(e.tgt).map_err(|_| {
ReplaceError::BadEdgeSpec(Direction::Incoming, WhichHugr::Replacement, e.clone())
})?;
e.check_tgt(&self.replacement)
e.check_tgt(&self.replacement, &e)
})?;
for e in self.mu_out.iter().chain(self.mu_new.iter()) {
if !h.contains_node(e.tgt) || removed.contains(&e.tgt) {
Expand All @@ -256,7 +256,7 @@ impl Rewrite for Replacement {
e.clone(),
));
}
e.check_tgt(h)?;
e.check_tgt(h, &e)?;
// The descendant check is to allow the case where the old edge is nonlocal
// from a part of the Hugr being moved (which may require changing source,
// depending on where the transplanted portion ends up). While this subsumes
Expand Down Expand Up @@ -353,8 +353,8 @@ fn transfer_edges<'a>(
h.valid_node(e.tgt).map_err(|_| {
ReplaceError::BadEdgeSpec(Direction::Incoming, WhichHugr::Retained, oe.clone())
})?;
e.check_src(h)?;
e.check_tgt(h)?;
e.check_src(h, &oe)?;
e.check_tgt(h, &oe)?;
match e.kind {
NewEdgeKind::Order => {
h.add_other_edge(e.src, e.tgt).unwrap();
Expand Down