Created
October 4, 2018 20:02
-
-
Save scalexm/cf32cea831a5d8193fa8a484099b71a2 to your computer and use it in GitHub Desktop.
Why functions shouldn't assume that their return type is WF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct OnlySized<T: Sized>(std::marker::PhantomData<T>); | |
impl<T: Sized> OnlySized<T> { | |
fn new() -> Self { | |
println!("{}", std::mem::size_of::<T>()); | |
OnlySized(std::marker::PhantomData) | |
} | |
} | |
trait Bar { } | |
fn foo() -> OnlySized<dyn Bar> { | |
OnlySized<dyn Bar>::new() | |
} | |
fn bar<T>(f: fn() -> T) { | |
let _ = f(); | |
} | |
fn main() { | |
bar(foo); | |
} | |
// Comparison: | |
// defining a function `fn foo() -> OnlySized<dyn Bar>` is like defining a new type and then | |
// ``` | |
// impl Fn<()> for foo { | |
// // here we should be proving that the assoc type value is WF, which is not the case | |
// type Output = OnlySized<dyn Bar>; | |
// } | |
// ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment