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

rustc is unable to evaluate a singular conditional pathway predicated on the length of an array literal #119908

Open
barabadzhi opened this issue Jan 12, 2024 · 3 comments
Assignees
Labels
A-const-prop Area: Constant Propagation A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-unconditional_panic Lint: unconditional_panic T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@barabadzhi
Copy link

barabadzhi commented Jan 12, 2024

rustc is unable to evaluate a singular conditional pathway predicated on the length of an array literal.

This behavior prevents an obviously correct code to compile
(conditional should be eliminated altogether, instead of throwing a compile error in this case):

fn main() {
    let x: [i32; 0] = [];
    if !x.is_empty() {
        x[0];
    }
}

thanks @compiler-errors 👍🏼

Reproducible example playground.

Meta

rustc --version --verbose:

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: aarch64-apple-darwin
release: 1.75.0
LLVM version: 17.0.6

(stable, beta & nightly are all affected seemingly independent of the platform or edition, incl. 2024)

Error output

Compiling playground v0.0.1 (/playground)
error: this operation will panic at runtime
 --> src/main.rs:4:9
  |
4 |         x[0];
  |         ^^^^ index out of bounds: the length is 0 but the index is 0
  |
  = note: `#[deny(unconditional_panic)]` on by default

error: could not compile `playground` (bin "playground") due to previous error
@barabadzhi barabadzhi added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 12, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 12, 2024
@compiler-errors
Copy link
Member

compiler-errors commented Jan 12, 2024

Minimal:

fn main() {
    let x: [i32; 0] = [];
    if x.len() > 0 {
        x[0];
    }
}

@saethlin saethlin added A-diagnostics Area: Messages for errors, warnings, and lints A-const-prop Area: Constant Propagation and removed I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 13, 2024
@fmease fmease added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-diagnostics Area: Messages for errors, warnings, and lints labels Jan 13, 2024
@barabadzhi
Copy link
Author

Indeed!
Then the issue is not inherently linked to macro expansion. Instead, it pertains to the compiler's inability to evaluate a singular conditional pathway predicated on the length of an array literal.

@barabadzhi barabadzhi changed the title rustc expands a correct simple declarative macro into noncompilable code rustc is unable to evaluate a singular conditional pathway predicated on the length of an array literal Jan 13, 2024
@barabadzhi
Copy link
Author

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-prop Area: Constant Propagation A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-unconditional_panic Lint: unconditional_panic T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants