[rust-dev] iface type parameter restrictions

Steven Blenkinsop steven099 at gmail.com
Mon Apr 16 18:23:13 PDT 2012


Motivated by the thread "paring back the self type to be, well, just a
type" <https://mail.mozilla.org/pipermail/rust-dev/2012-April/001661.html>
I decided to see how far I could get toward implementing monads given the
self type [constructor] as-is. I ran into several things that got in the
way, so I'm wondering how many of these are intentional. Here are some
reduced test cases and the compiler errors they gave. The last one is
obviously a compiler bug.

*************
iface I1<T: I2<self<T>>> {}
iface I2<T: I1<self<T>>> {}
fn main() {}

testcase1.rs:1:12: 1:21 error: illegal recursive type. insert a enum in the
cycle, if this is desired)
testcase1.rs:1 iface I1<T: I2<self<T>>> {}
                           ^~~~~~~~~
**************

The enum suggestion here isn't really useful. There's no inherent reason
that this can't work afaics, other than the difficulty you'd have
instantiating it.

*************
iface I1<T> {}
iface I2<T> {}

fn f<T: I1<U>, U: I2<T>>() {}
fn main() {}

testcase5.rs:4:11: 4:12 error: unresolved typename: U
testcase5.rs:4 fn f<T: I1<U>, U: I2<T>>() {}
                          ^
**************

Okay, I get it, type parameters must be introduced before you can use them
in your constraints. It would be nice to be able to instantiate those
recursive interfaces, though (if they worked).

*************
iface I1<T> {
fn F<I: I2<self<T>>>();
}
iface I2<T: I1<self<T>>> {}
fn main() {}

testcase2.rs:2:9: 2:18 error: unbound path I2<self<T>>
testcase2.rs:2 fn F<I: I2<self<T>>>();
                        ^~~~~~~~~
**************

See, here I was hoping to get around the restriction on recursive
interfaces by leaving the type parameterization until method invocation,
but clearly it doesn't work, though I can't entirely tell from the error
message why.

*************
iface I1<T> {
fn F<I: I2<self<T>>>();
}
iface I2<T: I1<self<T>>> {}

impl <T> of I1<T> for [T] {
fn F<I: I2<[T]>>() {}
}
fn main() {}

testcase3.rs:7:13: 7:14 error: unresolved typename: T
testcase3.rs:7 fn F<I: I2<[T]>>() {}
                            ^
error: aborting due to previous errors

*************

This is even weirder. It won't let me use impl type parameters in method
type parameter constraints. This is masking the previous error.

*************
iface I1<T> {
fn f<U: copy>(u: U) -> self<U>;
}

impl <T> of I1<T> for [T] {
 fn f<U: copy>(u: U) -> [U] { [u] }
}

fn func<T, I: I1<T>>(i: I) {
i.f(5);
}

fn main() {}

error: internal compiler error unexpected failure
note: The compiler hit an unexpected failure path. This is a bug. Try
running with RUST_LOG=rustc=0,::rt::backtrace to get further details and
report the results to github.com/mozilla/rust/issues

**************
Yeah, this is a bug. I'm not sure whether the code itself is right, though.
**
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20120416/be60f9ce/attachment.html>


More information about the Rust-dev mailing list