Skip to content

closures in dead code are no longer borrowchecked #140583

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
ginnyTheCat opened this issue May 2, 2025 · 2 comments · Fixed by #140590
Closed

closures in dead code are no longer borrowchecked #140583

ginnyTheCat opened this issue May 2, 2025 · 2 comments · Fixed by #140590
Assignees
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. P-critical Critical priority regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ginnyTheCat
Copy link

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

@ginnyTheCat ginnyTheCat added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels May 2, 2025
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 2, 2025
@jieyouxu
Copy link
Member

jieyouxu commented May 2, 2025

cc @lcnr on the bisection in case this was not intentional

@jieyouxu jieyouxu added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label May 2, 2025
@lcnr lcnr changed the title todo!() suppresses borrow check error closures in dead code are no longer borrowchecked May 2, 2025
@lcnr
Copy link
Contributor

lcnr commented May 2, 2025

The following no longer errors

fn main() {
    todo!();
    || {
        let x = String::new();
        drop(x);
        drop(x)
    };
}

@lcnr lcnr added P-critical Critical priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels May 2, 2025
@lcnr lcnr self-assigned this May 2, 2025
@jieyouxu jieyouxu added A-borrow-checker Area: The borrow checker and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 2, 2025
bors added a commit to rust-lang-ci/rust that referenced this issue May 2, 2025
borrowck nested items in dead code

fixes rust-lang#140583

r? `@compiler-errors`
@jieyouxu jieyouxu added regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. and removed regression-untriaged Untriaged performance or correctness regression. labels May 2, 2025
@bors bors closed this as completed in e9f8103 May 7, 2025
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this issue May 8, 2025
borrowck nested items in dead code

fixes rust-lang/rust#140583

r? `@compiler-errors`
github-actions bot pushed a commit to rust-lang/miri that referenced this issue May 10, 2025
borrowck nested items in dead code

fixes rust-lang/rust#140583

r? `@compiler-errors`
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. P-critical Critical priority regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants