Skip to content

Modify the scope_depth for variables in lambdas #350

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

Conversation

filipsajdak
Copy link
Contributor

If a variable parent is a function without the name, the scope_depth is increased by 1. Refrain from being mixed with variables from the scope where lambda was created.

After this change, the following code:

f_inout: (inout x) -> _ = {
    x *= 2;
    return x;
}

main: () -> int = {
    x := 21;

    l1 := :(forward x) = {
        std::cout << f_inout(forward x) << std::endl;
    };

    l1(x);

    x++;
}

Will generate (skipping boilerplate):

[[nodiscard]] auto f_inout(auto& x) -> auto{
    x *= 2;
    return x; 
}

[[nodiscard]] auto main() -> int{
    auto x {21}; 

    auto l1 {[](auto&& x) -> void{
        std::cout << f_inout(CPP2_FORWARD(x)) << std::endl;
    }}; 

    std::move(l1)(x);

    ++x;
}

Closes #295. All regression tests pass.

If variable parent is a function without the name the scope_depth
is increased by 1. Not to be mixed with variables from scope where
lambda was created.
@hsutter
Copy link
Owner

hsutter commented Apr 14, 2023

Thanks! Hmm. I think this will do it... I'll think some more about whether we should actually be bumping the scope depth, I wonder if there are cases for more complex nested unnamed functions where they really want their own scope level.

@hsutter hsutter merged commit 0a8dcf6 into hsutter:main Apr 14, 2023
@filipsajdak filipsajdak deleted the fsajdak-fix-forwarding-local-variable-when-shadowed-by-forward-lambda-argument branch April 15, 2023 00:24
zaucy pushed a commit to zaucy/cppfront that referenced this pull request Dec 5, 2023
If variable parent is a function without the name the scope_depth
is increased by 1. Not to be mixed with variables from scope where
lambda was created.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Defining lambda that takes argument using forward passing style and calling it on function variable makes cppfront treat the variable as forwarded
2 participants