Skip to content
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

Binding subpattern with @ allows for illegal mutation #14587

Closed
Marwes opened this issue Jun 1, 2014 · 6 comments · Fixed by #16053
Closed

Binding subpattern with @ allows for illegal mutation #14587

Marwes opened this issue Jun 1, 2014 · 6 comments · Fixed by #16053
Assignees
Milestone

Comments

@Marwes
Copy link
Contributor

Marwes commented Jun 1, 2014

fn main() {
    match &mut Some(1) {
        ref mut z @ &Some(ref a) => {
            **z = None;
            println!("{}", *a);
        }
        _ => ()
    }
}

This code compiles even though it should be rejected since writing to z invalidates the a variable.

@alexcrichton
Copy link
Member

That sounds quite bad, nominating.

@pnkfelix
Copy link
Member

pnkfelix commented Jun 5, 2014

P-backcompat-lang, 1.0.

@pnkfelix pnkfelix added this to the 1.0 milestone Jun 5, 2014
@nikomatsakis
Copy link
Contributor

cc me

@nikomatsakis
Copy link
Contributor

Ah, I think I know why this happens. Fascinating. It's no doubt because the matched expression is an rvalue. I think we'll have to upgrade how rvalues are handled in borrowck to fix this.

@alexcrichton
Copy link
Member

cc #12534

@nikomatsakis
Copy link
Contributor

Basically the problem is that we treat rvalues as though they are only ever once -- but with a devious match expression like this one you can arrange to consume them / borrow them multiple times. I think we ought to modify the memcategorization to store the expr-id of the rvalue and not just the temporary scope -- the borrow checker can then treat rvalues just as it treats lvalues. Basically we want to have the borrow checker reason about the temporary that will be created.

pcwalton added a commit to pcwalton/rust that referenced this issue Aug 1, 2014
This is an alternative to upgrading the way rvalues are handled in the
borrow check. Making rvalues handled more like lvalues in the borrow
check caused numerous problems related to double mutable borrows and
rvalue scopes. Rather than come up with more borrow check rules to try
to solve these problems, I decided to just forbid pattern bindings after
`@`. This affected fewer than 10 lines of code in the compiler and
libraries.

This breaks code like:

    match x {
        y @ z => { ... }
    }

    match a {
        b @ Some(c) => { ... }
    }

Change this code to use nested `match` or `let` expressions. For
example:

    match x {
        y => {
            let z = y;
            ...
        }
    }

    match a {
        Some(c) => {
            let b = Some(c);
            ...
        }
    }

Closes rust-lang#14587.

[breaking-change]
bors added a commit that referenced this issue Aug 1, 2014
This is an alternative to upgrading the way rvalues are handled in the
borrow check. Making rvalues handled more like lvalues in the borrow
check caused numerous problems related to double mutable borrows and
rvalue scopes. Rather than come up with more borrow check rules to try
to solve these problems, I decided to just forbid pattern bindings after
`@`. This affected fewer than 10 lines of code in the compiler and
libraries.

This breaks code like:

    match x {
        y @ z => { ... }
    }

    match a {
        b @ Some(c) => { ... }
    }

Change this code to use nested `match` or `let` expressions. For
example:

    match x {
        y => {
            let z = y;
            ...
        }
    }

    match a {
        Some(c) => {
            let b = Some(c);
            ...
        }
    }

Closes #14587.

[breaking-change]

May need discussion at the meeting, but r? @nikomatsakis anyway
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 5, 2023
fix: Bring back LRU limit for macro_expand query

Should fix the memory increase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants