Skip to content

compiler and language documentation disagree on lifetime extension for array expressions #146092

@dianne

Description

@dianne

I'm not sure whether this is a compiler or language docs issue, so I'll report it here and cc @rust-lang/lang-docs

The Reference and FLS specify that the operands of extending array expressions are extending. Clicking through to see the definition of array expressions, both specifications include [expr; N] repetition expressions. Thus from these specifications I would expect

let x = [&temp(); 1];

to extend temp() to live in the same scope as x. This is not the case (playground):

fn temp() {}

fn main() {
    let extended = [&temp()];
    extended; // this is okay

    let not_extended = [&temp(); 1];
    not_extended;
    //~^ ERROR: temporary value dropped while borrowed
}

On the compiler side, the implementation of extending expressions only includes normal array expressions, not repetition expressions, which are represented as hir::ExprKind::Repeat rather than hir:ExprKind::Array.

@rustbot label +T-compiler +T-lang +T-lang-docs +A-docs +A-temporary-lifetime-extension +A-array

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-arrayArea: `[T; N]`A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-temporary-lifetime-extensionArea: temporary lifetime extensionC-bugCategory: This is a bug.I-lang-nominatedNominated for discussion during a lang team meeting.P-lang-drag-2Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamT-lang-docsRelevant to the lang-docs team.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions