-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add trait+funcs for linking Hugrs explicitly by Node #2521
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
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2521 +/- ##
==========================================
+ Coverage 82.75% 82.94% +0.19%
==========================================
Files 252 254 +2
Lines 47065 47590 +525
Branches 42581 43101 +520
==========================================
+ Hits 38950 39475 +525
+ Misses 6055 6052 -3
- Partials 2060 2063 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
3a47eb3 to
5774665
Compare
|
|
| transfers: Transfers<SN, TGT::Node>, | ||
| node_map: &mut HashMap<SN, TGT::Node>, | ||
| ) { | ||
| // Resolve `use_existing` first in case the existing node is also replaced by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No test yet, coming up...(EDIT: added)
Alternatively we could error if the same existing-node is target of both a replace and a UseExisting.
|
Ok, #2518 now merged into main, and I hope that's fixed a number of issues here. As mentioned, I've gone with |
| /// | ||
| /// This is done by module-children from the inserted (source) Hugr replacing, or being replaced by, | ||
| /// module-children already in the target Hugr; static edges from the replaced node, | ||
| /// are transferred to come from the replacing node, and the replaced node(/subtree) then deleted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent, I think HugrLinking is very good.
hugr-core/src/hugr/linking.rs
Outdated
| /// If `parent` is `Some` but not in the graph. | ||
| #[allow(clippy::type_complexity)] | ||
| fn insert_from_view_link_nodes<H: HugrView>( | ||
| fn add_view_link_nodes<H: HugrView>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, good name proposals!
-
I prefer
inserttoadd(addsounds to me like adding an element to a container, whereasinsertis a more complex process?) I admit this is subjective, so up to you. -
Given your explanation, I think a good suffix for the names would be
_by_nodes. Names withbyare pretty idiomatic. -
If you say linking by name will be the most common use case, then I am not fussed if the method names for linking explicitly by node are quite verbose. As you suggest I would just drop the
by_nodessuffix for the link-by-name versions.
So maybe insert_link_view_by_nodes and insert_link_hugr_by_nodes? I don't know at this point 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so
- I think you're right about
insertrather thanadd: I think "insert" is "put the entrypoint-subtree under a new parent" whereas "link" is "do stuff with the module children" (only). Using "add" for "maybe insert" is a faux pas, will just go with insert. - Let's also establish the "link....by_nodes" convention (foreshadowing upcoming+shorter "link...." that's by name), replacing current "...._link_nodes".
- I also think rejecting names for being too long may be a bad move here. There's an old idea that the more a method does, the longer its name should be. These methods do quite a lot, so long names are at least ok....
- Hence: I propose:
HugrMut::insert_link_hugr_by_node(and s/hugr/view/), Dataflow builderadd_link_hugr_by_node(where add in the builder means insert elsewhere but nvm), Module builderlink_hugr_by_node(without the add: it only does stuff with module children).
Will enact shortly unless you object @lmondada...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, by_node singular or plural? I think I favour singular
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I like it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, done. insert_link_view_.... isn't great because there is also a thing called a LinkView but it is portgraph really.
And the Dataflow builder now has add_link_hugr_by_node_with_wires which is a lot of suffixes 👎 so better ideas welcome, but add.....with_wires_by_node is worse (it's not the wires that are by node it's the "link"!)
## 🤖 New release
* `hugr-model`: 0.22.4 -> 0.23.0 (✓ API compatible changes)
* `hugr-core`: 0.22.4 -> 0.23.0 (⚠ API breaking changes)
* `hugr-llvm`: 0.22.4 -> 0.23.0 (✓ API compatible changes)
* `hugr-passes`: 0.22.4 -> 0.23.0 (⚠ API breaking changes)
* `hugr-persistent`: 0.2.3 -> 0.3.0 (✓ API compatible changes)
* `hugr`: 0.22.4 -> 0.23.0 (✓ API compatible changes)
* `hugr-cli`: 0.22.4 -> 0.23.0 (✓ API compatible changes)
### ⚠ `hugr-core` breaking changes
```text
--- failure enum_variant_missing: pub enum variant removed or renamed ---
Description:
A publicly-visible enum has at least one variant that is no longer available under its prior name. It may have been renamed or removed entirely.
ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.43.0/src/lints/enum_variant_missing.ron
Failed in:
variant PackageEncodingError::ExtensionVersion, previously in file /tmp/.tmp8eoQo1/hugr-core/src/envelope/package_json.rs:89
--- failure function_parameter_count_changed: pub fn parameter count changed ---
Description:
A publicly-visible function now takes a different number of parameters.
ref: https://doc.rust-lang.org/cargo/reference/semver.html#fn-change-arity
impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.43.0/src/lints/function_parameter_count_changed.ron
Failed in:
hugr_core::import::import_package now takes 3 parameters instead of 2, in /tmp/.tmpBTXSed/hugr/hugr-core/src/import.rs:187
--- failure struct_missing: pub struct removed or renamed ---
Description:
A publicly-visible struct cannot be imported by its prior path. A `pub use` may have been removed, or the struct itself may have been renamed or removed entirely.
ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.43.0/src/lints/struct_missing.ron
Failed in:
struct hugr_core::extension::prelude::PRELUDE_REGISTRY, previously in file /tmp/.tmp8eoQo1/hugr-core/src/extension/prelude.rs:43
struct hugr_core::extension::PRELUDE_REGISTRY, previously in file /tmp/.tmp8eoQo1/hugr-core/src/extension/prelude.rs:43
struct hugr_core::std_extensions::logic::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/logic.rs:134
struct hugr_core::extension::prelude::PRELUDE, previously in file /tmp/.tmp8eoQo1/hugr-core/src/extension/prelude.rs:43
struct hugr_core::extension::PRELUDE, previously in file /tmp/.tmp8eoQo1/hugr-core/src/extension/prelude.rs:43
struct hugr_core::std_extensions::ptr::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/ptr.rs:112
struct hugr_core::std_extensions::arithmetic::int_types::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/arithmetic/int_types.rs:209
struct hugr_core::std_extensions::arithmetic::conversions::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/arithmetic/conversions.rs:174
struct hugr_core::std_extensions::collections::array::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/collections/array.rs:93
struct hugr_core::std_extensions::arithmetic::int_types::INT_TYPES, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/arithmetic/int_types.rs:49
struct hugr_core::std_extensions::arithmetic::float_types::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/arithmetic/float_types.rs:104
struct hugr_core::std_extensions::collections::static_array::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/collections/static_array.rs:142
struct hugr_core::std_extensions::collections::list::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/collections/list.rs:289
struct hugr_core::std_extensions::STD_REG, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions.rs:35
struct hugr_core::std_extensions::collections::value_array::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/collections/value_array.rs:99
struct hugr_core::std_extensions::collections::borrow_array::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/collections/borrow_array.rs:290
struct hugr_core::std_extensions::arithmetic::float_ops::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/arithmetic/float_ops.rs:115
struct hugr_core::std_extensions::arithmetic::int_ops::EXTENSION, previously in file /tmp/.tmp8eoQo1/hugr-core/src/std_extensions/arithmetic/int_ops.rs:254
```
### ⚠ `hugr-passes` breaking changes
```text
--- failure enum_missing: pub enum removed or renamed ---
Description:
A publicly-visible enum cannot be imported by its prior path. A `pub use` may have been removed, or the enum itself may have been renamed or removed entirely.
ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.43.0/src/lints/enum_missing.ron
Failed in:
enum hugr_passes::non_local::NonLocalEdgesError, previously in file /tmp/.tmp8eoQo1/hugr-passes/src/non_local.rs:57
```
<details><summary><i><b>Changelog</b></i></summary><p>
## `hugr-model`
<blockquote>
##
[0.23.0](hugr-model-v0.22.4...hugr-model-v0.23.0)
- 2025-09-30
### Bug Fixes
- [**breaking**] Appease `cargo-audit` by replacing unmaintained
dependencies ([#2572](#2572))
### New Features
- Documentation and error hints
([#2523](#2523))
</blockquote>
## `hugr-core`
<blockquote>
##
[0.23.0](hugr-core-v0.22.4...hugr-core-v0.23.0)
- 2025-09-30
### Bug Fixes
- [**breaking**] Appease `cargo-audit` by replacing unmaintained
dependencies ([#2572](#2572))
- *(core)* check extension versions on model import
([#2580](#2580))
- [**breaking**] test extension version compatibility on ModelWithExts
([#2587](#2587))
- *(core)* check used extension versions against resolved extensions
([#2588](#2588))
- [**breaking**] model import loads Package extensions
([#2590](#2590))
### Miscellaneous Tasks
- [**breaking**] Cleanup deprecated definitions
([#2594](#2594))
### New Features
- add trait+funcs for linking Hugrs explicitly by Node
([#2521](#2521))
- Documentation and error hints
([#2523](#2523))
- Allow creating DFG builders from existing hugrs
([#2562](#2562))
- add_input/output for arbitrary DFGBuilders
([#2564](#2564))
- [**breaking**] Return error instead of panicking in
DFGWrapper::add_{in,out}put
([#2571](#2571))
- *(core)* inner acccesors for WithGenerator error
([#2583](#2583))
- Normalize CFGs ([#2591](#2591))
### Refactor
- [**breaking**] Replace lazy_static with std::sync::LazyLock
([#2567](#2567))
</blockquote>
## `hugr-llvm`
<blockquote>
##
[0.23.0](hugr-llvm-v0.22.4...hugr-llvm-v0.23.0)
- 2025-09-30
### Miscellaneous Tasks
- [**breaking**] Cleanup deprecated definitions
([#2594](#2594))
### Refactor
- [**breaking**] Replace lazy_static with std::sync::LazyLock
([#2567](#2567))
### Testing
- Add framework for LLVM execution tests involving panics
([#2568](#2568))
</blockquote>
## `hugr-passes`
<blockquote>
##
[0.23.0](hugr-passes-v0.22.4...hugr-passes-v0.23.0)
- 2025-09-30
### Bug Fixes
- DeadCodeElim keeps consumers of linear outputs
([#2560](#2560))
- [**breaking**] Appease `cargo-audit` by replacing unmaintained
dependencies ([#2572](#2572))
### Miscellaneous Tasks
- [**breaking**] Cleanup deprecated definitions
([#2594](#2594))
### New Features
- [**breaking**] DeadCodeElimPass reports error on non-existent
entry_points ([#2566](#2566))
- Normalize CFGs ([#2591](#2591))
### Refactor
- [**breaking**] Replace lazy_static with std::sync::LazyLock
([#2567](#2567))
</blockquote>
## `hugr-persistent`
<blockquote>
##
[0.3.0](hugr-persistent-v0.2.3...hugr-persistent-v0.3.0)
- 2025-09-30
### Miscellaneous Tasks
- [**breaking**] Cleanup deprecated definitions
([#2594](#2594))
### Refactor
- [**breaking**] Replace lazy_static with std::sync::LazyLock
([#2567](#2567))
</blockquote>
## `hugr`
<blockquote>
##
[0.23.0](hugr-v0.22.4...hugr-v0.23.0)
- 2025-09-30
### Bug Fixes
- DeadCodeElim keeps consumers of linear outputs
([#2560](#2560))
- [**breaking**] Appease `cargo-audit` by replacing unmaintained
dependencies ([#2572](#2572))
- *(core)* check extension versions on model import
([#2580](#2580))
- [**breaking**] test extension version compatibility on ModelWithExts
([#2587](#2587))
- *(core)* check used extension versions against resolved extensions
([#2588](#2588))
- [**breaking**] model import loads Package extensions
([#2590](#2590))
### Miscellaneous Tasks
- [**breaking**] Cleanup deprecated definitions
([#2594](#2594))
### New Features
- [**breaking**] DeadCodeElimPass reports error on non-existent
entry_points ([#2566](#2566))
- add trait+funcs for linking Hugrs explicitly by Node
([#2521](#2521))
- Documentation and error hints
([#2523](#2523))
- Allow creating DFG builders from existing hugrs
([#2562](#2562))
- add_input/output for arbitrary DFGBuilders
([#2564](#2564))
- [**breaking**] Return error instead of panicking in
DFGWrapper::add_{in,out}put
([#2571](#2571))
- *(core)* inner acccesors for WithGenerator error
([#2583](#2583))
- Normalize CFGs ([#2591](#2591))
### Refactor
- [**breaking**] Replace lazy_static with std::sync::LazyLock
([#2567](#2567))
</blockquote>
## `hugr-cli`
<blockquote>
##
[0.22.3](hugr-cli-v0.22.2...hugr-cli-v0.22.3)
- 2025-09-11
### New Features
- *(hugr-cli)* CliError::validate helper
([#2507](#2507))
</blockquote>
</p></details>
---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).
Closes #2355.
This adds a new
trait HugrLinking: HugrMutwith methodsinsert_link_hugr_by_nodeandinsert_link_view_by_nodethat allow inserting a Hugr/View intoself- the entrypoint subtree (anywhere) and/or the module-children (under the module-root ofself). An error is raised if you're trying to insert the entrypoint-subtree into two places. The added nodes can be linked i.e. static edges added from module-children to uses, both from new to existing or vice versa. The new methods are default-implemented on top ofHugrMut::insert_(view_)forest, and there's a blanket impl for anyHugrMut, so the trait is just to keep these methods separated out.Note that it would be possible to deprecate
insert_hugrnow, although I have not:insert_link_hugr_by_node(other, Some(parent), HashMap::new())does the same job (explicitly not copying any module-children), and returns an error, which is probably what we want.