Skip to content

Commit 16903a6

Browse files
authored
chore: Edition 2024 (#2195)
Migrates the package to rust's `edition 2024`. Closes #2144 This PR's changes come almost completely from automated tools. It may be easier to look at them commit by commit. In order, 1. run `cargo fix edition` and remove some backwards-compatibility changes (e.g. matching on `expr_2021` instead of `expr` in macro_rules). 2. run `cargo fmt` 3. fix new lints coming in `rust 1.87`. The last commit is the noisiest one, and totally optional: 4. `clippy --fix`es some additional non-default lints, - Ensuring code links in the rustdocs are formated using backticks. - Adding some `must_use`s - Replacing matches on `bool` with if/else - Removing unnecessary `into_iter` calls - etc. I'll make sure to `.git-ignore-revs` this change to avoid polluting the git history.
1 parent dd003d8 commit 16903a6

File tree

199 files changed

+2852
-2443
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+2852
-2443
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ default-members = ["hugr", "hugr-core", "hugr-passes", "hugr-cli", "hugr-model"]
1616

1717
[workspace.package]
1818
rust-version = "1.85"
19-
edition = "2021"
19+
edition = "2024"
2020
homepage = "https://github.com/CQCL/hugr"
2121
repository = "https://github.com/CQCL/hugr"
2222
license = "Apache-2.0"
@@ -31,11 +31,16 @@ unexpected_cfgs = { level = "warn", check-cfg = [
3131
] }
3232

3333
missing_docs = "warn"
34+
3435
[workspace.lints.clippy]
3536
# Unstable check, may cause false positives.
3637
# https://github.com/rust-lang/rust-clippy/issues/5112
3738
debug_assert_with_mut_call = "warn"
3839

40+
# TODO: Reduce the size of error types.
41+
result_large_err = "allow"
42+
large_enum_variant = "allow"
43+
3944
[workspace.dependencies]
4045
insta = { version = "1.42.2" }
4146
bitvec = "1.0.1"

hugr-cli/src/extensions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl ExtArgs {
2828
pub fn run_dump(&self, registry: &ExtensionRegistry) {
2929
let base_dir = &self.outdir;
3030

31-
for ext in registry.iter() {
31+
for ext in registry {
3232
let mut path = base_dir.clone();
3333
for part in ext.name().split('.') {
3434
path.push(part);

hugr-cli/src/hugr_io.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Input/output arguments for the HUGR CLI.
22
33
use clio::Input;
4-
use hugr::envelope::{read_envelope, EnvelopeError};
4+
use hugr::envelope::{EnvelopeError, read_envelope};
55
use hugr::extension::ExtensionRegistry;
66
use hugr::package::Package;
77
use hugr::{Extension, Hugr};
@@ -43,7 +43,7 @@ impl HugrInputArgs {
4343
/// Read a hugr envelope from the input and return the package encoded
4444
/// within.
4545
///
46-
/// If [HugrInputArgs::hugr_json] is `true`, [HugrInputArgs::get_hugr] should be called instead as
46+
/// If [`HugrInputArgs::hugr_json`] is `true`, [`HugrInputArgs::get_hugr`] should be called instead as
4747
/// reading the input as a package will fail.
4848
pub fn get_package(&mut self) -> Result<Package, CliError> {
4949
let extensions = self.load_extensions()?;
@@ -58,9 +58,9 @@ impl HugrInputArgs {
5858
/// Read a hugr JSON file from the input.
5959
///
6060
/// This is a legacy option for reading old HUGR JSON files when the
61-
/// [HugrInputArgs::hugr_json] flag is used.
61+
/// [`HugrInputArgs::hugr_json`] flag is used.
6262
///
63-
/// For most cases, [HugrInputArgs::get_package] should be called instead.
63+
/// For most cases, [`HugrInputArgs::get_package`] should be called instead.
6464
pub fn get_hugr(&mut self) -> Result<Hugr, CliError> {
6565
let extensions = self.load_extensions()?;
6666
let mut buffer = BufReader::new(&mut self.input);
@@ -79,8 +79,8 @@ impl HugrInputArgs {
7979

8080
/// Return a register with the selected extensions.
8181
///
82-
/// This includes the standard extensions if [HugrInputArgs::no_std] is `false`,
83-
/// and the extensions loaded from the paths in [HugrInputArgs::extensions].
82+
/// This includes the standard extensions if [`HugrInputArgs::no_std`] is `false`,
83+
/// and the extensions loaded from the paths in [`HugrInputArgs::extensions`].
8484
pub fn load_extensions(&self) -> Result<ExtensionRegistry, CliError> {
8585
let mut reg = if self.no_std {
8686
hugr::extension::PRELUDE_REGISTRY.to_owned()

hugr-cli/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
//! [INPUT] Input file. Defaults to `-` for stdin.
5757
//! ```
5858
59-
use clap::{crate_version, Parser};
59+
use clap::{Parser, crate_version};
6060
use clap_verbosity_flag::log::Level;
6161
use clap_verbosity_flag::{InfoLevel, Verbosity};
6262
use hugr::envelope::EnvelopeError;
@@ -119,6 +119,7 @@ pub struct OtherArgs {
119119

120120
impl OtherArgs {
121121
/// Test whether a `level` message should be output.
122+
#[must_use]
122123
pub fn verbosity(&self, level: Level) -> bool {
123124
self.verbose.log_level_filter() >= level
124125
}

hugr-cli/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use clap::Parser as _;
44

5-
use hugr_cli::{mermaid, validate, CliArgs};
5+
use hugr_cli::{CliArgs, mermaid, validate};
66

77
use clap_verbosity_flag::log::Level;
88

@@ -22,7 +22,7 @@ fn main() {
2222
eprintln!("Unknown command");
2323
std::process::exit(1);
2424
}
25-
};
25+
}
2626
}
2727

2828
/// Run the `validate` subcommand.
@@ -31,7 +31,7 @@ fn run_validate(mut args: validate::ValArgs) {
3131

3232
if let Err(e) = result {
3333
if args.verbosity(Level::Error) {
34-
eprintln!("{}", e);
34+
eprintln!("{e}");
3535
}
3636
std::process::exit(1);
3737
}
@@ -43,7 +43,7 @@ fn run_mermaid(mut args: mermaid::MermaidArgs) {
4343

4444
if let Err(e) = result {
4545
if args.other_args.verbosity(Level::Error) {
46-
eprintln!("{}", e);
46+
eprintln!("{e}");
4747
}
4848
std::process::exit(1);
4949
}

hugr-cli/src/mermaid.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use std::io::Write;
44
use clap::Parser;
55
use clap_verbosity_flag::log::Level;
66
use clio::Output;
7-
use hugr::package::PackageValidationError;
87
use hugr::HugrView;
8+
use hugr::package::PackageValidationError;
99

10-
use crate::hugr_io::HugrInputArgs;
1110
use crate::OtherArgs;
11+
use crate::hugr_io::HugrInputArgs;
1212

1313
/// Dump the standard extensions.
1414
#[derive(Parser, Debug)]
@@ -39,9 +39,10 @@ pub struct MermaidArgs {
3939
impl MermaidArgs {
4040
/// Write the mermaid diagram to the output.
4141
pub fn run_print(&mut self) -> Result<(), crate::CliError> {
42-
match self.input_args.hugr_json {
43-
true => self.run_print_hugr(),
44-
false => self.run_print_envelope(),
42+
if self.input_args.hugr_json {
43+
self.run_print_hugr()
44+
} else {
45+
self.run_print_envelope()
4546
}
4647
}
4748

@@ -73,6 +74,7 @@ impl MermaidArgs {
7374
}
7475

7576
/// Test whether a `level` message should be output.
77+
#[must_use]
7678
pub fn verbosity(&self, level: Level) -> bool {
7779
self.other_args.verbosity(level)
7880
}

hugr-cli/src/validate.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,26 @@ pub const VALID_PRINT: &str = "HUGR valid!";
3030
impl ValArgs {
3131
/// Run the HUGR cli and validate against an extension registry.
3232
pub fn run(&mut self) -> Result<Vec<Hugr>, CliError> {
33-
let result = match self.input_args.hugr_json {
34-
true => {
35-
let hugr = self.input_args.get_hugr()?;
36-
hugr.validate()
37-
.map_err(PackageValidationError::Validation)?;
38-
vec![hugr]
39-
}
40-
false => {
41-
let package = self.input_args.get_package()?;
42-
package.validate()?;
43-
package.modules
44-
}
33+
let result = if self.input_args.hugr_json {
34+
let hugr = self.input_args.get_hugr()?;
35+
hugr.validate()
36+
.map_err(PackageValidationError::Validation)?;
37+
vec![hugr]
38+
} else {
39+
let package = self.input_args.get_package()?;
40+
package.validate()?;
41+
package.modules
4542
};
4643

4744
if self.verbosity(Level::Info) {
48-
eprintln!("{}", VALID_PRINT);
45+
eprintln!("{VALID_PRINT}");
4946
}
5047

5148
Ok(result)
5249
}
5350

5451
/// Test whether a `level` message should be output.
52+
#[must_use]
5553
pub fn verbosity(&self, level: Level) -> bool {
5654
self.other_args.verbosity(level)
5755
}

hugr-cli/tests/gen_extensions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn test_extension_dump(mut cmd: Command) {
3636
"collections/list.json",
3737
];
3838
// check all paths exist
39-
for path in expected_paths.iter() {
39+
for path in &expected_paths {
4040
let full_path = temp_dir.join(path);
4141
assert!(full_path.exists());
4242
}

hugr-cli/tests/validate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![cfg(all(test, not(miri)))]
66

77
use assert_cmd::Command;
8-
use assert_fs::{fixture::FileWriteStr, NamedTempFile};
8+
use assert_fs::{NamedTempFile, fixture::FileWriteStr};
99
use hugr::builder::{DFGBuilder, DataflowSubContainer, ModuleBuilder};
1010
use hugr::envelope::EnvelopeConfig;
1111
use hugr::package::Package;

hugr-core/src/builder.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
//! Depending on the type of HUGR you want to build, you may want to use one of
66
//! the following builders:
77
//!
8-
//! - [ModuleBuilder]: For building a module with function declarations and
8+
//! - [`ModuleBuilder`]: For building a module with function declarations and
99
//! definitions.
10-
//! - [DFGBuilder]: For building a dataflow graph.
11-
//! - [FunctionBuilder]: A `DFGBuilder` specialised in defining functions with a
10+
//! - [`DFGBuilder`]: For building a dataflow graph.
11+
//! - [`FunctionBuilder`]: A `DFGBuilder` specialised in defining functions with a
1212
//! dataflow graph.
13-
//! - [CFGBuilder]: For building a control flow graph.
14-
//! - [ConditionalBuilder]: For building a conditional node.
15-
//! - [TailLoopBuilder]: For building a tail-loop node.
13+
//! - [`CFGBuilder`]: For building a control flow graph.
14+
//! - [`ConditionalBuilder`]: For building a conditional node.
15+
//! - [`TailLoopBuilder`]: For building a tail-loop node.
1616
//!
17-
//! Additionally, the [CircuitBuilder] provides an alternative to the
18-
//! [DFGBuilder] when working with circuits, where some inputs of operations directly
17+
//! Additionally, the [`CircuitBuilder`] provides an alternative to the
18+
//! [`DFGBuilder`] when working with circuits, where some inputs of operations directly
1919
//! correspond to some outputs and operations can be directly appended using
2020
//! unit indices.
2121
//!
@@ -87,8 +87,8 @@
8787
//! ```
8888
use thiserror::Error;
8989

90-
use crate::extension::simple_op::OpLoadError;
9190
use crate::extension::SignatureError;
91+
use crate::extension::simple_op::OpLoadError;
9292
use crate::hugr::ValidationError;
9393
use crate::ops::handle::{BasicBlockID, CfgID, ConditionalID, DfgID, FuncID, TailLoopID};
9494
use crate::ops::{NamedOp, OpType};
@@ -122,12 +122,12 @@ pub use conditional::{CaseBuilder, ConditionalBuilder};
122122
mod circuit;
123123
pub use circuit::{CircuitBuildError, CircuitBuilder};
124124

125-
/// Return a FunctionType with the same input and output types (specified).
125+
/// Return a `FunctionType` with the same input and output types (specified).
126126
pub fn endo_sig(types: impl Into<TypeRow>) -> Signature {
127127
Signature::new_endo(types)
128128
}
129129

130-
/// Return a FunctionType with the specified input and output types.
130+
/// Return a `FunctionType` with the specified input and output types.
131131
pub fn inout_sig(inputs: impl Into<TypeRow>, outputs: impl Into<TypeRow>) -> Signature {
132132
Signature::new(inputs, outputs)
133133
}
@@ -139,8 +139,8 @@ pub enum BuildError {
139139
/// The constructed HUGR is invalid.
140140
#[error("The constructed HUGR is invalid: {0}.")]
141141
InvalidHUGR(#[from] ValidationError<Node>),
142-
/// SignatureError in trying to construct a node (differs from
143-
/// [ValidationError::SignatureError] in that we could not construct a node to report about)
142+
/// `SignatureError` in trying to construct a node (differs from
143+
/// [`ValidationError::SignatureError`] in that we could not construct a node to report about)
144144
#[error(transparent)]
145145
SignatureError(#[from] SignatureError),
146146
/// Tried to add a malformed [Const]
@@ -152,8 +152,10 @@ pub enum BuildError {
152152
#[error("CFG entry node already built for CFG node: {0}.")]
153153
EntryBuiltError(Node),
154154
/// We don't allow creating `BasicBlockBuilder<Hugr>`s when the sum-rows
155-
/// are not homogeneous. Use a CFGBuilder and create a valid graph instead.
156-
#[error("Cannot initialize hugr for a BasicBlockBuilder with complex sum-rows. Use a CFGBuilder instead.")]
155+
/// are not homogeneous. Use a `CFGBuilder` and create a valid graph instead.
156+
#[error(
157+
"Cannot initialize hugr for a BasicBlockBuilder with complex sum-rows. Use a CFGBuilder instead."
158+
)]
157159
BasicBlockTooComplex,
158160
/// Node was expected to have a certain type but was found to not.
159161
#[error("Node with index {node} does not have type {op_desc} as expected.")]
@@ -179,7 +181,7 @@ pub enum BuildError {
179181
#[error("Wire not found in Hugr: {0}.")]
180182
WireNotFound(Wire),
181183

182-
/// Error in CircuitBuilder
184+
/// Error in `CircuitBuilder`
183185
#[error("Error in CircuitBuilder: {0}.")]
184186
CircuitError(#[from] circuit::CircuitBuildError),
185187

@@ -222,7 +224,9 @@ pub enum BuilderWiringError {
222224
src_offset: Port,
223225
},
224226
/// The ancestors of an inter-graph edge are not related.
225-
#[error("Cannot connect an inter-graph edge between unrelated nodes. Tried connecting {src} ({src_offset}) with {dst} ({dst_offset}).")]
227+
#[error(
228+
"Cannot connect an inter-graph edge between unrelated nodes. Tried connecting {src} ({src_offset}) with {dst} ({dst_offset})."
229+
)]
226230
#[allow(missing_docs)]
227231
NoRelationIntergraph {
228232
src: Node,
@@ -231,7 +235,9 @@ pub enum BuilderWiringError {
231235
dst_offset: Port,
232236
},
233237
/// Inter-Graph edges can only carry copyable data.
234-
#[error("Inter-graph edges cannot carry non-copyable data {typ}. Tried connecting {src} ({src_offset}) with {dst} ({dst_offset}).")]
238+
#[error(
239+
"Inter-graph edges cannot carry non-copyable data {typ}. Tried connecting {src} ({src_offset}) with {dst} ({dst_offset})."
240+
)]
235241
#[allow(missing_docs)]
236242
NonCopyableIntergraph {
237243
src: Node,
@@ -246,12 +252,12 @@ pub enum BuilderWiringError {
246252
pub(crate) mod test {
247253
use rstest::fixture;
248254

255+
use crate::Hugr;
249256
use crate::extension::prelude::{bool_t, usize_t};
250-
use crate::hugr::{views::HugrView, HugrMut};
257+
use crate::hugr::{HugrMut, views::HugrView};
251258
use crate::ops;
252259
use crate::package::Package;
253260
use crate::types::{PolyFuncType, Signature};
254-
use crate::Hugr;
255261

256262
use super::handle::BuildHandle;
257263
use super::{

0 commit comments

Comments
 (0)