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!: MakeOpDef has new extension method. #1266

Merged
merged 3 commits into from
Jul 8, 2024
Merged

feat!: MakeOpDef has new extension method. #1266

merged 3 commits into from
Jul 8, 2024

Conversation

ss2165
Copy link
Member

@ss2165 ss2165 commented Jul 5, 2024

Closes #1241

Used to ensure try_from_name only succeeds when the extension is correct

BREAKING CHANGE:

  • MakeOpDef trait has new required extension method.
  • try_from_name takes the OpDef extension and checks it

Used to ensure `try_from_name` only succeeds when the extension is correct

BREAKING CHANGE:
- `MakeOpDef` trait has new required `extension_id` static method.
- `try_from_name` takes an `&OpDef` rather than just names and checks the extension is expected
@ss2165 ss2165 requested a review from doug-q July 5, 2024 14:57
@ss2165 ss2165 requested a review from a team as a code owner July 5, 2024 14:57
Copy link

codecov bot commented Jul 5, 2024

Codecov Report

Attention: Patch coverage is 42.42424% with 19 lines in your changes missing coverage. Please review.

Project coverage is 87.09%. Comparing base (defd2bb) to head (8059d6c).
Report is 2 commits behind head on main.

Files Patch % Lines
hugr-core/src/extension/simple_op.rs 42.85% 7 Missing and 1 partial ⚠️
...-core/src/std_extensions/arithmetic/conversions.rs 0.00% 4 Missing ⚠️
...gr-core/src/std_extensions/arithmetic/float_ops.rs 0.00% 4 Missing ⚠️
hugr-core/src/std_extensions/logic.rs 57.14% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1266      +/-   ##
==========================================
- Coverage   87.15%   87.09%   -0.07%     
==========================================
  Files         103      103              
  Lines       19284    19311      +27     
  Branches    17136    17163      +27     
==========================================
+ Hits        16807    16818      +11     
- Misses       1699     1715      +16     
  Partials      778      778              
Flag Coverage Δ
rust 86.56% <42.42%> (-0.08%) ⬇️

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.

Copy link
Collaborator

@doug-q doug-q left a comment

Choose a reason for hiding this comment

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

I find this code(that was already here!) quite confusing, I may be off track here, but nevertheless:

All callsites of try_from_name get it out of an opdef. All implementations of MakeOpDef::from_def delegate to try_from_name.

How about:

  • try_from_name takes an &ExtensionId and a impl FromStr.
  • extension_id gets a &self parameter

from_def implementations are changed to pass the ExtensionId they each have access to.

This breaks from_def implementations that were previously not checking the extension(all of them).

It means types that implement MakeOpDef are free to be used with multiple extensions. As they were before.

I do think I weakly prefer putting extension_id on NamedOp, but this is also fine.

@@ -138,10 +143,19 @@ impl<T: MakeOpDef> MakeExtensionOp for T {

/// Load an [MakeOpDef] from its name.
/// See [strum_macros::EnumString].
pub fn try_from_name<T>(name: &OpNameRef) -> Result<T, OpLoadError>
pub fn try_from_name<T>(def: &OpDef) -> Result<T, OpLoadError>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure this change makes sense. we already have MakeOpDef::from_def

Copy link
Member Author

Choose a reason for hiding this comment

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

from_def is abstract, MakeOpDef implementers need to have some way of loading themselves from a definition. It so happens that many can load themselves from (name, extension) because they implement FromStr, so we use a specific function for that case (try_from_name).

i dont mind passing name and extension explicitly like you suggest, so I've done that anyway. It's a bit clearer what the job of the function is.

@@ -51,6 +53,9 @@ pub trait MakeOpDef: NamedOp {
/// Return the signature (polymorphic function type) of the operation.
fn signature(&self) -> SignatureFunc;

/// The ID of the extension this operation is defined in.
fn extension_id() -> ExtensionId;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should have where Self: Sized to maintain object-safety.

Copy link
Member Author

Choose a reason for hiding this comment

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

taking &self is a good idea, going with that

@ss2165
Copy link
Member Author

ss2165 commented Jul 5, 2024

I find this code(that was already here!) quite confusing

Agreed, so do I, which is a lot worse given I wrote it. A clean-up is in order but don't want to do that in this PR.

@ss2165 ss2165 requested a review from doug-q July 5, 2024 16:07
@ss2165 ss2165 changed the title feat!: MakeOpDef has new extension_id static method. feat!: MakeOpDef has new extension_id method. Jul 5, 2024
Copy link
Collaborator

@doug-q doug-q left a comment

Choose a reason for hiding this comment

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

Let's rename expected_extension -> extension. I have some op: &impl MakeOpDef, i can ask it it's extension: op.extension().

@@ -51,6 +53,9 @@ pub trait MakeOpDef: NamedOp {
/// Return the signature (polymorphic function type) of the operation.
fn signature(&self) -> SignatureFunc;

/// The ID of the extension this operation is defined in.
fn expected_extension(&self) -> ExtensionId;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
fn expected_extension(&self) -> ExtensionId;
fn extension(&self) -> ExtensionId;

@ss2165 ss2165 requested a review from doug-q July 8, 2024 12:24
@doug-q doug-q changed the title feat!: MakeOpDef has new extension_id method. feat!: MakeOpDef has new extension method. Jul 8, 2024
Copy link
Collaborator

@doug-q doug-q left a comment

Choose a reason for hiding this comment

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

Thanks. I've taken the liberty of updating the PR title and commit message.

@ss2165 ss2165 added this pull request to the merge queue Jul 8, 2024
Merged via the queue into main with commit 75192f7 Jul 8, 2024
21 of 22 checks passed
@ss2165 ss2165 deleted the ss/check-extension branch July 8, 2024 12:32
@hugrbot hugrbot mentioned this pull request Jul 8, 2024
github-merge-queue bot pushed a commit that referenced this pull request Jul 10, 2024
## 🤖 New release
* `hugr`: 0.6.1 -> 0.7.0
* `hugr-core`: 0.3.1 -> 0.4.0
* `hugr-passes`: 0.3.0 -> 0.4.0
* `hugr-cli`: 0.1.2 -> 0.1.3

<details><summary><i><b>Changelog</b></i></summary><p>

## `hugr`
<blockquote>

## 0.7.0 (2024-07-10)

### Bug Fixes

- Bring back input_extensions serialized field in rust NodeSer
([#1275](#1275))
- [**breaking**] `ops::Module` now empty struct rather than unit struct
([#1271](#1271))

### Features

- Add `force_order` pass.
([#1285](#1285))
- [**breaking**] `MakeOpDef` has new `extension` method.
([#1266](#1266))

### Refactor

- [**breaking**] Remove `Value::Tuple`
([#1255](#1255))
- [**breaking**] Rename `HugrView` function type methods + simplify
behaviour ([#1265](#1265))

### Styling

- Change "serialise" etc to "serialize" etc.
([#1251](#1251))

### Testing

- Add a test for [#1257](#1257)
([#1260](#1260))
</blockquote>

## `hugr-core`
<blockquote>

## 0.4.0 (2024-07-10)

### Bug Fixes

- Bring back input_extensions serialized field in rust NodeSer
([#1275](#1275))
- [**breaking**] `ops::Module` now empty struct rather than unit struct
([#1271](#1271))

### Features

- [**breaking**] `MakeOpDef` has new `extension` method.
([#1266](#1266))

### Refactor

- [**breaking**] Remove `Value::Tuple`
([#1255](#1255))
- [**breaking**] Rename `HugrView` function type methods + simplify
behaviour ([#1265](#1265))

### Styling

- Change "serialise" etc to "serialize" etc.
([#1251](#1251))

### Testing

- Add a test for [#1257](#1257)
([#1260](#1260))
</blockquote>

## `hugr-passes`
<blockquote>

## 0.4.0 (2024-07-10)

### Features

- Add `force_order` pass.
([#1285](#1285))

### Refactor

- [**breaking**] Remove `Value::Tuple`
([#1255](#1255))
</blockquote>

## `hugr-cli`
<blockquote>

## 0.1.3 (2024-07-10)

### Styling

- Change "serialise" etc to "serialize" etc.
([#1251](#1251))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/MarcoIeni/release-plz/).
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.

machinery in simple_op.rs doesn't check extension
2 participants