Skip to content

drop-checking is more permissive when patterns have guards #142057

New issue

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

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

Already on GitHub? Sign in to your account

Open
dianne opened this issue Jun 5, 2025 · 0 comments
Open

drop-checking is more permissive when patterns have guards #142057

dianne opened this issue Jun 5, 2025 · 0 comments
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Comments

@dianne
Copy link
Contributor

dianne commented Jun 5, 2025

When pattern bindings are lowered to MIR, they're dropped in reverse-order of declaration (with some caveats when or-patterns are involved). However, by-ref bindings are created eagerly before anything else if guards are present, so that they can be used in guards (#49870). Because that affects the order in which drops are scheduled, this difference is observable through the drop check (playground link):

struct Struct<T>(T);
impl<T> Drop for Struct<T> {
    fn drop(&mut self) {}
}

fn main() {
    // This is an error: `short1` is dead before `long1` is dropped.
    match (Struct(&&0), 1) {
        (mut long1, ref short1) => long1.0 = &short1,
    }
    // This is OK: `short2`'s storage is live until after `long2`'s drop runs.
    match (Struct(&&0), 1) {
        (mut long2, ref short2) if true => long2.0 = &short2,
        _ => unreachable!(),
    }
}

Related: #142056

cc @rust-lang/lang since if this needs fixing it'll involve changing what programs are allowed. I imagine that needs a T-lang decision?

@rustbot label: +T-compiler +T-lang +A-MIR

@dianne dianne added the C-bug Category: This is a bug. label Jun 5, 2025
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team labels Jun 5, 2025
@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team
Projects
None yet
Development

No branches or pull requests

3 participants