Skip to content

Type inference incorrectly selects specialized instance of type parameter #40718

Open
@sfackler

Description

@sfackler
#![feature(specialization)]
use std::vec;

struct Foo<T>(T);

impl<T> Foo<T> {
    fn foo<I: IntoIterator<Item = T>>(it: I) -> Foo<T> {
        <Self as SpecExtend<_, _>>::from_iter(it.into_iter())
    }
}

trait SpecExtend<T, I> {
    fn from_iter(iter: I) -> Self;
}

impl<T, I> SpecExtend<T, I> for Foo<T>
    where I: Iterator<Item = T>
{
    default fn from_iter(iter: I) -> Self {
        panic!()
    }

}

impl<T> SpecExtend<T, vec::IntoIter<T>> for Foo<T> {
    fn from_iter(iter: vec::IntoIter<T>) -> Self {
        panic!()
    }
}

fn main() {}
rustc 1.17.0-nightly (134c4a0f0 2017-03-20)
error[E0308]: mismatched types
 --> <anon>:8:47
  |
8 |         <Self as SpecExtend<_, _>>::from_iter(it.into_iter())
  |                                               ^^^^^^^^^^^^^^ expected struct `std::vec::IntoIter`, found associated type
  |
  = note: expected type `std::vec::IntoIter<T>`
             found type `<I as std::iter::IntoIterator>::IntoIter`

error: aborting due to previous error

@aturon

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-specializationArea: Trait impl specializationC-bugCategory: This is a bug.F-specialization`#![feature(specialization)]`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