rust-lang / rust-lang/rust

`[expr; 0]` ignores lifetimes

Open
#144,045 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-array A-lifetimes A-ZST C-bug T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

use std::marker::PhantomData;

pub struct Thing<'a> {
    _phantom: PhantomData<fn(&'a ()) -> &'a ()>,
}

pub fn foo<'a, 'b>(x: Thing<'a>) -> [Thing<'b>; 0] {
    [x; 0]
}

pub fn lol<'a, 'b>(x: &'a i32) -> [&'b i32; 0] {
    [x; 0]
}

I expected the code not to compile, but instead it compiles without errors or warnings.

See also https://github.com/rust-lang/rust/issues/79580, which is about [expr; 0] being weird.

Meta

Reproducible on the playground with 1.90.0-nightly (2025-07-15 3014e79f9c8d5510ea7b)

@rustbot labels +A-zst +A-lifetimes

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the two examples in the Rust Playground using the nightly version reported in the issue, then read issue #79580 for related repeat-expression behavior. Trace how [expr; 0] handles the lifetimes in these cases; done means the examples' lifetime expectations are addressed and regression coverage is added.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.