Skip to content

Valid code fails with "reference to local variable declared in enclosing function" #140632

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

Closed
Ignition opened this issue May 19, 2025 · 5 comments · Fixed by #140699
Closed

Valid code fails with "reference to local variable declared in enclosing function" #140632

Ignition opened this issue May 19, 2025 · 5 comments · Fixed by #140699
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation

Comments

@Ignition
Copy link

#include <array>
#include <ranges>

int main() {
    constexpr auto v = std::array{1, 2, 3, 4, 5, 6};
    for (auto const& [t0, t1, t2] : v | std::views::adjacent<3>) {
    }
}

error: reference to local variable 'v' declared in enclosing function 'main'

Works in GCC 15.1
Error in clang 20.1.0
Works if you remove constexpr

godbolt link

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation and removed new issue labels May 19, 2025
@llvmbot
Copy link
Member

llvmbot commented May 19, 2025

@llvm/issue-subscribers-clang-frontend

Author: Gareth Andrew Lloyd (Ignition)

```cpp #include <array> #include <ranges>

int main() {
constexpr auto v = std::array{1, 2, 3, 4, 5, 6};
for (auto const& [t0, t1, t2] : v | std::views::adjacent<3>) {
}
}

&gt; error: reference to local variable 'v' declared in enclosing function 'main'

Works in GCC 15.1
Error in clang 20.1.0
Works if you remove constexpr

[godbolt link](https://godbolt.org/z/7d3aW5Y4b)
</details>

@shafik
Copy link
Collaborator

shafik commented May 20, 2025

These look like duplicates: #140622

If you think there is a difference then we would need a reduction to verify it.

cor3ntin added a commit to cor3ntin/llvm-project that referenced this issue May 20, 2025
…out initializer

The evaluation context was improperly set up, such that we were
trying to setup cleanups for a global var at the point of use,
which lead to incorrect diagnostics about the variable not being
capturable.

Fixes llvm#140632
Fixes llvm#140622
cor3ntin added a commit to cor3ntin/llvm-project that referenced this issue May 20, 2025
…out initializer

The evaluation context was improperly set up, such that we were
trying to setup cleanups for a global var at the point of use,
which lead to incorrect diagnostics about the variable not being
capturable.

Fixes llvm#140632
Fixes llvm#140622
cor3ntin added a commit that referenced this issue May 20, 2025
…out initializer (#140699)

The evaluation context was improperly set up, such that we were trying
to set up cleanups for a global variable at the point of use, which led
to incorrect diagnostics about the variable not being capturable.

Fixes #140632
Fixes #140622
@shafik
Copy link
Collaborator

shafik commented May 20, 2025

@Endilll I think it might be worth it to get a reduction for this, to see if it ends up being a different test case once it is minimal than the other cases.

@cor3ntin
Copy link
Contributor

@shafik I did check that this code was fixed by #140699

@shafik
Copy link
Collaborator

shafik commented May 20, 2025

@shafik I did check that this code was fixed by #140699

yeah, I want to see if the reduction yields a significantly different test case, so we can expand coverage but maybe it just boils down to the same code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation
Projects
None yet
5 participants