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

assert_is_covariant / assert_is_contravariant #35

Open
foobles opened this issue Jun 28, 2020 · 0 comments · May be fixed by #36
Open

assert_is_covariant / assert_is_contravariant #35

foobles opened this issue Jun 28, 2020 · 0 comments · May be fixed by #36

Comments

@foobles
Copy link

foobles commented Jun 28, 2020

Given that the variance of generic types is purely implicit, it would be greatly beneficial to have assertions to test the expected variance of a struct.

this is my proposed syntax:

assert_is_covariant!(for['a, T] (Foo<'a, 'b, T>) over 'b);

assert_is_contravariant!((fn(i32, T) -> bool) over T);

Which means "assert that for all lifetimes 'a and types T, Foo<'a, 'b, T> is covariant over 'b". Unfortunately, given the restrictions of declarative macros currently, it would be very challenging to make a for<...> syntax instead of for[...]. Additionally, if the for clause is not needed, it could just be omitted entirely. Note that the lifetime being tested cannot be included in the for clause.

This is how it would perform the test:

Assume that 'a is the lifetime being tested, Type is the fully qualified type (like &'a U), and GenericParams is everything in the for[...] clause.

  • Create a local type, e.g., Cov, parameterized with the lifetime being tested, followed by the remaining generic parameters, and wrapping the provided type, i.e., struct Cov<'a, GenericParams...>(Type); Since 'a is referred to within Type, by ensuring that 'a is the first parameter of Cov, it allows us to refer to refer to Type but with our own lifetimes replacing 'a.

  • Create a function, e.g., test_cov taking generic parameters <'__a: '__b, '__b, GenericParams...> and value parameters
    sub: *const Cov<'__a, GenericParams...> and
    mut sup: *const Cov<'__b, GenericParams...>.
    Then in the body, assert that the type of sub is indeed a subtype of sup by performing the assignment sup = sub;.

The implementation of assert_is_contravariant is almost identical, but would say sub = sup; instead, with corresponding changes to mutability.

The reason *const is used around the sup/sub parameters is to ensure that this test can be performed even if T: !Sized.

In the case of testing a type parameter T being tested, it would do the following:

  • Define a local typedef, e.g., type Transform<GenericParams..., T> = Type;
  • Recurse, invoking:
assert_is_covariant!(for[GenericParams...] (Transform<GenericParams..., &'__a ()>) over '__a);

This works because &'a () is always covariant over 'a. As a result, the subtyping
relation between any &'x () and &'y () always matches the relation between lifetimes 'x
and 'y. Therefore, testing variance over a type parameter T can be replaced by testing
variance over lifetime 'a in &'a ().
Even though this only checks cases where T is a reference, since a type constructor can be
ONLY covariant, contravariant, or invariant over a type parameter, if it is works in this case
it proves that the type is covariant in all cases.

The main downside to this approach is if there is some trait bound on T which &() does not implement. In this case, the user must simply provide a type known to satisfy the trait bounds that is covariant over some lifetime parameter, and then test variance over that lifetime.

@foobles foobles linked a pull request Jun 28, 2020 that will close this issue
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 a pull request may close this issue.

1 participant