Skip to content

Commit

Permalink
Add test of broken SiblingMut covariance
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Oct 9, 2023
1 parent 375a6c8 commit 2f572aa
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/hugr/views/sibling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ mod test {
use crate::builder::{Container, Dataflow, DataflowSubContainer, HugrBuilder, ModuleBuilder};
use crate::extension::PRELUDE_REGISTRY;
use crate::hugr::NodeType;
use crate::ops::handle::{CfgID, DfgID, FuncID, ModuleRootID};
use crate::ops::handle::{CfgID, DataflowParentID, DfgID, FuncID, ModuleRootID};
use crate::ops::{dataflow::IOTrait, Input, OpTag, Output};
use crate::type_row;
use crate::types::{FunctionType, Type};
Expand Down Expand Up @@ -462,4 +462,33 @@ mod test {
simple_dfg_hugr.replace_op(root, bad_nodetype).unwrap();
assert!(simple_dfg_hugr.validate(&PRELUDE_REGISTRY).is_err());
}

#[rstest]
//#[should_panic] // See commented out line in test
fn sibling_mut_covariance(mut simple_dfg_hugr: Hugr) {
let root = simple_dfg_hugr.root();
let case_nodetype = NodeType::open_extensions(crate::ops::Case {
signature: simple_dfg_hugr.root_type().op_signature(),
});
let mut sib_mut = SiblingMut::<DfgID>::try_new(&mut simple_dfg_hugr, root).unwrap();
// As expected, we cannot replace the root with a Case
assert_eq!(
sib_mut.replace_op(root, case_nodetype.clone()),
Err(HugrError::InvalidTag {
required: OpTag::Dfg,
actual: OpTag::Case
})
);

let mut nested_sib_mut =
SiblingMut::<DataflowParentID>::try_new(&mut sib_mut, root).unwrap();
nested_sib_mut
.replace_op(root, case_nodetype.clone())
.unwrap();
// The following line panics:
// assert_eq!(sib_mut.root_type(), &case_nodetype);
// But it'd have been much better to have stopped things getting to this point,
// is if we don't call root_type we can just carry on:
assert!(!DfgID::TAG.is_superset(case_nodetype.tag()));
}
}

0 comments on commit 2f572aa

Please sign in to comment.