Skip to content

Unreachable statement is not checked for const assertions #111202

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

Open
TheNeikos opened this issue May 4, 2023 · 6 comments
Open

Unreachable statement is not checked for const assertions #111202

TheNeikos opened this issue May 4, 2023 · 6 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-monomorphization Area: Monomorphization C-bug Category: This is a bug.

Comments

@TheNeikos
Copy link
Contributor

I tried this code:

use core::marker::PhantomData;

struct Check<T: Accept>(PhantomData<T>);
impl<T: Accept> Check<T> {
    const CHECK: () = assert!(T::STREAM);
}

trait Accept {
    type Foo;
    const STREAM: bool = false;
}

impl Accept for u64 {
    type Foo = u64;
    const STREAM: bool = false;
}

struct Foo;

impl Foo {
    fn only_stream<T: Accept>(self, t: T)
    {
        let _ = Check::<T>::CHECK;
        println!("Is stream!");
    }
}

fn get_foo() -> Foo {
    todo!()
}

fn main() {
    let foo: Foo = todo!(); // Comment these out to make it fail to compile
    foo.only_stream(5u64);  // this line as well
    
    let foo = get_foo();
    foo.only_stream(5u64);
}

I expected to see this happen: I expected the second line in main to cause a compilation failure. But due to the todo this does not happen.

Instead, this happened: It compiles, but warns about unused code.

Meta

rustc --version --verbose:

Stable 1.69

this also happens on nightly 1.71.0-nightly (2023-05-03 473f916d836cc662c5bd)

Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c3caf7f91c27f00067173e0e83406a24

Found together with @matthiasbeyer

@TheNeikos TheNeikos added the C-bug Category: This is a bug. label May 4, 2023
@TheNeikos
Copy link
Contributor Author

While this is not high impact problem for working code, since it never progresses past the todo!, this does come up when testing. Which is also how we found out about this. Notably for compile_fail tests.

The fact that the location of the todo! is important, seems to me that this may not be expected.

@matthiasbeyer
Copy link

matthiasbeyer commented May 4, 2023

this does come up when testing. Which is also how we found out about this. Notably for compile_fail tests.

doctests, that is.

@oli-obk
Copy link
Contributor

oli-obk commented May 5, 2023

It is somewhat intended. Avoiding looking into dead code is done on purpose, so that if cfg(windows) { f::<i32>() } else { g() } doesn't instantiate the generic function f for i32 on windows, because that may end up being expensive or even wrong (failing to compile on non-windows, but compiling on windows).

That said, this all is done rather opportunistically and there is some desire to change something in this space that may then also affect what we do in dead code.

@TheNeikos
Copy link
Contributor Author

I understand. It is a rather unfortunate cross section of not wanting to instantiate a complete system in a doctest to get a valid T while also having compile_fail on it.

Putting the todo into a seperate function works at least here. Is there maybe a way to mark something as 'this will fail at runtime' but will not otherwise impede the computed data flow? (And thus not trigger the dead code analysis?

@oli-obk
Copy link
Contributor

oli-obk commented May 5, 2023

Is there maybe a way to mark something as 'this will fail at runtime' but will not otherwise impede the computed data flow? (And thus not trigger the dead code analysis?

Thinking about this I feel like todo!() and unimplemented!() should serve that purpose (in contrast to panic! or unreachable!()) as these signal that any dead code around them is only dead code for now. I don't have a good idea for how to teach rustc about that though.

@moxian

This comment has been minimized.

@rustbot rustbot added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label May 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-monomorphization Area: Monomorphization C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

6 participants