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

Allow custom types defined by static references #418

Closed
wants to merge 4 commits into from

Conversation

ss2165
Copy link
Member

@ss2165 ss2165 commented Aug 17, 2023

Most of the time can avoid string comparison using pointer equality short circuiting.

Might be a bit sketch.

@ss2165 ss2165 requested a review from aborgna-q August 17, 2023 10:50
generic over reference lifetime
src/utils.rs Outdated
@@ -39,3 +39,51 @@ pub(crate) mod test {
webbrowser::open(&base).unwrap();
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Leave the #[cfg(test)] block at the bottom.

SmolStr::new_inline("qubit"),
SmolStr::new_inline("prelude"),
TypeBound::Any,
);

pub(crate) const QB_T: Type = Type::new_extension(QB_CUSTOM_T);
pub(crate) const USIZE_T: Type = Type::new_extension(USIZE_CUSTOM_T);
pub(crate) const USIZE_CUSTOM_REF: &CustomType = &USIZE_CUSTOM_T;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need the explicit const references?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think because it is not temporary?

Checking quantinuum-hugr v0.1.0 (/Users/seyon/ng/hugr)
error[E0493]: destructor of `CustomType` cannot be evaluated at compile-time
  --> src/extension/prelude.rs:66:59
   |
66 | pub(crate) const QB_T: Type = Type::new_static_extension(&QB_CUSTOM_T);
   |                                                           ^^^^^^^^^^^- value is dropped here
   |                                                           |
   |                                                           the destructor for this type cannot be evaluated in constants

error[E0716]: temporary value dropped while borrowed
  --> src/extension/prelude.rs:66:59
   |
66 | pub(crate) const QB_T: Type = Type::new_static_extension(&QB_CUSTOM_T);
   |                               ----------------------------^^^^^^^^^^^-
   |                               |                           |          |
   |                               |                           |          temporary value is freed at the end of this statement
   |                               |                           creates a temporary value which is freed while still in use
   |                               argument requires that borrow lasts for `'static`

Some errors have detailed explanations: E0493, E0716.
For more information about an error, try `rustc --explain E0493`.
error: could not compile `quantinuum-hugr` (lib) due to 2 previous errors

Copy link
Collaborator

Choose a reason for hiding this comment

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

You'd need to use static instead of const so they are included in the compiled binary... But then you can't use it for the KnownTypeConst::TYPE const >.<

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I feel the static route is too painful - for now I've just made the reference constants private

src/utils.rs Outdated
fn eq(&self, other: &Self) -> bool {
match (self, other) {
// pointer equality can give false-negative
(Self::Static(l0), Self::Static(r0)) => std::ptr::eq(*l0, *r0) || l0 == r0,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is only valid for T: Eq, otherwise you don't have reflexivity.

@ss2165
Copy link
Member Author

ss2165 commented Aug 17, 2023

Given this is just a reinvention of std::borrow::Cow and maybe just worse anyway, closing as bikeshedding

@ss2165 ss2165 closed this Aug 17, 2023
@ss2165 ss2165 deleted the new/custom-refs branch August 17, 2023 13:37
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.

2 participants