Issue 98963
Summary lambda in class body should have the same type among different TUs
Labels new issue
Assignees
Reporter namniav
    [basic.def.odr#15.6](https://eel.is/c++draft/basic.def.odr#15.6)
> In each such definition, except within the default arguments and default template arguments of `D`, corresponding *lambda-_expression_*s shall have the same closure type

When the lambda _expression_ is used with `decltype` in side class body(but not inside a function), Clang with libc++ generates different closure types in different TUs.
Clang with libstdc++ seems not have this problem. GCC also has this problem for both `F1` and `F2`.

Demo: https://godbolt.org/z/M4xjWesYn

* common.hpp
```c++
#include <typeinfo>
#include <array>

struct A {
    using F1 = decltype([]{});   // Clang with libc++  generates different closure types for different TUs
    inline static auto f2 = []{};
    using F2 = decltype(f2);  // Clang Ok, GCC No
};

[[gnu::always_inline]]
inline auto func() {
    using F3 = decltype([]{}); // OK
    auto f4 = []{};
    using F4 = decltype(f4); // OK
    return std::array{ &typeid(F3), &typeid(F4) };
}

auto FOOBAR() {
    auto t = func();
    return std::array{
        &typeid(A::F1),
        &typeid(A::F2),
 t[0],
        t[1]
    };
}
```
* foo.cpp
```c++
#define FOOBAR foo
#include "common.hpp"
```

* bar.cpp
```c++
#define FOOBAR bar
#include "common.hpp"
```

* main.cpp
```c++
#include <cstdio>
#include <typeinfo>
#include <array>

std::array<const std::type_info*, 4> foo();
std::array<const std::type_info*, 4> bar();

int main() {
    auto f = foo();
    auto b = bar();
 for (int i = 0; i < 4; ++i) {
        std::printf("F%d same type ? %d\n", i+1, *f[i] == *b[i]);
        //std::printf("name=%s hash_code=%-20llu addr=%p\n", f[i]->name(), (unsigned long long)f[i]->hash_code(), f[i]);
        //std::printf("name=%s hash_code=%-20llu addr=%p\n", b[i]->name(), (unsigned long long)b[i]->hash_code(), b[i]);
    }
}

```

**Outputs** :
* Clang, libc++
> ```
> F1 same type ? 0
> F2 same type ? 1
> F3 same type ? 1
> F4 same type ? 1
> ```

* Clang, libstdc++
> ```
> F1 same type ? 1
> F2 same type ? 1
> F3 same type ? 1
> F4 same type ? 1
> ```

* GCC, libstdc++
> ```
> F1 same type ? 0
> F2 same type ? 0
> F3 same type ? 1
> F4 same type ? 1
> ```

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

Reply via email to