Skip to content
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

"mismatched types" on indexing into trait with two Index subtraits via output type #70944

Closed
fabianschuiki opened this issue Apr 9, 2020 · 5 comments · Fixed by #77956
Closed
Labels
A-associated-items Area: Associated items such as associated types and consts. A-inference Area: Type inference A-traits Area: Trait system C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fabianschuiki
Copy link

fabianschuiki commented Apr 9, 2020

If a trait has two or more Index subtraits, and one obtains a reference to something that implements that trait under specific conditions, typeck reports an error for indexing with all but the last Index's key. Seems to happen only if that reference was obtained through another trait's output type.

Consider the following example (playground):

use std::ops::Index;

struct KeyA;
struct KeyB;
struct KeyC;

trait Foo: Index<KeyA> + Index<KeyB> + Index<KeyC> {}
trait FooBuilder {
    type Inner: Foo;
    fn inner(&self) -> &Self::Inner;
}

fn do_stuff(foo: &impl FooBuilder) {
    let inner = foo.inner();
    &inner[KeyA];
    //     ^^^^ expected struct `KeyC`, found struct `KeyA`
    &inner[KeyB];
    //     ^^^^ expected struct `KeyC`, found struct `KeyB`
    &inner[KeyC];
}

This breaks with the message:

error[E0308]: mismatched types
  --> src/lib.rs:15:12
   |
15 |     &inner[KeyA];
   |            ^^^^ expected struct `KeyC`, found struct `KeyA`

error[E0308]: mismatched types
  --> src/lib.rs:16:12
   |
16 |     &inner[KeyB];
   |            ^^^^ expected struct `KeyC`, found struct `KeyB`

The issue does not occur if the indexing is performed on a Foo directly. That is, the following works just fine (playground):

fn do_stuff(foo: &impl Foo) {
    let inner = foo;
    &inner[KeyA];
    &inner[KeyB];
    &inner[KeyC];
}

What I would expect to happen is that all three index operations work. More specifically, I would expect each of the operations to produce the corresponding Index::Output type correctly.

Meta

rustc --version --verbose:

rustc 1.42.0 (b8cedc004 2020-03-09)
binary: rustc
commit-hash: b8cedc00407a4c56a3bda1ed605c6fc166655447
commit-date: 2020-03-09
host: x86_64-unknown-linux-gnu
release: 1.42.0
LLVM version: 9.0
@fabianschuiki
Copy link
Author

fabianschuiki commented Apr 9, 2020

The problem also occurs if .index() is called explicitly (playground):

fn do_stuff(foo: &impl FooBuilder) {
    let inner = foo.inner();
    inner.index(KeyA);
    //          ^^^^ expected struct `KeyC`, found struct `KeyA`
    inner.index(KeyB);
    //          ^^^^ expected struct `KeyC`, found struct `KeyB`
    inner.index(KeyC);
}

Or also when Index::index() is called explicitly (playground):

fn do_stuff(foo: &impl FooBuilder) {
    let inner = foo.inner();
    Index::index(inner, KeyA);
    //                  ^^^^ expected struct `KeyC`, found struct `KeyA`
    Index::index(inner, KeyB);
    //                  ^^^^ expected struct `KeyC`, found struct `KeyB`
    Index::index(inner, KeyC);
}

However, the problem does not occur if the key type is specified together with the Index trait (playground):

fn do_stuff(foo: &impl FooBuilder) {
    let inner = foo.inner();
    Index::<KeyA>::index(inner, KeyA);
    Index::<KeyB>::index(inner, KeyB);
    Index::<KeyC>::index(inner, KeyC);
    // works just fine
}

@steffahn
Copy link
Member

steffahn commented Apr 9, 2020

@rustbot modify labels to A-traits, A-associated-items, A-inference, C-bug, and T-compiler.

@rustbot rustbot added A-associated-items Area: Associated items such as associated types and consts. A-inference Area: Type inference A-traits Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 9, 2020
@WaffleLapkin
Copy link
Member

Feels like a duplicate of #72582

@WaffleLapkin
Copy link
Member

This is fixed on nightly. Presumably by #73905

@steffahn
Copy link
Member

Bisection points to 08e2d46, so it is #73905 indeed. I guess we still need @rustbot modify labels to E-needs-test.

@rustbot rustbot added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Oct 10, 2020
@bors bors closed this as completed in cbc42a0 Oct 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-inference Area: Type inference A-traits Area: Trait system C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants