Skip to content

impl Trait with associated type equality constraint fails to resolve when constraint contains associated type of non-parameter type #53984

Closed
@ytausky

Description

@ytausky

The following code fails to compile:

use std::iter::once;

trait TokenSpec {
    type Ident;
}

impl TokenSpec for String {
    type Ident = String;
}

struct Expr<I> {
    ident: I,
}

type Input = Expr<<String as TokenSpec>::Ident>;

fn foo() -> impl Iterator<Item = Input> {
    once(Expr { ident: "my_string".to_string() })
}

fn main() {
    let v: Vec<_> = foo().collect();
    println!("{:?}", v);
}

I get this error message:

error[E0271]: type mismatch resolving `<std::iter::Once<Expr<std::string::String>> as std::iter::Iterator>::Item == Expr<<std::string::String as TokenSpec>::Ident>`
  --> src/main.rs:17:13
   |
17 | fn foo() -> impl Iterator<Item = Input> {
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found associated type
   |
   = note: expected type `Expr<std::string::String>`
              found type `Expr<<std::string::String as TokenSpec>::Ident>`
   = note: the return type of a function must have a statically known size

I think this case should work, since <String as TokenSpec>::Ident can be resolved at type checking time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.WG-traitsWorking group: Traits, https://internals.rust-lang.org/t/announcing-traits-working-group/6804

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions