Issue 234701
Summary Miscompile due to lambda name mangling clash
Labels new issue
Assignees
Reporter kamrann
    Compilation of the following on latest `main` succeeds but miscompiles, with the invocation of `lam2` generating code for a call to `lam1` resulting in an exit code of `1`.
```
module;

#include <concepts>
#include <array>
#include <ranges>
#include <cstdlib>

export module lambda_bug;

template < typename >
auto bad_function() -> int
{
	static constexpr auto lam1 = [](int n) { std::exit(1); return n; };
	static constexpr auto lam2 = [](int n) { return n; };

	auto const rg = std::array{ 0 };

	for (auto x : rg | std::views::transform(lam2))
	{
		return x;
	}

	for (auto x : rg | std::views::transform(lam1))
	{
	}

	// passes, types are distinguished
	static_assert(not std::same_as< decltype(lam1), decltype(lam2) >);
}

export auto repro() -> int
{
	return bad_function< int >();
}
```
```
import lambda_bug;

int main ()
{
	return repro();
}
```
```
clang version 24.0.0git (https://github.com/llvm/llvm-project.git eabd581c97f4ad061e6d99ca29c29cdb14434f2a)
Target: x86_64-unknown-linux-gnu
Thread model: posix

Compiled with clang++ -std=c++23 -stdlib=libc++
```

The below variation on the other hand produces a compiler diagnostic, which presumably is the same underlying bug.
```
export module mod;
template <typename> auto bad_function() -> int
{
    static constexpr auto lam1 = [](auto n) { return 0; };
 static constexpr auto lam2 = [](auto n) { return n; };
    char const rg[] = "a";
    for (auto x : rg | std ::views ::transform(lam2)) {
 return x;
    }
    for (auto x : rg | std ::views ::transform(lam1)) {
 }
}
export auto repro() -> int
{
    return bad_function<int>();
}
```
```
error: definition with same mangled name '_ZNSt3__16ranges14transform_viewINS0_8ref_viewIA2_KcEEZW3mod12bad_functionIiEivEUlT_E_E10__iteratorILb0EEdeEv' as another definition
```
A mostly reduced form of this variation is attached. Note that although this variation is a single TU, it still appears to be tied to C++ module-specific name mangling.
[repro.zip](https://github.com/user-attachments/files/33833413/repro.zip)

Some possibly related older (closed) issues:
#33271
#60985

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to