Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Type inference of a diverging function is weird #20172

Copy link
Copy link
@nodakai

Description

@nodakai
Issue body actions

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
}

Metadata

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.