[clang] [clang-repl] Allow lambda to capture top level varible in increamental mode (PR #178579)
Vassil Vassilev via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 28 23:58:42 PST 2026
vgvassilev wrote:
> > I am a bit confused, if the lambda is on the global scope it implicitly captures by reference, right: [https://godbolt.org/z/rM1z1xfjq我有点困惑,如果](https://godbolt.org/z/rM1z1xfjq%E6%88%91%E6%9C%89%E7%82%B9%E5%9B%B0%E6%83%91%EF%BC%8C%E5%A6%82%E6%9E%9C) lambda 在全局作用域中,它会隐式地按引用捕获,对吧:https://godbolt.org/z/rM1z1xfjq
> > In that sense I'd think the diagnostic is correct, right?从这个角度来看,我认为诊断是正确的,对吧?
>
> It really depends on how clang-repl defines its execution context. Intuitively, we feel like we are working within a function scope (like main), so we unconsciously reach for [&] or [&variable] to capture surrounding state. However, technically, clang-repl treats these variables as globals to ensure persistence, meaning we can access them directly without any capture. The diagnostic, therefore, reflects this implementation choice: we perceive it as a local context, but the tool defines it as global. So should user see variable in the top level of repl the same kind like some variable in a function context?
I believe it should behave in the same way as if we are working on the global scope. For example `auto capture = [&]() { return x * 2; };` has little to do with executing statements on the global scope. I'd say this issue is invalid as what clang-repl displays is exactly what compiled c++ does. I think we should close the issue with "won't fix" and clarify this there. What do you think?
https://github.com/llvm/llvm-project/pull/178579
More information about the cfe-commits
mailing list