Skip to content

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

Closed
@fabianschuiki

Description

@fabianschuiki

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-inferenceArea: Type inferenceA-trait-systemArea: Trait systemC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions