Skip to content
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

feat!: Add associated type Node to HugrView #1932

Merged
merged 8 commits into from
Feb 27, 2025
Merged

feat!: Add associated type Node to HugrView #1932

merged 8 commits into from
Feb 27, 2025

Conversation

lmondada
Copy link
Contributor

@lmondada lmondada commented Feb 24, 2025

closes #1927

Yey, this is a fun one 🫠

This makes HugrView general over the Node type. Note that HugrMut and all operations that mutate hugrs still only act on the node type Node.

I suggest once this is merged in that we add a .git-blame-ignore-revs file to simplify future git blames:

# Commits to be ignored by git blame

# Add Node associated type to HugrView (large mechanical change)
<commit of this PR on main>

BREAKING CHANGE: HugrView now has a Node associated type. Make your code generic over the node type where you can, replacing Node with H::Node. Otherwise, use the type bound H: HugrView<Node = Node>.

@lmondada lmondada requested a review from a team as a code owner February 24, 2025 17:54
@lmondada lmondada requested a review from cqc-alec February 24, 2025 17:54
Copy link

codecov bot commented Feb 24, 2025

Codecov Report

Attention: Patch coverage is 86.83729% with 72 lines in your changes missing coverage. Please review.

Project coverage is 83.69%. Comparing base (5e5cfcc) to head (daa1f67).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
hugr-core/src/hugr/internal.rs 36.00% 16 Missing ⚠️
hugr-core/src/hugr/views/impls.rs 65.90% 15 Missing ⚠️
hugr-llvm/src/utils/fat.rs 65.85% 14 Missing ⚠️
hugr-passes/src/const_fold.rs 65.00% 7 Missing ⚠️
hugr-passes/src/dataflow.rs 66.66% 4 Missing ⚠️
hugr-core/src/hugr/views/descendants.rs 90.32% 0 Missing and 3 partials ⚠️
hugr-core/src/hugr/views/root_checked.rs 33.33% 2 Missing ⚠️
hugr-core/src/hugr/views/sibling.rs 96.00% 0 Missing and 2 partials ⚠️
hugr-core/src/hugr/views/sibling_subgraph.rs 96.96% 2 Missing ⚠️
hugr-core/src/core.rs 66.66% 1 Missing ⚠️
... and 6 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1932      +/-   ##
==========================================
- Coverage   83.70%   83.69%   -0.02%     
==========================================
  Files         196      196              
  Lines       37543    37691     +148     
  Branches    34356    34504     +148     
==========================================
+ Hits        31425    31545     +120     
- Misses       4335     4360      +25     
- Partials     1783     1786       +3     
Flag Coverage Δ
python 92.34% <ø> (ø)
rust 82.89% <86.83%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hugrbot
Copy link
Collaborator

hugrbot commented Feb 24, 2025

This PR contains breaking changes to the public Rust API.

cargo-semver-checks summary

--- failure method_requires_different_generic_type_params: method now requires a different number of generic type parameters ---

Description:
A method now requires a different number of generic type parameters than it used to. Uses of this method that supplied the previous number of generic types will be broken.
      ref: https://doc.rust-lang.org/reference/items/generics.html
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.39.0/src/lints/method_requires_different_generic_type_params.ron

Failed in:
hugr_core::hugr::views::sibling_subgraph::SiblingSubgraph::try_from_nodes_with_checker takes 0 generic types instead of 1, in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/hugr/views/sibling_subgraph.rs:234
hugr_core::hugr::views::SiblingSubgraph::try_from_nodes_with_checker takes 0 generic types instead of 1, in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/hugr/views/sibling_subgraph.rs:234

--- failure trait_associated_type_added: non-sealed public trait added associated type without default value ---

Description:
A non-sealed trait has gained an associated type without a default value, which breaks downstream implementations of the trait
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-item-no-default
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.39.0/src/lints/trait_associated_type_added.ron

Failed in:
trait associated type hugr_core::hugr::internal::HugrInternals::Node in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/hugr/internal.rs:30

--- failure trait_method_added: pub trait method added ---

Description:
A non-sealed public trait added a new method without a default implementation, which breaks downstream implementations of the trait
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-item-no-default
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.39.0/src/lints/trait_method_added.ron

Failed in:
trait method hugr_core::hugr::internal::HugrInternals::get_pg_index in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/hugr/internal.rs:42
trait method hugr_core::hugr::internal::HugrInternals::get_node in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/hugr/internal.rs:45

--- failure trait_requires_more_generic_type_params: trait now requires more generic type parameters ---

Description:
A trait now requires more generic type parameters than it used to. Uses of this trait that supplied the previously-required number of generic types will be broken. To fix this, consider supplying default values for newly-added generic types.
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-parameter-no-default
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.39.0/src/lints/trait_requires_more_generic_type_params.ron

Failed in:
trait HugrNodeRef (0 -> 1 required generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/hugr/views/petgraph.rs:187

--- failure type_requires_more_generic_type_params: type now requires more generic type parameters ---

Description:
A type now requires more generic type parameters than it used to. Uses of this type that supplied the previously-required number of generic types will be broken. To fix this, consider supplying default values for newly-added generic types.
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-parameter-no-default
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.39.0/src/lints/type_requires_more_generic_type_params.ron

Failed in:
Struct HugrNodeRef (0 -> 1 required generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/hugr/views/petgraph.rs:187

--- failure function_requires_different_generic_type_params: function now requires a different number of generic type parameters ---

Description:
A function now requires a different number of generic type parameters than it used to. Uses of this function that supplied the previous number of generic types will be broken.
      ref: https://doc.rust-lang.org/reference/items/generics.html
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.39.0/src/lints/function_requires_different_generic_type_params.ron

Failed in:
function ensure_no_nonlocal_edges (0 -> 1 generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-passes/src/non_local.rs:32
function ensure_no_nonlocal_edges (0 -> 1 generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-passes/src/non_local.rs:32
function nonlocal_edges (0 -> 1 generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-passes/src/non_local.rs:14
function nonlocal_edges (0 -> 1 generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-passes/src/non_local.rs:14
function partial_from_const (1 -> 2 generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-passes/src/dataflow.rs:93

--- failure trait_associated_type_added: non-sealed public trait added associated type without default value ---

Description:
A non-sealed trait has gained an associated type without a default value, which breaks downstream implementations of the trait
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-item-no-default
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.39.0/src/lints/trait_associated_type_added.ron

Failed in:
trait associated type hugr_passes::dataflow::ConstLoader::Node in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-passes/src/dataflow.rs:63

--- failure trait_requires_more_generic_type_params: trait now requires more generic type parameters ---

Description:
A trait now requires more generic type parameters than it used to. Uses of this trait that supplied the previously-required number of generic types will be broken. To fix this, consider supplying default values for newly-added generic types.
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-parameter-no-default
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.39.0/src/lints/trait_requires_more_generic_type_params.ron

Failed in:
trait NonLocalEdgesError (0 -> 1 required generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-passes/src/non_local.rs:26
trait ConstLocation (0 -> 1 required generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-passes/src/dataflow.rs:44

--- failure type_requires_more_generic_type_params: type now requires more generic type parameters ---

Description:
A type now requires more generic type parameters than it used to. Uses of this type that supplied the previously-required number of generic types will be broken. To fix this, consider supplying default values for newly-added generic types.
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-parameter-no-default
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.39.0/src/lints/type_requires_more_generic_type_params.ron

Failed in:
Enum NonLocalEdgesError (0 -> 1 required generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-passes/src/non_local.rs:26
Enum ConstLocation (0 -> 1 required generic types) in /home/runner/work/hugr/hugr/PR_BRANCH/hugr-passes/src/dataflow.rs:44

@lmondada
Copy link
Contributor Author

lmondada commented Feb 25, 2025

@doug-q for hugr-llvm, I have restricted all implementations to Hugr<Node = Node>. I hit a roadblock trying to implement it for any Self::Node in Namer, because it assumes a Node can be converted to an integer index.

I'm sure it would be easy to change but I don't think we need to support more general Node types for LLVM emitting, so this should be harmless.

Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

Comment on lines +53 to +56
pub trait HugrNode: Copy + Ord + std::fmt::Debug + std::fmt::Display + std::hash::Hash {}

impl<T: Copy + Ord + std::fmt::Debug + std::fmt::Display + std::hash::Hash> HugrNode for T {}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we don't have to_pg_index and to_node in the HugrNode trait instead of HugrInternals is that we need context to create one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm expecting that node types that are made of (node id, hugr id) pairs might map in different ways to NodeIndex (otherwise might as well stick with Node)

Comment on lines 41 to 45
/// Convert a node to a portgraph node index.
fn to_pg_index(&self, node: Self::Node) -> portgraph::NodeIndex;

/// Convert a portgraph node index to a node.
fn to_node(&self, index: portgraph::NodeIndex) -> Self::Node;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally to_* methods convert the whole struct into something else.

Here it may be more rusty to have get_node/get_pg_index.

Copy link
Contributor Author

@lmondada lmondada Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will rename!

@lmondada lmondada changed the title chore!: Add associated type Node to HugrView feat!: Add associated type Node to HugrView Feb 27, 2025
@lmondada lmondada enabled auto-merge February 27, 2025 09:15
@lmondada lmondada added this pull request to the merge queue Feb 27, 2025
Merged via the queue into main with commit 50139ff Feb 27, 2025
24 of 26 checks passed
@lmondada lmondada deleted the chore/nodeat branch February 27, 2025 09:24
@hugrbot hugrbot mentioned this pull request Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The Node type should be an associated type of HugrView
4 participants