Skip to content

rustc fails to resolve trait implementation #84548

@BusyJay

Description

@BusyJay

I tried this code:

trait A<T> {
    fn a() -> T;
}

pub struct GlobalA;

impl A<usize> for GlobalA {
    fn a() -> usize {
        0
    }
}

pub struct GlobalB;

impl<T> A<Vec<T>> for GlobalB
where GlobalA: A<T>
{
    fn a() -> Vec<T> {
        let s: usize = GlobalA::a();
        let mut v = Vec::with_capacity(s);
        for _ in 0..s {
            v.push(GlobalA::a());
        }
        v
    }
}

I expected to see this happen:
Compile successfully.

Instead, this happened:

error[E0308]: mismatched types
  --> src/lib.rs:19:24
   |
15 | impl<T> A<Vec<T>> for GlobalB
   |      - this type parameter
...
19 |         let s: usize = GlobalA::a();
   |                -----   ^^^^^^^^^^^^ expected `usize`, found type parameter `T`
   |                |
   |                expected due to this
   |
   = note:        expected type `usize`
           found type parameter `T`

error: aborting due to previous error

Wrote <GlobalA as A<usize>>::a() as a workaround.

Meta

rustc --version --verbose:

rustc 1.51.0 (2fd73fabe 2021-03-23)
binary: rustc
commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
commit-date: 2021-03-23
host: aarch64-apple-darwin
release: 1.51.0
LLVM version: 11.0.1
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.T-typesRelevant to the types 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