Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unreachability warning inconsistently provided with thread spawn #59521

Open
kinnison opened this issue Mar 29, 2019 · 2 comments
Open

unreachability warning inconsistently provided with thread spawn #59521

kinnison opened this issue Mar 29, 2019 · 2 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@kinnison
Copy link
Contributor

Hi,

charmander on #rust discussed an interesting issue where unreachability of code was inconsistently reported when threads return !. We reduced it to the following example:

#![feature(never_type)]

fn run() -> ! {
    loop {}
}
#[allow(dead_code)]
fn spawny<F>(closure: F) -> std::thread::JoinHandle<!>
where
    F: Send + 'static + FnOnce() -> !,
{
    std::thread::spawn(closure)
}

fn main() {
    // This one results in the println!() being marked unreachable
    let handle = spawny(move || run()); 
    // This one does not
    // let handle = std::thread::spawn(move || run());
    let _res = handle.join().unwrap();
    println!("hello, world");
}

If the thread is spawned via the spawny() function, then println!() is marked unreachable, otherwise if spawned via an in-line std::thread::spawn() call, it is not. In both cases _res has the type !.

@WiSaGaN
Copy link
Contributor

WiSaGaN commented Mar 29, 2019

Maybe redundant.

fn run() -> ! {
    std::thread::spawn(move || {
        loop {}
    }).join().unwrap() 
}

fn main() {
    run();
    println!("hello, world");
}

This warns correctly. Playground link

@jonas-schievink jonas-schievink added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Mar 29, 2019
@Absobel
Copy link

Absobel commented Aug 20, 2023

I tested the first snippet and for the second case _res doesn't have the type ! anymore but it is of type (), and handle is of type JoinHandle<()> instead of JoinHandle<!> for the first case, probably why it doesn't warn.

I don't why it does that though because according to the signature of spawn the function should return JoinHandle<T> where T is the return type of the closure

Also the second snippet warns correctly because run() returns never and the compiler directly flags the rest of the code unreachable when there is a never somewhere if I understood correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

4 participants