Skip to content

Instantly share code, notes, and snippets.

@nikomatsakis nikomatsakis/trait-alias tests Secret

Last active Oct 23, 2018
Embed
What would you like to do?
Things to test:
- `impl TraitAlias for u32 { .. }` -- should error I think?
- Kinds of trait aliases:
- `trait SendSyncAlias = Send + Sync`
- `trait WhereSendAlias = where Self: Send`
- `trait SendEqAlias<T> = Send where T: PartialEq<Self>`
- with associated items specified (`trait U32Iterator = Iterator<Item = u32>`)
- with a lifetime parameter (`trait PartialEqRef<'a, T> = PartialEq<&'a T>` or something like that)
- Test (ideally) each kind of where clause in each possible position:
- Type parameter bounds (`struct Foo<T: SendSyncAlias>`, `impl<T: SendSyncAlias> Foo for T`)
- Where clauses (`struct Foo<T> where T: SendSyncAlias`)
- Impl trait (`fn foo(x: &impl SendEqAlias<u32>) { 22_u32 == *x }`)
- On functions, in particular, test each kind of where-clause, e.g., but especially "special stuff":
- `fn foo<T: U32Iterator>(x: &T) -> Option<u32> { x.next() }`
- `fn foo<T: SendEqAlias<u32>>(x: &T) -> bool { 22_u32 == *x }`
- `fn foo<T: SendSyncAlias>() { is_send_and_sync::<T>(); } fn is_send_and_sync<T: Send + Sync>() { }`
- Trait object:
- `trait Alias = Eq`, `&dyn Alias` -- should fail object safety check
- `&dyn Alias` -- check that it works, document behavior
- `impl dyn Alias` -- check that it works, document behavior
- Test that it implements the relevant traits:
- `trait Alias = Debug; fn foo(x: &dyn Debug) { bar(x); } fn bar<T: ?Sized + Debug>(x: &T) { }`
- with associated items specified:
- `trait U32Iterator = Iterator<Item = u32>; &dyn U32Iterator`
- without associated items specified:
- `trait Alias = Iterator; &dyn Alias` -- error
- Well-formedness conditions on alias definitions:
- `trait Foo<T: Send> { } trait Bar<T> = Foo<T>;` // error -- requires `T: Send`
- `trait Foo<'a, T: 'a> { } trait Bar<'a, T> = Foo<T: 'a>;` // error -- requires `T: 'a`
- Extra conditions on trait aliases:
- given `trait Foo<T> { } trait Bar<T: Display> = Foo<T>;` then:
- `fn is_foo<T: Bar<U>, U>() { /* ok */ }`
- `fn is_bar<T: Bar<U>, U>() { /* NOT ok */ }`
- `fn is_bar2<T: Bar<U>, U: Display>() { /* ok */ }`
- Associated type constraints:
- given `trait MyIterator = Iterator`:
- `fn foo<T: MyIterator<Item = u32>>() { /* ok */ }`
- `fn foo<T: MyIterator<Something = u32>>() { /* not ok */ }`
- given `trait Foo { type Assoc; } trait Bar { type Assoc; } trait FooBar = Foo + Bar`:
- `fn foo<T: FooBar<Assoc = u32>> { /* not ok */ }`
- (or take example from [ambiguous constraints](https://rust-lang.github.io/rfcs/1733-trait-alias.html#ambiguous-constraints) section)
- Other situations:
- `trait Static = 'static` as describes in ["only lifetimes"](https://rust-lang.github.io/rfcs/1733-trait-alias.html#trait-alias-containing-only-lifetimes)
-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.