Skip to content

Type inference error: the type of this value must be known in this context #23012

Closed
@carllerche

Description

@carllerche

Repro:

pub trait Receive<T: Send, E: Send> : FnOnce(Result<T, E>) + Send {
    fn receive(self, res: Result<T, E>);
}

impl<T: Send, E: Send, F: FnOnce(Result<T, E>) + Send> Receive<T, E> for F {
    fn receive(self, res: Result<T, E>) {
        self(res)
    }
}

pub trait Async<T, E> {
    fn receive<F: Receive<T, E>>(self, f: F);
}

impl<T, E> Async<T, E> for Result<T, E> {
    fn receive<F: Receive<T, E>>(self, f: F) {
        f(self)
    }
}

fn res() -> Result<u32, ()> {
    Ok(123)
}

pub fn main() {
    res().receive(|res| println!("{}", res.unwrap()))
}

Output:

$ rustc fn.rs
fn.rs:26:40: 26:52 error: the type of this value must be known in this context
fn.rs:26     res().receive(|res| println!("{}", res.unwrap()))
                                                ^~~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:43: 2:76 note: expansion site
<std macros>:1:1: 2:78 note: in expansion of println!
fn.rs:26:25: 26:54 note: expansion site
error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-implied-boundsArea: Implied bounds / inferred outlives-boundsA-inferenceArea: Type inferenceC-bugCategory: This is a bug.S-types-deferredStatus: Identified as a valid potential future enhancement that is not currently being worked onT-typesRelevant to the types 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