Skip to content

Coroutine upvars are not required to be live in borrowck if witness has no drop requirements #144155

Closed

Description

@compiler-errors

I tried this code:

struct NeedsDrop<'a>(&'a Vec<i32>);

impl Drop for NeedsDrop<'_> {
    fn drop(&mut self) {
        println!("dropping {:#?}", self.0);
    }
}

async fn await_point() {}

fn main() {
    let v = vec![1, 2, 3];
    let x = NeedsDrop(&v);
    let c = async {
        std::future::ready(()).await;
        drop(x);
    };
    drop(v);
}

I expected it to fail, since dropping the coroutine c requires that NeedsDrop's 'a lifetime is live, since dropping the coroutine also performs a drop of NeedsDrop (as an upvar).

Instead, it compiled. This is UB.

This fails on rustc as of nightly 2025-07-18. Not sure if this has either always been incorrect, or if this was introduced due to the fix in #117134.

Activity

  1. added
    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
    C-bugCategory: This is a bug.
    on Jul 18, 2025
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jul 18, 2025
  3. compiler-errors commented on Jul 18, 2025

    @compiler-errors
    ContributorAuthor

    Bisection would be helpful if someone wants to do this; specifically, I want to know if this code went from fail->pass sometime in the past.

  4. added
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    on Jul 18, 2025
  5. lqd commented on Jul 18, 2025

    @lqd
    Member

    For me this has always failed since 1.36 until #117134 (or say, until 1.74 as I haven't bisected the 1.73 to 1.74 nightlies)

  6. matthiaskrgr commented on Jul 18, 2025

    @matthiaskrgr
    Member

    nightly-2023-11-02

    error[E0505]: cannot move out of `v` because it is borrowed
      --> /tmp/crash.rs:18:10
       |
    12 |     let v = vec![1, 2, 3];
       |         - binding `v` declared here
    13 |     let x = NeedsDrop(&v);
       |                       -- borrow of `v` occurs here
    ...
    18 |     drop(v);
       |          ^ move out of `v` occurs here
    19 | }
       | - borrow might be used here, when `c` is dropped and runs the destructor for coroutine
    
    error: aborting due to previous error; 1 warning emitted
    

    nightly-2023-11-03
    pass

    prs:

    commit[0] 2023-11-01: Auto merge of #117029 - rmehri01:mir_opt_filecheck_inline_tests, r=cjgillot
    commit[1] 2023-11-01: Auto merge of #117498 - matthiaskrgr:rollup-z7mg4ck, r=matthiaskrgr
    commit[2] 2023-11-02: Auto merge of #117506 - massivebird:ord-typo, r=ChrisDenton
    commit[3] 2023-11-02: Auto merge of #117386 - roblabla:fix-switch-stdio-win7, r=ChrisDenton
    commit[4] 2023-11-02: Auto merge of #117204 - nnethercote:rustc_ast_passes, r=compiler-errors
    commit[5] 2023-11-02: Auto merge of #117466 - compiler-errors:alias-bound, r=aliemjay
    commit[6] 2023-11-02: Auto merge of #117513 - matthiaskrgr:rollup-jvl6y84, r=matthiaskrgr
    commit[7] 2023-11-02: Auto merge of #117521 - GuillaumeGomez:impl-on-foreign-order, r=notriddle
    commit[8] 2023-11-02: Auto merge of #117471 - onur-ozkan:tmp-fix, r=clubby789
    commit[9] 2023-11-02: Auto merge of #117134 - lcnr:dropck_outlives-coroutine, r=compiler-errors

  7. compiler-errors commented on Jul 18, 2025

    @compiler-errors
    ContributorAuthor

    commit[9] 2023-11-02: Auto merge of #117134 - lcnr:dropck_outlives-coroutine, r=compiler-errors

    this one ^

  8. added
    S-has-bisectionStatus: A bisection has been found for this issue
    and removed
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    on Jul 18, 2025
  9. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jul 19, 2025
  10. added
    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
    and removed
    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jul 21, 2025
  11. added a commit that references this issue on Aug 11, 2025
    4e87b74
  12. added a commit that references this issue on Aug 11, 2025
    98fa8b7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-async-awaitArea: Async & AwaitA-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-criticalCritical priorityS-has-bisectionStatus: A bisection has been found for this issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions