-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
generic over reference lifetime
src/utils.rs
Outdated
@@ -39,3 +39,51 @@ pub(crate) mod test { | |||
webbrowser::open(&base).unwrap(); | |||
} | |||
} |
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.
Leave the #[cfg(test)]
block at the bottom.
src/extension/prelude.rs
Outdated
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; |
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.
Why do we need the explicit const
references?
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 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
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.
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 >.<
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.
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, |
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 is only valid for T: Eq
, otherwise you don't have reflexivity.
Most of the time can avoid string comparison using pointer equality short circuiting.
Might be a bit sketch.