-
Notifications
You must be signed in to change notification settings - Fork 13.3k
autoderef: (&mut foo[bar]).baz()
works, but foo[bar].baz()
fails to type check due to wrong deref
#21630
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
Comments
This is not supposed to be possible. We screwed up somewhere in the definition of the |
Same with |
@nikomatsakis Should we change the definition of // `?Sized` bounds omitted for brevity
trait Index<Index> {
type Output;
fn index(&self, &Index) -> &Self::Output;
}
trait IndexMut<Index>: Index<Index> {
fn index_mut(&mut self, &Index) -> &mut <Self as Index<Index>>::Output;
} cc @aturon |
closes #21630 Overloaded indexing (`&[mut] foo[bar]`) only works when `<Self as Index>::Output` is the same as `<Self as IndexMut>::Output` (see issue above). To restrict implementations of `IndexMut` that doesn't work, this PR makes `IndexMut` a supertrait over `Index`, i.e. `trait IndexMut<I>: Index<I>`, just like in the `trait DerefMut: Deref` case. This breaks all downstream implementations of `IndexMut`, in most cases this simply means removing the `type Output = ..` bit, which is now redundant, from `IndexMut` implementations: ``` diff impl Index<Foo> for Bar { type Output = Baz; .. } impl IndexMut<Foo> for Bar { - type Output = Baz; .. } ``` [breaking-change] --- r? @nikomatsakis
STR
Version
When
<Bar as Index<Foo>>::Output == <Bar as IndexMut<Foo>>::Output == Vec<i8>
, thebar[Foo].push(0)
method call properly derefsbar[Foo]
toVec<i8>
. But, when[i8] = <Bar as Index<Foo>>::Output != <Bar as IndexMut<Foo>>::Output = Vec<i8>
, the method call wrongly derefsbar[Foo]
to[i8]
. In both cases explictly using(&mut bar[Foo]).push(0)
does work.cc @nikomatsakis @nick29581
The text was updated successfully, but these errors were encountered: