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

Inference difference between pure associated types and generic types with associated type equalities #23065

Closed
huonw opened this issue Mar 5, 2015 · 1 comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-typesystem Area: The type system

Comments

@huonw
Copy link
Member

huonw commented Mar 5, 2015

Spawned off #23025 (comment).

use std::ops::Deref;
use std::slice;

trait Iter: Sized {
    type Item;

    fn next(&mut self) -> Option<Self::Item> { None }

    fn cloned2(self) -> Cloned<Self>
        where Self::Item: Deref,
              <Self::Item as Deref>::Target: Clone
    {
        Cloned {
            inner: self
        }
    }
}

trait AdditiveIter<A>: Iter {
    fn sum2(self) -> A;
}
impl<I: Iter<Item = i64>> AdditiveIter<i64> for I {
    fn sum2(self) -> i64 { 0 }
}
// (need more than one impl)
impl<I: Iter<Item = i32>> AdditiveIter<i32> for I {
    fn sum2(self) -> i32 { 0 }
}

impl<'a, T> Iter for slice::Iter<'a,T> {
    type Item = &'a T;
}

struct Cloned<I: Iter> {
    inner: I
}

#[cfg(not(assoc))]
impl<T, D, I> Iter for Cloned<I>
    where I: Iter<Item = D>,
          D: Deref<Target = T>,
          T: Clone
{
    type Item = T;
}
#[cfg(assoc)]
impl<I: Iter> Iter for Cloned<I>
    where I::Item: Deref,
          <I::Item as Deref>::Target: Clone
{
    type Item = <I::Item as Deref>::Target;
}

fn square_sum2(s: &[i64]) -> i64 {
    let sum = s.iter().cloned2().sum2();
    sum * sum
}

fn main() {}

Compiling with --cfg assoc works fine, while compiling without it gives:

cloned-infer.rs:56:5: 56:8 error: the type of this value must be known in this context
cloned-infer.rs:56     sum * sum
                       ^~~
error: aborting due to previous error
@huonw huonw added the A-typesystem Area: The type system label Mar 5, 2015
@nikomatsakis nikomatsakis added the A-associated-items Area: Associated items (types, constants & functions) label Mar 6, 2015
@steveklabnik
Copy link
Member

Compiling without works properly today. Closing!

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 (types, constants & functions) A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

3 participants