Skip to content

Improve compiler error on associated types #71035

Closed
@manuels

Description

@manuels

If you have a variable with type impl TryStream<Ok=T, Error=E> and want to use it as a impl Stream<Item=Result<T,E>>, the compiler complains.
The compiler does not take you by the hand and helps you with helpful hints.

I tried (roughly) this code: (EDIT: find a self-contained example here)

use bytes;
use std::io;
use futures::channel::mpsc;
use async_std::task;

//pub fn my_rx(sock: std::sync::Arc<UdpSocket>) -> impl futures::Stream<Item=Result<(bytes::Bytes, std::net::SocketAddr), std::io::Error>> { // WORKS
pub fn my_rx(sock: std::sync::Arc<UdpSocket>) -> impl futures::TryStream<Ok=(bytes::Bytes, std::net::SocketAddr), Error=std::io::Error> { // ERROR
    ...
}

fn main() {
    let inet_rx = my_rx();
    let (tx1, inet_rx1) = mpsc::unbounded();
    let (tx2, inet_rx2) = mpsc::unbounded();
    let mut tx = tx1.fanout(tx2).sink_map_err(|_| io::Error::new(io::ErrorKind::Other, "Send Error"));
    let foo = inet_rx.forward(tx);
    let _ = task::spawn(foo);
}

I expected to see this happen:
I would like the compiler to tell me that that that while there exists an implementation of TryStream<Ok=T, Error=E> as Stream<Result<T,E>>, there might exist other implementations and therefore this code is not accepted. Using Stream<Result<T,E>> instead of TryStream<Ok=T, Error=E> would solve the error.

Instead, this happened:
There are no hints by the compiler.

Meta

rustc --version --verbose:

rustc 1.41.0-nightly (6d77e45f0 2019-12-04)
binary: rustc
commit-hash: 6d77e45f01079fe3d40180b3e256e414ab379f63
commit-date: 2019-12-04
host: x86_64-unknown-linux-gnu
release: 1.41.0-nightly
LLVM version: 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.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