mozilla/rust — Issue #1492: Should it be possible to use a boxed iface type for a parameter bounded on itself?
Author: marijnh
Created: 2012-01-11 14:44:39+00:00
State: open
Labels: rfc

I.e.

```
iface foo { fn foo(); }
fn do_foo(x: T) { x.foo(); }
fn do_foo_with_foo(x: foo) { do_foo(x); } // Does not compile
```

The reason that does not compile is that no dictionary can be passed for type `foo`, since that is a generalization of all types that conform to the `foo` interface, and thus doesn't have a single dict associated with it.

This seems a big enough composability hazard to merit a workaround. Unfortunately, the workaround is somewhat ugly. We could allow the compiler to pass NULL for the dict pointer in such a case, and have the code that looks up a function in a dict check, and if it finds a NULL, it could go into a codepath that fetches both the dict and the actual self value from the box. This causes a lot of branches and extra code to be generated. Another, also wasteful approach is to generate a second version of the function (`do_foo` in the example), which doesn't take a dict for the parameter bound, but simply expects the T type to be a boxed `foo`, and call that when passing a value of type `foo` to `do_foo`.