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

Update tracking bug for removal of YokeTraitHack #1134

Merged
merged 3 commits into from
Oct 3, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 88 additions & 41 deletions utils/yoke/src/trait_hack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! # Trait bounds in Yoke
//!
//! [Compiler bug #85636](https://github.com/rust-lang/rust/issues/85636) makes it tricky to add
//! [Compiler bug #89196](https://github.com/rust-lang/rust/issues/89196) makes it tricky to add
//! trait bounds involving `yoke` types.
//!
//! For example, you may want to write:
Expand All @@ -26,53 +26,100 @@
//!
//! # Examples
//!
//! Code that does not compile:
//! Code that does not compile ([playground](https://play.rust-lang.org/?version=beta&mode=debug&edition=2018&gist=ebbda5b15a398d648bdff9e439b27dc0)):
//!
//! ***[#1061](https://github.com/unicode-org/icu4x/issues/1061): The following example
//! compiles starting in Rust 1.56.
//! ```compile_fail
//! trait MiniYokeable<'a> {
Copy link
Member

Choose a reason for hiding this comment

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

Let's not check in MiniYoke stuff: can we get the version of this that uses actual Yoke?

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 was afraid you'd ask that... I copied my minified example into here because it serves the purpose of the compile_fail. But I'll try to un-minify it...

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I'd like this to show off the kind of code that will actually break in ICU4X :)

Copy link
Member Author

Choose a reason for hiding this comment

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

ok so note that this is in the yoke crate, not the icu_provider crate. So I updated the example to use the real Yoke/Yokeable but MiniDataProvider. Is that ok or do you want me to add the dev dependency, or move this to the other crate?

//! type Output;
//! }
//!
//! ```ignore
//! use yoke::Yoke;
//! use yoke::Yokeable;
//! struct MiniYoke<Y: for<'a> MiniYokeable<'a>> {
//! pub yokeable: Y,
//! }
//!
//! // Example trait and struct for illustration purposes:
//! trait MyTrait {}
//! struct MyStruct {}
//! impl MyTrait for MyStruct {}
//! unsafe impl<'a> Yokeable<'a> for MyStruct {
//! // (not shown; see `Yokeable` for examples)
//! # type Output = MyStruct;
//! # fn transform(&'a self) -> &'a Self::Output {
//! # self
//! # }
//! # fn transform_owned(self) -> Self::Output {
//! # self
//! # }
//! # unsafe fn make(from: Self::Output) -> Self {
//! # std::mem::transmute(from)
//! # }
//! # fn transform_mut<F>(&'a mut self, f: F)
//! # where
//! # F: 'static + for<'b> FnOnce(&'b mut Self::Output),
//! # {
//! # unsafe {
//! # f(std::mem::transmute::<&'a mut Self, &'a mut Self::Output>(
//! # self,
//! # ))
//! # }
//! # }
//! impl<Y> Clone for MiniYoke<Y>
//! where
//! Y: for<'a> MiniYokeable<'a>,
//! for<'a> <Y as MiniYokeable<'a>>::Output: Clone
//! {
//! fn clone(&self) -> Self {
//! unimplemented!()
//! }
//! }
//!
//! impl<Y, C> MyTrait for Yoke<Y, C>
//! trait MiniDataMarker {
//! type Yokeable: for<'a> MiniYokeable<'a>;
//! }
//!
//! struct MiniDataPayload<M>
//! where
//! Y: for<'a> Yokeable<'a>,
//! for<'a> <Y as Yokeable<'a>>::Output: MyTrait,
//! {}
//! M: MiniDataMarker
//! {
//! pub yoke: MiniYoke<M::Yokeable>,
//! }
//!
//! fn example() {
//! let y = Yoke::<MyStruct, ()>::new_always_owned(MyStruct {});
//! // error[E0277]: the trait bound `for<'a> <MyStruct as Yokeable<'a>>::Output: MyTrait` is not satisfied
//! let _: &dyn MyTrait = &y;
//! impl<M> Clone for MiniDataPayload<M>
//! where
//! M: MiniDataMarker,
//! for<'a> <M::Yokeable as MiniYokeable<'a>>::Output: Clone,
//! {
//! fn clone(&self) -> Self {
//! unimplemented!()
//! }
//! }
//!
//! trait MiniDataProvider<M>
//! where
//! M: MiniDataMarker
//! {
//! fn mini_load_payload(&self) -> MiniDataPayload<M>;
//! }
//!
//! struct MiniStructProvider<M>
//! where
//! M: MiniDataMarker,
//! {
//! pub payload: MiniDataPayload<M>,
//! }
//!
//! impl<M> MiniDataProvider<M> for MiniStructProvider<M>
//! where
//! M: MiniDataMarker,
//! for<'a> <M::Yokeable as MiniYokeable<'a>>::Output: Clone,
//! {
//! fn mini_load_payload(&self) -> MiniDataPayload<M> {
//! self.payload.clone()
//! }
//! }
//!
//! #[derive(Clone)]
//! struct SimpleStruct(pub u32);
//!
//! impl<'a> MiniYokeable<'a> for SimpleStruct {
//! type Output = SimpleStruct;
//! }
//!
//! impl MiniDataMarker for SimpleStruct {
//! type Yokeable = SimpleStruct;
//! }
//!
//! fn main() {
//! let provider = MiniStructProvider {
//! payload: MiniDataPayload {
//! yoke: MiniYoke {
//! yokeable: SimpleStruct(42)
//! }
//! }
//! };
//!
//! // Broken:
//! // "method cannot be called on `MiniStructProvider<_>` due to unsatisfied trait bounds"
//! let payload: MiniDataPayload<SimpleStruct> = provider.mini_load_payload();
//!
//! // Working:
//! let payload = MiniDataProvider::<SimpleStruct>::mini_load_payload(&provider);
//!
//! assert_eq!(payload.yoke.yokeable.0, 42);
//! }
//! ```
//!
Expand Down