Skip to content

Unreachable statement is not checked for const assertions #111202

Open
@TheNeikos

Description

@TheNeikos

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-monomorphizationArea: MonomorphizationC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions