Skip to content

Possibly invalid "unconstrained types" calling trait method with constraints #12028

Closed
@erickt

Description

@erickt

Here is some example code:

trait Hash<H> {
    fn hash2(&self, hasher: &H) -> u64;
}

trait Stream {
    fn input(&mut self, bytes: &[u8]);
    fn result(&self) -> u64;
}

trait StreamHasher<S: Stream> {
    fn stream(&self) -> S;
}

//////////////////////////////////////////////////////////////////////////////

trait StreamHash<S: Stream, H: StreamHasher<S>>: Hash<H> {
    fn input_stream(&self, stream: &mut S);
}

impl<S: Stream, H: StreamHasher<S>> Hash<H> for u8 {
    fn hash2(&self, hasher: &H) -> u64 {
        let mut stream = hasher.stream();
        self.input_stream(&mut stream);
        stream.result()
    }
}

impl<S: Stream, H: StreamHasher<S>> StreamHash<S, H> for u8 {
    fn input_stream(&self, stream: &mut S) {
        stream.input([*self]);
    }
}

fn main() {}

This errors with:

hash9.rs:23:9: 23:40 error: cannot determine a type for this bounded type parameter: unconstrained type
hash9.rs:23         self.input_stream(&mut stream);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

One possibility is that the unconstrained type checker isn't taking in consideration of the impl<S: Stream, H: StreamHasher<S>> Hash<H> for u8 constraint and is only being evaluated in context of the unconstrained Hash<H> trait.

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions