Skip to content
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
5 changes: 4 additions & 1 deletion hugr-core/src/hugr/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use thiserror::Error;
use crate::core::NodeIndex;
use crate::hugr::Hugr;
use crate::ops::OpType;
use crate::types::EdgeKind;
use crate::{Node, PortIndex};
use portgraph::hierarchy::AttachError;
use portgraph::{Direction, LinkError, PortView};
Expand Down Expand Up @@ -215,7 +216,9 @@ impl TryFrom<&Hugr> for SerHugrLatest {
let op = hugr.get_optype(node);
let is_value_port = offset < op.value_port_count(dir);
let is_static_input = op.static_port(dir).is_some_and(|p| p.index() == offset);
let offset = (is_value_port || is_static_input).then_some(offset as u32);
let other_port_is_not_order = op.other_port_kind(dir) != Some(EdgeKind::StateOrder);
let offset = (is_value_port || is_static_input || other_port_is_not_order)
.then_some(offset as u32);
(node_rekey[&node], offset)
};

Expand Down
26 changes: 26 additions & 0 deletions hugr-core/src/hugr/serialize/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::builder::{
Container, DFGBuilder, Dataflow, DataflowHugr, DataflowSubContainer, HugrBuilder,
ModuleBuilder, endo_sig, inout_sig, test::closed_dfg_root_hugr,
};
use crate::envelope::{EnvelopeConfig, read_envelope, write_envelope};
use crate::extension::ExtensionRegistry;
use crate::extension::prelude::Noop;
use crate::extension::prelude::{bool_t, qb_t, usize_t};
Expand All @@ -16,15 +17,21 @@ use crate::hugr::validate::ValidationError;
use crate::hugr::views::ExtractionResult;
use crate::ops::custom::{ExtensionOp, OpaqueOp, OpaqueOpError};
use crate::ops::{self, DFG, Input, Module, Output, Value, dataflow::IOTrait};
use crate::package::Package;
use crate::std_extensions::arithmetic::float_types::float64_type;
use crate::std_extensions::arithmetic::int_types::{ConstInt, INT_TYPES};
use crate::std_extensions::logic::LogicOp;
use crate::std_extensions::std_reg;
use crate::test_file;
use crate::types::type_param::TypeParam;
use crate::types::{
FuncValueType, PolyFuncType, PolyFuncTypeRV, Signature, SumType, Type, TypeArg, TypeBound,
TypeRV,
};
use crate::{OutgoingPort, Visibility, type_row};
use std::fs::File;
use std::io::{BufReader, Cursor};

use std::sync::LazyLock;

use itertools::Itertools;
Expand Down Expand Up @@ -625,6 +632,25 @@ fn std_extensions_valid() {
}
}

#[test]
#[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri
// https://github.com/CQCL/hugr/issues/2600
fn cfg_edge_ordering() {
let pkg: Package = Package::load(
BufReader::new(File::open(test_file!("issue-2600.hugr")).unwrap()),
None,
)
.unwrap();
pkg.validate().unwrap();

let mut data1: Vec<u8> = Vec::new();
let _ = write_envelope(&mut data1, &pkg, EnvelopeConfig::text());

let buff1 = Cursor::new(data1);
let (_, pkg1) = read_envelope(buff1, &std_reg()).unwrap();
pkg1.validate().unwrap();
}

mod proptest {
use super::check_testing_roundtrip;
use super::{NodeSer, SimpleOpDef};
Expand Down
2 changes: 1 addition & 1 deletion hugr-py/src/hugr/hugr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ def _serialize_link(
def _constrain_offset(self, p: P) -> PortOffset | None:
"""Constrain an offset to be a valid encoded port offset.

Order edges and control flow edges should be encoded without an offset.
Order edges should be encoded without an offset.
"""
if p.offset < 0:
assert p.offset == -1, "Only order edges are allowed with offset < 0"
Expand Down
Binary file added resources/test/issue-2600.hugr
Binary file not shown.
Loading