Skip to content

Type inference of a diverging function is weird #20172

Closed

Description

These two codes compile fine:

fn main() {
    panic!();
    panic!() //  warning: unreachable expression, #[warn(unreachable_code)] on by default
}
fn f<R>() -> R {
    panic!()
}

fn main() {
    println!("hello");
    f()
}

but this does not (note the semicolon after f()):

fn f<R>() -> R {
    panic!()
}

fn main() {
    println!("hello");
    f(); // error: unable to infer enough type information about `_`; type annotations required
}

Though the Rust language reference seems to say there's an (inhabitable) type called the "bottom type," the behavior of rustc is almost as if the bottom type does not really exist in Rust. These examples gave me an impression that a type equation involving a diverging function is simply discarded (otherwise R in the last example should correctly be inferred as the bottom type and there should be no difference between foo(); and panic();,) though I'm aware librustc/middle/infer/combine.rs does consistency check of FnDiverigng...

See also #20105 which was originally filed as a complaint for not allowing this code:

fn g<F, R>(_: F) where F: Fn<(), R> {
}

fn main() {
    g(|| panic!()); // error: unable to infer enough type information about `_`; type annotations required
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions