Stream: wg-traits

Topic: Repl support for associated types


Sydney Acksman (Jan 24 2020 at 02:06, on Zulip):

I have a trait impl that's unique but can't be solved by the current Rust compiler for reasons. I'm currently trying to write it as a program to load into the repl to see if chalk can solve it but I can't get it to understand associated types. What exactly is the syntax for associated types in the repl?

Sydney Acksman (Jan 24 2020 at 02:20, on Zulip):

It requires full specified type syntax aka <T as X>::Y doesn't it.

Jack Huey (Jan 24 2020 at 02:27, on Zulip):

To use an associated type, yes you need <T as X>::Y

Jack Huey (Jan 24 2020 at 02:27, on Zulip):

I'm actually surprised there isn't an example of associated types in the libstd.chalk

Sydney Acksman (Jan 24 2020 at 02:29, on Zulip):

Alright, new question: why doesn't this solve.

trait Sized { }

trait ValueType {
    type Inner;
}
trait Value<V> where V: ValueType<Inner = Self> { }
trait Packable where Self: ValueType { }
struct Packed<V> where V: Packable { }
impl<V> ValueType for Packed<V> where V: Packable {
    type Inner = <V as ValueType>::Inner;
}

struct i32 { }
struct Int32 { }
impl ValueType for Int32 {
    type Inner = i32;
}
impl Packable for Int32 { }
impl Value<Int32> for i32 { }

struct Vec<T> where T: Sized { }
trait RepeatedField<V> { }
impl<V> RepeatedField<V> for Vec<<V as ValueType>::Inner> where V: ValueType, <V as ValueType>::Inner: Value<V> { }
impl<V> RepeatedField<Packed<V>> for Vec<<Packed<V> as ValueType>::Inner> where V: Packable, <V as ValueType>::Inner: Value<V> { }

Chalk says there's an overlapping impl, but I can't see it. The impl over Packed should be different from V: ValueType, V::Inner: Value<V> since Packed<V>::Inner doesn't implement Value<Packed<V>>. So it should be unique, right?

Sydney Acksman (Jan 24 2020 at 02:30, on Zulip):

Damn, after explaining it now I see it.

Sydney Acksman (Jan 24 2020 at 03:34, on Zulip):

I have figured out a fix though that falls inline with my original logic for why this was valid earlier

struct Packed<V> where V: Packable, <V as ValueType>::Inner: Value<V> { }
impl<V> ValueType for Packed<V> where V: Packable, <V as ValueType>::Inner: Value<V> {
    type Inner = <V as ValueType>::Inner;
}
Last update: Feb 25 2020 at 04:15UTC