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

Improper identifier hygiene in match arm patterns #1

Closed
frxstrem opened this issue Jan 19, 2022 · 0 comments · Fixed by #2
Closed

Improper identifier hygiene in match arm patterns #1

frxstrem opened this issue Jan 19, 2022 · 0 comments · Fixed by #2
Labels
bug Something isn't working

Comments

@frxstrem
Copy link
Owner

Reported by Reddit user u/pinespear in this comment:

You have some problems with hygiene of identifiers. Basically, identifiers from inner scope can pollute outer scope and it leads to functionally different logic being executed.

Example is a bit artificial, because I tried to minimize it to show the idea:

This works without panic:

let val = Some(vec![0_usize]);

let mut v = vec![1];

match val {
    Some(mut v) => drop(v.pop()),
    None => {}
};

v.pop().unwrap();

This panics:

let val = Some(vec![0_usize]);

let mut v = vec![1];

cain! {
    let _ = match val {
        Some(mut v) => drop(v.pop()),
        None => {}
    };

    v.pop().unwrap();
}

Reason it panics is it expands to:

let val = Some(<[_]>::into_vec(box [0_usize]));
let mut v = <[_]>::into_vec(box [1]);
{
    match val {
        Some(mut v) => {
            let _ = drop(v.pop());
            v.pop().unwrap();
        }
        None => {
            let _ = {};
            v.pop().unwrap();
        }
    }
}

and second v.pop() gets executed on inner identifier v except of outer identifier v

This is trivial case which can be solved by "sanitizing" identifiers in arms and "un-sanitizing" them in the scope so they won't get leaked:

    match val {
        Some(mut v_randomized213345) => {
            let _ = {
                let mut v = v_randomized213345;
                drop(v.pop())
            };
            v.pop().unwrap();
        }
        None => {
            let _ = {};
            v.pop().unwrap();
        }
    }

but there may be other cases which are harder to notice, I'm not sure how can you formally guarantee that code logic and behavior won't get affected.

@frxstrem frxstrem added the bug Something isn't working label Jan 19, 2022
frxstrem added a commit that referenced this issue Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant