Skip to content

Commit

Permalink
chore!: Replace thiserror with derive_more 1.0 (#624)
Browse files Browse the repository at this point in the history
`derive_more` 1.0 can now be used as a more granular replacement for
`thiserror`.
The main changes required were:

- Explicit derives for `Display` and `From`, in addition to `Error`.
- Use `#[display(...)]` directives instead of `#[error(...)]`. The
syntax is sightly different for referencing tuple elements (`_0` vs
`0`). `error(transparent)` is also the default behaviour.
- The new `From` derive is less intrusive (derives it for every variant
non-`from(ignore)`d, unless explicit variants are picked with
`#[from]`).

Those changes are non-breaking (see first commit). Since the next
release will be a breaking one, I included some cleanups too.

- Added all missing `#[non_exhaustive]`s to the error enums (this
missing is what caused the last breaking change).
- Converted unary tuple variants into named structs.
`derive_more` assumes unary tuples to be an error source, so we had to
add `#[error(ignore)]` in some cases.
- Add more contexts to some errors (e.g. adding the node id to errors
that only printed the port offset).

BREAKING CHANGE: Made all errors `non_exhaustive`, and renamed some
fields for clarity.
  • Loading branch information
aborgna-q authored Sep 30, 2024
1 parent 1cf9dcb commit 2250ce7
Show file tree
Hide file tree
Showing 19 changed files with 241 additions and 183 deletions.
7 changes: 3 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ criterion = "0.5.1"
crossbeam-channel = "0.5.8"
csv = "1.2.2"
delegate = "0.13.0"
derive_more = "0.99.18"
derive_more = "1.0.0"
downcast-rs = "1.2.0"
fxhash = "0.2.1"
lazy_static = "1.5.0"
Expand All @@ -61,7 +61,6 @@ serde_json = "1.0"
smol_str = "0.2.0"
strum = "0.26.3"
strum_macros = "0.26.4"
thiserror = "1.0.64"
tracing-appender = "0.2.2"
tracing-subscriber = "0.3.17"
typetag = "0.2.18"
Expand Down
9 changes: 7 additions & 2 deletions tket2-hseries/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ serde_json.workspace = true
smol_str.workspace = true
strum.workspace = true
strum_macros.workspace = true
thiserror.workspace = true
itertools.workspace = true
clap = { workspace = true, optional = true}
clap = { workspace = true, optional = true }
hugr-cli = { workspace = true, optional = true }
derive_more = { workspace = true, features = [
"error",
"display",
"from",
"into",
] }

[dev-dependencies]
cool_asserts.workspace = true
Expand Down
46 changes: 26 additions & 20 deletions tket2-hseries/src/extension/hseries/lower.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use derive_more::{Display, Error, From};
use hugr::ops::NamedOp;
use hugr::{
algorithms::validation::{ValidatePassError, ValidationLevel},
builder::{BuildError, DFGBuilder, Dataflow, DataflowHugr},
Expand All @@ -11,7 +13,6 @@ use hugr::{
use lazy_static::lazy_static;
use std::collections::HashMap;
use strum::IntoEnumIterator;
use thiserror::Error;
use tket2::{extension::rotation::RotationOpBuilder, Tk2Op};

use crate::extension::hseries::{HSeriesOp, HSeriesOpBuilder};
Expand All @@ -35,26 +36,30 @@ fn const_f64<T: Dataflow + ?Sized>(builder: &mut T, value: f64) -> Wire {
}

/// Errors produced by lowering [Tk2Op]s.
#[derive(Debug, Error)]
#[allow(missing_docs)]
#[derive(Debug, Display, Error, From)]
#[non_exhaustive]
pub enum LowerTk2Error {
#[error("Error when building the circuit: {0}")]
BuildError(#[from] BuildError),

#[error("Unrecognised operation: {0:?} with {1} inputs")]
/// An error raised when building the circuit.
#[display("Error when building the circuit: {_0}")]
BuildError(BuildError),
/// Found an unrecognised operation.
#[display("Unrecognised operation: {} with {_1} inputs", _0.name())]
UnknownOp(Tk2Op, usize),

#[error("Error when replacing op: {0}")]
OpReplacement(#[from] HugrError),

#[error("Error when lowering ops: {0}")]
CircuitReplacement(#[from] hugr::algorithms::lower::LowerError),

#[error("Tk2Ops were not lowered: {0:?}")]
Unlowered(Vec<Node>),

#[error(transparent)]
ValidationError(#[from] ValidatePassError),
/// An error raised when replacing an operation.
#[display("Error when replacing op: {_0}")]
OpReplacement(HugrError),
/// An error raised when lowering operations.
#[display("Error when lowering ops: {_0}")]
CircuitReplacement(hugr::algorithms::lower::LowerError),
/// Tk2Ops were not lowered after the pass.
#[display("Tk2Ops were not lowered: {missing_ops:?}")]
#[from(ignore)]
Unlowered {
/// The list of nodes that were not lowered.
missing_ops: Vec<Node>,
},
/// Validation error in the final hugr.
ValidationError(ValidatePassError),
}

fn op_to_hugr(op: Tk2Op) -> Result<Hugr, LowerTk2Error> {
Expand Down Expand Up @@ -179,7 +184,8 @@ impl LowerTket2ToHSeriesPass {
self.0.run_validated_pass(hugr, registry, |hugr, level| {
lower_tk2_op(hugr)?;
if *level != ValidationLevel::None {
check_lowered(hugr).map_err(LowerTk2Error::Unlowered)?;
check_lowered(hugr)
.map_err(|missing_ops| LowerTk2Error::Unlowered { missing_ops })?;
}
Ok(())
})
Expand Down
14 changes: 6 additions & 8 deletions tket2-hseries/src/lazify_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! [HSeriesOp::Measure]: crate::extension::hseries::HSeriesOp::Measure
use std::collections::{HashMap, HashSet};

use derive_more::{Display, Error, From};
use hugr::{
algorithms::{
ensure_no_nonlocal_edges,
Expand All @@ -20,7 +21,6 @@ use hugr::{
types::Signature,
Hugr, HugrView, IncomingPort, Node, OutgoingPort, SimpleReplacement,
};
use thiserror::Error;
use tket2::Tk2Op;

use lazy_static::lazy_static;
Expand All @@ -39,18 +39,16 @@ use crate::extension::{
#[derive(Default)]
pub struct LazifyMeasurePass(ValidationLevel);

#[derive(Error, Debug)]
#[derive(Error, Debug, Display, From)]
#[non_exhaustive]
/// An error reported from [LazifyMeasurePass].
pub enum LazifyMeasurePassError {
/// The [Hugr] was invalid either before or after a pass ran.
#[error(transparent)]
ValidationError(#[from] ValidatePassError),
ValidationError(ValidatePassError),
/// The [Hugr] was found to contain non-local edges.
#[error(transparent)]
NonLocalEdgesError(#[from] NonLocalEdgesError),
NonLocalEdgesError(NonLocalEdgesError),
/// A [SimpleReplacement] failed during the running of the pass.
#[error(transparent)]
SimpleReplacementError(#[from] SimpleReplacementError),
SimpleReplacementError(SimpleReplacementError),
}

impl LazifyMeasurePass {
Expand Down
18 changes: 7 additions & 11 deletions tket2-hseries/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Provides a preparation and validation workflow for Hugrs targeting
//! Quantinuum H-series quantum computers.
use derive_more::{Display, Error, From};
use hugr::{
algorithms::{
force_order,
Expand All @@ -10,8 +11,6 @@ use hugr::{
};
use tket2::Tk2Op;

use thiserror::Error;

use extension::{
futures::FutureOpDef,
hseries::{HSeriesOp, LowerTk2Error, LowerTket2ToHSeriesPass},
Expand All @@ -33,21 +32,18 @@ pub struct HSeriesPass {
validation_level: ValidationLevel,
}

#[derive(Error, Debug)]
#[derive(Error, Debug, Display, From)]
#[non_exhaustive]
/// An error reported from [HSeriesPass].
pub enum HSeriesPassError {
/// The [hugr::Hugr] was invalid either before or after a pass ran.
#[error(transparent)]
ValidationError(#[from] ValidatePassError),
ValidationError(ValidatePassError),
/// An error from the component [LazifyMeasurePass].
#[error(transparent)]
LazyMeasureError(#[from] LazifyMeasurePassError),
LazyMeasureError(LazifyMeasurePassError),
/// An error from the component [force_order()] pass.
#[error(transparent)]
ForceOrderError(#[from] HugrError),
ForceOrderError(HugrError),
/// An error from the component [LowerTket2ToHSeriesPass] pass.
#[error(transparent)]
LowerTk2Error(#[from] LowerTk2Error),
LowerTk2Error(LowerTk2Error),
}

impl HSeriesPass {
Expand Down
2 changes: 1 addition & 1 deletion tket2-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ hugr = { workspace = true }
portgraph = { workspace = true, features = ["serde"] }
pyo3 = { workspace = true }
num_cpus = { workspace = true }
derive_more = { workspace = true }
derive_more = { workspace = true, features = ["into", "from"] }
itertools = { workspace = true }
portmatching = { workspace = true }
strum = { workspace = true }
Expand Down
8 changes: 6 additions & 2 deletions tket2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ num-rational = { workspace = true }
num-complex = { workspace = true, optional = true }
tket-json-rs = { workspace = true }
rayon = { workspace = true }
thiserror = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
downcast-rs = { workspace = true }
Expand All @@ -50,7 +49,12 @@ typetag = { workspace = true }
itertools = { workspace = true }
petgraph = { workspace = true }
portmatching = { workspace = true, optional = true, features = ["serde"] }
derive_more = { workspace = true }
derive_more = { workspace = true, features = [
"error",
"display",
"from",
"into",
] }
hugr = { workspace = true }
hugr-core = { workspace = true }
portgraph = { workspace = true, features = ["serde"] }
Expand Down
Loading

0 comments on commit 2250ce7

Please sign in to comment.