rust-lang / rust-lang/rust

compiler and language documentation disagree on lifetime extension for array expressions

Open
#146,092 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-array A-docs A-temporary-lifetime-extension C-bug I-lang-radar T-compiler T-lang T-lang-docs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

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

Start with the Reference and FLS lifetime-extension sections and reproduce the discrepancy using the linked Playground example. Then inspect compiler/rustc_hir_analysis/src/check/region.rs around the extending-expression implementation, comparing array and repetition expressions; done means the specification and compiler behavior agree.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.