Skip to content

Type inference fails to deduce blanket-implemented trait of super traits with associated types #9881

@Veetaha

Description

@Veetaha

In the following code (minimized from futures::{Stream, TryStream}) try_poll_next() method is displayed as unresolved reference, however it is compilable

pub trait TryStream: Stream {
    fn try_poll_next(&self) {}
}

pub trait Stream {
    // Removing associated type makes the error disappear
    type Item;
    fn poll_next(&self) {}
}

// Trait that only has `Stream` as super-trait
trait StreamAlias: Stream<Item = ()> {}

// Blanket impls
impl<S: Stream<Item = ()>> TryStream for S {}

impl<S: Stream<Item = ()>> StreamAlias for S {}

// Concrete implementor of `Stream`
struct StreamImpl;

impl Stream for StreamImpl {
    type Item = ();
}

fn foo() -> impl StreamAlias {
    StreamImpl
}

fn main() {
    let alias = foo();
	// try_poll_next() here is unresolved, though the code compiles
    alias.try_poll_next();

    // However direct `Stream` supertrait works, this method is resolved
	alias.poll_next();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tytype system / type inference / traits / method resolutionC-bugCategory: bugS-needs-testBug fixed, but needs a test.good first issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions