Skip to content

Need to repeat bounds of Associated Types #20705

Closed
@bluss

Description

@bluss

Even if the trait in the example declares a bound for its associated type, we have to repeat it to have the code compile.

Remove the where <T as HasNumber>::N: SignedInt and we get a compilation error:

 type `<T as HasNumber>::N` does not implement any method in scope named `abs`
use std::num::SignedInt;

trait HasNumber {
    type N: SignedInt;
    fn number(&self) -> Self::N;
}

fn user<T>(x: T) 
    where
        T: HasNumber,
        <T as HasNumber>::N: SignedInt,
{
    let n = x.number(); // ok
                        // without the repeated bound on T::N, we get this:
    let nabs = n.abs(); // type `<T as HasNumber>::N` does not implement any method in scope named `abs`
}

fn main() {
}

rustc version:

rustc 1.0.0-nightly (ea6f65c5f 2015-01-06 19:47:08 +0000)
binary: rustc
commit-hash: ea6f65c5f1a3f84e010d2cef02a0160804e9567a
commit-date: 2015-01-06 19:47:08 +0000
host: x86_64-unknown-linux-gnu
release: 1.0.0-nightly

cc @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions