Skip to content

[expr; 0] ignores lifetimes #144045

@theemathas

Description

@theemathas

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 #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ZSTArea: Zero-sized types (ZSTs).A-arrayArea: `[T; N]`A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions