Issue 134049
Summary Specific structure name can cause shadowing of globally defined lambda, causing "struct::operator()" to be called instead of "lambda::operator()"
Labels new issue
Assignees
Reporter jjkazana
    Defining structure with naming convention used by the compiler for lambda expressions ("$_\<num\>") causes compiler to use structure's "operator()" instead of the one defined for a global lambda(s).

In the following code:

```cpp
struct $_0
{
    int c;
 void operator()() const {std::cout << __func__ << " " << c <<" from struct\n";}
};

auto lambda = [](){std::cout << __func__ << " from lambda\n";};

int main()
{
    lambda();
}
```
Call to "lambda()" invokes "$_0::operator()" instead of the expected lambda:
![Image](https://github.com/user-attachments/assets/77513173-f5de-483f-b89e-639c43c8c2a4)

If the "$_0::operator()" makes use of any memory, then calls to lambdas result in memory access operations. "-fsanitize=address" allows for detection of them: [godbolt example](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:1,endLineNumber:16,positionColumn:1,positionLineNumber:16,selectionStartColumn:1,selectionStartLineNumber:16,startColumn:1,startLineNumber:16),source:'%23include+%3Ciostream%3E%0A%0Astruct+$_0%0A%7B%0A++++int+var%3B%0A++++void+operator()()+const+%7Bstd::cout+%3C%3C+__func__+%3C%3C+%22+%22+%3C%3C+var+%3C%3C%22+from+struct+(const)%5Cn%22%3B%7D%0A++++void+operator()()+%7Bstd::cout+%3C%3C+__func__+%3C%3C+%22+%22+%3C%3C+var+%3C%3C%22+from+struct+(mutable)%5Cn%22%3B+var%3D0xBEEF%3B%7D%0A%7D%3B%0A%0Aauto+lambda+%3D+%5B%5D()+/*mutable*/+%7Bstd::cout+%3C%3C+__func__+%3C%3C+%22+from+lambda%5Cn%22%3B%7D%3B%0A%0Aint+main()%0A%7B%0A++++lambda()%3B%0A%7D%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:46.16052398750079,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang_trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-O0+-Wall+-fsanitize%3Daddress',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(trunk)+(Editor+%231)',t:'0')),k:31.807650355851912,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+clang+20.1.0',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'0'),l:'5',n:'0',o:'Output+of+x86-64+clang+(trunk)+(Compiler+%231)',t:'0')),k:22.03182565664731,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to