Skip to content

pin! in an if let guard of an or-pattern arm never drops the value pinned for the second alternative, which breaks Pin's drop guarantee #165322

Description

@leeewee

I tried this code:

#![forbid(unsafe_code)]
struct D(u8);
impl Drop for D {
    fn drop(&mut self) {
        println!("drop {}", self.0);
    }
}

fn f(x: Result<u8, u8>) {
    match x {
        Ok(v) | Err(v) if let p = std::pin::pin!(D(v)) && p.0 > 0 => println!("arm {v}"),
        _ => {}
    }
}

fn main() {
    f(Ok(1));
    f(Err(2)); // the `D(2)` pinned in the guard is never dropped
}

Expected: arm 1, drop 1, arm 2, drop 2.

Actual: arm 1, drop 1, arm 2. D(2) is never dropped, in debug and release builds.

LLM disclosure: found by an LLM-driven analysis.

Meta

rustc 1.99.0 (b940084d7 2026-09-28)
rustc 1.100.0-beta.5 (8ea38001b 2026-10-13)
rustc 1.101.0-nightly (92ffebece 2026-10-28)

Activity

  1. added
    C-bug
    on Nov 2, 2026
  2. added
    needs-triage
    on Nov 2, 2026
  3. added
    A-destructors
    T-compiler
    I-unsound
    A-patterns
    A-pin
    and removed
    needs-triage
    on Nov 2, 2026
  4. added
    I-prioritize
    on Nov 2, 2026
  5. theemathas commented on Nov 2, 2026

    @theemathas
    Contributor
  6. theemathas commented on Nov 2, 2026

    @theemathas
    Contributor

    Another reproducer:

    #![feature(super_let)]
    
    struct LoudDrop;
    impl Drop for LoudDrop {
        fn drop(&mut self) {
            println!("dropping LoudDrop");
        }
    }
    
    fn main() {
        match true {
            false | true
                if let _ = {
                    super let a = LoudDrop;
                    println!("created LoudDrop");
                } => {}
            _ => {}
        }
    }

    Output

    created LoudDrop
    
  7. added
    I-memleak
    F-super_let
    on Nov 2, 2026
  8. self-assigned this
    on Nov 2, 2026
  9. added
    S-found-by-fuzzing
    on Nov 3, 2026
  10. Hoverbear commented on Nov 18, 2026

    @Hoverbear
    Contributor
    Bisect output
    ❯ cargo bisect-rustc --start=2025-08-06 --end=2025-08-09 --prompt -- run
    checking the start range to find a passing nightly
    installing nightly-2025-08-06
    rust-std-nightly-x86_64-unknown-linux-gnu: 29.76 MB / 29.76 MB [====================] 100.00 % 36.57 MB/s testing...
       Compiling dependency_1 v0.1.0 (/home/ana/git/ferrocene/reproductions/dependency-1)
       Compiling dependency_2 v0.1.0 (/home/ana/git/ferrocene/reproductions/dependency-2)
       Compiling reproductions v0.1.0 (/home/ana/git/ferrocene/reproductions)
        Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s
         Running `target-bisector-nightly-2025-08-06-x86_64-unknown-linux-gnu/debug/reproductions`
    arm 1
    arm 2
    drop 2
    
    
    nightly-2025-08-06 finished with exit code Some(0).
    please select an action to take:
    RESULT: nightly-2025-08-06, ===> Successfully compiled
    uninstalling nightly-2025-08-06
    
    checking the end range to verify it does not pass
    installing nightly-2025-08-09
    rust-std-nightly-x86_64-unknown-linux-gnu: 29.89 MB / 29.89 MB [====================] 100.00 % 36.22 MB/s testing...
       Compiling dependency_1 v0.1.0 (/home/ana/git/ferrocene/reproductions/dependency-1)
       Compiling dependency_2 v0.1.0 (/home/ana/git/ferrocene/reproductions/dependency-2)
       Compiling reproductions v0.1.0 (/home/ana/git/ferrocene/reproductions)
        Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s
         Running `target-bisector-nightly-2025-08-09-x86_64-unknown-linux-gnu/debug/reproductions`
    arm 1
    drop 1
    arm 2
    
    
    nightly-2025-08-09 finished with exit code Some(0).
    please select an action to take:
    RESULT: nightly-2025-08-09, ===> Compile error
    uninstalling nightly-2025-08-09
    
    2 versions remaining to test after this (roughly 2 steps)
    installing nightly-2025-08-07
    rust-std-nightly-x86_64-unknown-linux-gnu: 29.76 MB / 29.76 MB [====================] 100.00 % 37.01 MB/s testing...
       Compiling dependency_1 v0.1.0 (/home/ana/git/ferrocene/reproductions/dependency-1)
       Compiling dependency_2 v0.1.0 (/home/ana/git/ferrocene/reproductions/dependency-2)
       Compiling reproductions v0.1.0 (/home/ana/git/ferrocene/reproductions)
        Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s
         Running `target-bisector-nightly-2025-08-07-x86_64-unknown-linux-gnu/debug/reproductions`
    arm 1
    arm 2
    drop 2
    
    
    nightly-2025-08-07 finished with exit code Some(0).
    please select an action to take:
    RESULT: nightly-2025-08-07, ===> Successfully compiled
    uninstalling nightly-2025-08-07
    
    1 versions remaining to test after this (roughly 1 steps)
    installing nightly-2025-08-08
    rust-std-nightly-x86_64-unknown-linux-gnu: 29.83 MB / 29.83 MB [====================] 100.00 % 35.96 MB/s testing...
       Compiling dependency_1 v0.1.0 (/home/ana/git/ferrocene/reproductions/dependency-1)
       Compiling dependency_2 v0.1.0 (/home/ana/git/ferrocene/reproductions/dependency-2)
       Compiling reproductions v0.1.0 (/home/ana/git/ferrocene/reproductions)
        Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s
         Running `target-bisector-nightly-2025-08-08-x86_64-unknown-linux-gnu/debug/reproductions`
    arm 1
    drop 1
    arm 2
    
    
    nightly-2025-08-08 finished with exit code Some(0).
    please select an action to take:
    RESULT: nightly-2025-08-08, ===> Compile error
    uninstalling nightly-2025-08-08
    
    searched toolchains nightly-2025-08-06 through nightly-2025-08-09
    
    
    ********************************************************************************
    Regression in nightly-2025-08-08
    ********************************************************************************
    
    fetching https://static.rust-lang.org/dist/2025-08-07/channel-rust-nightly-git-commit-hash.txt
    nightly manifest 2025-08-07: 40 B / 40 B [=========================================] 100.00 % 716.98 KB/s converted 2025-08-07 to 7d82b83ed57d188ab3f2441a765a6419685a88a3
    fetching https://static.rust-lang.org/dist/2025-08-08/channel-rust-nightly-git-commit-hash.txt
    nightly manifest 2025-08-08: 40 B / 40 B [===========================================] 100.00 % 1.15 MB/s converted 2025-08-08 to 2fd855fbfc8239285aa2d596f76a8cc75e17ce02
    looking for regression commit between 2025-08-07 and 2025-08-08
    fetching (via remote github) commits from max(7d82b83ed57d188ab3f2441a765a6419685a88a3, 2025-08-05) to 2fd855fbfc8239285aa2d596f76a8cc75e17ce02
    ending github query because we found starting sha: 7d82b83ed57d188ab3f2441a765a6419685a88a3
    get_commits_between returning commits, len: 7
      commit[0] 2025-08-06: Auto merge of #144509 - cuviper:relnotes-1.89.0, r=BoxyUwU
      commit[1] 2025-08-06: Auto merge of #145020 - GuillaumeGomez:rollup-5prs9kw, r=GuillaumeGomez
      commit[2] 2025-08-07: Auto merge of #115746 - tgross35:unnamed-threads-panic-message, r=cuviper
      commit[3] 2025-08-07: Auto merge of #145014 - bjorn3:revert_preserve_debug_gdb_scripts, r=lqd
      commit[4] 2025-08-07: Auto merge of #144997 - BoxyUwU:bootstrap_bump, r=Mark-Simulacrum
      commit[5] 2025-08-07: Auto merge of #145043 - Zalathar:rollup-3dbvdrm, r=Zalathar
      commit[6] 2025-08-07: Auto merge of #145056 - flip1995:clippy-subtree-update, r=Manishearth
    ERROR: no CI builds available between 7d82b83ed57d188ab3f2441a765a6419685a88a3 and 2fd855fbfc8239285aa2d596f76a8cc75e17ce02 within last 167 days
    

    I believe this regressed in 5e781d0. As far as I can tell 4529dd9 does not exhibit the issue.

  11. added
    S-has-bisection
    regression-from-stable-to-stable
    on Nov 18, 2026
  12. dianne commented on Nov 18, 2026

    @dianne
    Member

    I believe this regressed in 5e781d0. As far as I can tell 4529dd9 does not exhibit the issue.

    that's because #143764 changes the order in which or-alternatives are lowered. this bug existed before that, but it would manifest on other alternatives. given the root cause1, I expect this has been around as long as both super let and if let guards were both in nightly2, and it was definitely around before that PR was merged (which was before if let guards were stable). on the previous nightly, the output is

    arm 1
    arm 2
    drop 2
    

    Footnotes

    1. fixed by deduplicate super let bindings that have the scope of an if let guard's scrutinee #165363 ↩

    2. though this case in particular would also only manifest once pin! was implemented in terms of super let ↩

  13. removed
    regression-from-stable-to-stable
    S-has-bisection
    on Nov 18, 2026
  14. Hoverbear commented on Nov 20, 2026

    @Hoverbear
    Contributor

    Hmm so that'd point to a7c39b6 aka #139114, then?

  15. dianne commented on Nov 21, 2026

    @dianne
    Member

    yep! bisecting with a reproducer that counts how many times a pin!'s pinned value is dropped confirms that expectation, too:

    Details

    ********************************************************************************
    Regression in nightly-2025-04-20
    ********************************************************************************
    
    fetching https://static.rust-lang.org/dist/2025-04-19/channel-rust-nightly-git-commit-hash.txt
    nightly manifest 2025-04-19: 40 B / 40 B [==============================================================] 100.00 % 865.79 KB/s converted 2025-04-19 to 191df20fcad9331d3a948aa8e8556775ec3fe69d
    fetching https://static.rust-lang.org/dist/2025-04-20/channel-rust-nightly-git-commit-hash.txt
    nightly manifest 2025-04-20: 40 B / 40 B [==============================================================] 100.00 % 913.89 KB/s converted 2025-04-20 to 077cedc2afa8ac0b727b7a6cbe012940ba228deb
    looking for regression commit between 2025-04-19 and 2025-04-20
    fetching (via remote github) commits from max(191df20fcad9331d3a948aa8e8556775ec3fe69d, 2025-04-17) to 077cedc2afa8ac0b727b7a6cbe012940ba228deb
    ending github query because we found starting sha: 191df20fcad9331d3a948aa8e8556775ec3fe69d
    get_commits_between returning commits, len: 4
      commit[0] 2025-04-18: Auto merge of #139996 - matthiaskrgr:rollup-0nka2hw, r=matthiaskrgr
      commit[1] 2025-04-19: Auto merge of #140015 - GuillaumeGomez:subtree-update_cg_gcc_2025-04-18, r=antoyo
      commit[2] 2025-04-19: Auto merge of #139114 - m-ou-se:super-let-pin, r=davidtwco
      commit[3] 2025-04-19: Auto merge of #140040 - ChrisDenton:rollup-56bzfuq, r=ChrisDenton
    

    from the reproducer

    #![feature(if_let_guard)]
    #![feature(cell_update)]
    #![allow(stable_features, irrefutable_let_patterns)]
    
    use std::cell::Cell;
    
    struct D<'a>(&'a Cell<usize>);
    impl Drop for D<'_> {
        fn drop(&mut self) {
            self.0.update(|x| x + 1);
        }
    }
    
    fn main() {
        let c = Cell::new(0);
        for x in [false, true] {
            match x {
                false | true if let _ = std::pin::pin!(D(&c)) => {}
                _ => {}
            }
        }
        assert_eq!(c.get(), 2);
    }

    as far as stable rust is concerned, if let guards have always had this bug, but before stabilization it was a nightly-only regression. I don't know if any of our current regression labels fit that case

  16. removed
    I-prioritize
    on Nov 22, 2026
  17. rustbot commented on Nov 22, 2026

    @rustbot
    Collaborator

    Assigning P-medium (discussion on Zulip)

  18. added
    P-medium
    on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-destructorsA-patternsA-pinC-bugF-super_letI-memleakI-unsoundP-mediumS-found-by-fuzzingT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions