Skip to content

closures in dead code are no longer borrowchecked #140583

Closed
@ginnyTheCat

Description

@ginnyTheCat

I minimized this code from a compile_fail doctest that started compiling in nightly even though it didn't in stable. I appended a minified (probably not minimal) example below.

Replacing todo!() with 5 for example causes it to fail to compile on nightly as well. Therefore I'm not quite sure whether this really counts as a regresssion, it does not seem to be planned / mentioned anywhere though.

cargo bisect-rustc points to d4f880f, merge of #138499 (nightly 2025-04-09).

Code

I tried this code:

struct X;

fn run(a: u32, b: &mut Vec<&X>, c: impl Fn(&mut Vec<&X>, &[X])) {
    c(b, &[X, X, X]);
}

fn main() {
    let mut res = Vec::new();
    run(todo!(), &mut res, |r, xes| r.extend(xes));
}

I expected to see this happen:

error: lifetime may not live long enough
 --> src/main.rs:9:37
  |
9 |     run(todo!(), &mut res, |r, xes| r.extend(xes));
  |                             -  ---  ^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`
  |                             |  |
  |                             |  has type `&'1 [X]`
  |                             has type `&mut Vec<&'2 X>`

Instead, this happened: compiled successfully

Version it worked on

It most recently worked on: Rust 1.86, Rust 1.87 Beta.7, Rust Nightly 2025-04-08

rustc 1.88.0-nightly (e643f59f6 2025-04-07)
binary: rustc
commit-hash: e643f59f6da3a84f43e75dea99afaa5b041ea6bf
commit-date: 2025-04-07
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Version with regression

rustc --version --verbose:

rustc 1.88.0-nightly (d4f880f8c 2025-04-08)
binary: rustc
commit-hash: d4f880f8ce832cd7560bb2f1ebc34f967055ffd7
commit-date: 2025-04-08
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Metadata

Metadata

Assignees

Labels

A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.P-criticalCritical priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions