-
Notifications
You must be signed in to change notification settings - Fork 7
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!: Allow CustomConsts to (optionally) be hashable #1397
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1397 +/- ##
=======================================
Coverage 87.28% 87.28%
=======================================
Files 128 128
Lines 21721 21774 +53
Branches 18721 18774 +53
=======================================
+ Hits 18959 19006 +47
- Misses 1990 1996 +6
Partials 772 772
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
This is not really needed yet but seems like a very good thing to have for dataflow analysis and not a bad idea anyway: most constants are hashable! The approach taken with MaybeHash contrasts with Hence, picking @aborgna-q out here as he may know of better/rustier ways here. Can we |
hugr-core/src/ops/constant/custom.rs
Outdated
/// Note: this uses `dyn` rather than being parametrized by `<H: Hasher>` so that we can | ||
/// still use `dyn CustomConst`. |
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.
I.e. wee need the trait to be object safe
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.
Comment updated.
There is no reflection in Rust, so there's no way to branch on whether a type implements a trait or not (not with stable code at least). This looks like a good alternative. We can discuss whether to define a new trait vs adding another trait method to I may suggest renaming it to |
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.
Drop all those allocations!
hugr-core/src/ops/constant/custom.rs
Outdated
fn maybe_hash(&self, st: &mut dyn Hasher) -> bool { | ||
Hash::hash(self, &mut Box::new(st)); |
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.
fn maybe_hash(&self, st: &mut dyn Hasher) -> bool { | |
Hash::hash(self, &mut Box::new(st)); | |
fn maybe_hash(&self, mut st: &mut dyn Hasher) -> bool { | |
Hash::hash(self, &mut st); |
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.
That gives me:
error[E0277]: the size for values of type `dyn Hasher` cannot be known at compilation time
--> hugr-core/src/ops/constant/custom.rs:113:26
|
113 | Hash::hash(self, &mut *st);
| ---------- ^^^^^^^^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: the trait `Sized` is not implemented for `dyn Hasher`
which I admit is odd - I'm not trying to pass a Hasher, only a &mut
to it. Am I missing something?
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.
Ah sorry I'd put in an extra *
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.
*st
is trying to dereference an unsized value into the stack.
The suggestion avoids that by using T : Hasher
&mut T : Hasher
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.
This does look to me as if I'm passing a mutable reference to a mutable reference!
I like Also I admit I'm not really clear why we don't require PartialEq as a prereq for CustomConst. You can always implement with |
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.
Patch coverage is 4.76190%
🫠
Sorry, I really did think there was already some code hashing lists. Maybe I got confused with stuff in a previous iteration of "const_fold2" 😅 ...anyway, I've added enough to make it green, could dup much of that into collections too I suppose to get a final few percent. |
## 🤖 New release * `hugr`: 0.12.1 -> 0.13.0 (✓ API compatible changes) * `hugr-core`: 0.9.1 -> 0.10.0 (✓ API compatible changes) * `hugr-passes`: 0.8.1 -> 0.8.2 (✓ API compatible changes) * `hugr-cli`: 0.6.0 -> 0.6.1 (✓ API compatible changes) <details><summary><i><b>Changelog</b></i></summary><p> ## `hugr` <blockquote> ## 0.13.0 (2024-10-08) ### Bug Fixes - [**breaking**] Make list length op give back the list ([#1547](#1547)) ### Features - [**breaking**] Allow CustomConsts to (optionally) be hashable ([#1397](#1397)) - Add an `OpLoadError` variant of `BuildError`. ([#1537](#1537)) - [**breaking**] `HugrMut::remove_node` and `SimpleReplacement` return removed weights ([#1516](#1516)) - Draft for `hugr-model` with export, import, parsing and pretty printing ([#1542](#1542)) </blockquote> ## `hugr-core` <blockquote> ## 0.10.0 (2024-10-08) ### Bug Fixes - [**breaking**] Make list length op give back the list ([#1547](#1547)) ### Features - [**breaking**] Allow CustomConsts to (optionally) be hashable ([#1397](#1397)) - Add an `OpLoadError` variant of `BuildError`. ([#1537](#1537)) - [**breaking**] `HugrMut::remove_node` and `SimpleReplacement` return removed weights ([#1516](#1516)) - Draft for `hugr-model` with export, import, parsing and pretty printing ([#1542](#1542)) </blockquote> ## `hugr-passes` <blockquote> ## 0.8.1 (2024-09-04) ### Features - Op replacement and lowering functions ([#1509](#1509)) </blockquote> ## `hugr-cli` <blockquote> ## 0.6.0 (2024-09-04) ### Features - [**breaking**] Allow registry specification in `run_dump` ([#1501](#1501)) - [**breaking**] Add `Package::validate` and return `ExtensionRegistry` in helpers. ([#1507](#1507)) </blockquote> </p></details> --- This PR was generated with [release-plz](https://github.com/MarcoIeni/release-plz/).
impl TryHash for Foo { }
) to say "no, not hashable"BREAKING CHANGE: any
impl CustomConst
will need to eitherimpl Hash
orimpl MaybeHash