Skip to content

Diverging expression not detected inside a while condition #141274

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
theemathas opened this issue May 20, 2025 · 2 comments
Open

Diverging expression not detected inside a while condition #141274

theemathas opened this issue May 20, 2025 · 2 comments
Labels
A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@theemathas
Copy link
Contributor

I tried this code:

#![allow(unreachable_code)]
fn main() {
    let _: bool = 'a: {
        while break 'a true {}
    };
}

I expected the code to compile. Instead, I got the following compile error:

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
4 |         while break 'a true {}
  |         ^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
  |
  = note: `while` loops evaluate to unit type `()`
help: consider returning a value here
  |
4 |         while break 'a true {} /* `bool` value */
  |                                ++++++++++++++++++

For more information about this error, try `rustc --explain E0308`.

Note that I can use uninitialized variables in the unreachable code, which indicates that the compiler realizes that the expression diverges. For example, this code compiles fine:

fn main() {
    let x: i32;
    let _: bool = 'a: {
        while break 'a true {}
        x += 1;
        true  // Deleting this `true` results in a type error.
    };
}

Usually, when a diverging expression appears in a block, rust no longer checks if the last expression in the block has the expected type. For example, this code compiles fine:

fn main() {
    let x: i32;
    let _: bool = 'a: {
        if break 'a true {}
        x += 1;
        // No `true` needed here
    };
}

Although, strangely enough, leaving the if as the last expression causes the code to again not compile:

fn main() {
    let _: bool = 'a: {
        if break 'a true {}
    };
}
error[E0308]: mismatched types
 --> src/main.rs:4:26
  |
4 |         if break 'a true {}
  |                          ^^ expected `bool`, found `()`

For more information about this error, try `rustc --explain E0308`.

All of this seems rather inconsistent.

This was discovered while experimenting with #118673

Meta

Reproducible on the playground with stable rust 1.87.0.

@theemathas theemathas added the C-bug Category: This is a bug. label May 20, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 20, 2025
@theemathas
Copy link
Contributor Author

To summarize

Type error:

fn main() {
    let _: bool = 'a: {
        while break 'a true {}
    };
}

Type error:

fn main() {
    let _: bool = 'a: {
        if break 'a true {}
    };
}

Type error:

fn main() {
    let _: bool = 'a: {
        while break 'a true {}
        let _ = 1;
    };
}

Compiles fine:

fn main() {
    let _: bool = 'a: {
        if break 'a true {}
        let _ = 1;
    };
}

@asquared31415
Copy link
Contributor

There are several limitations with the current reachability analysis, in particular "propagating" that information upwards through loops and blocks happens rarely. Additionally changes to loop reachability can be quite subtle, as more accurate analysis may cause more things to resolve to the never type, which has several problems with fallback.

@jieyouxu jieyouxu added the T-lang Relevant to the language team label May 20, 2025
@jieyouxu jieyouxu added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 5, 2025
@Noratrieb Noratrieb added A-inference Area: Type inference T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants