Issue 128840
Summary clangd false positive redefinition error of type traits template when #include <print> after import with type_traits include
Labels false-positive
Assignees
Reporter FalcoGer
    So when I define such a module

```cpp
// Math.cppm
module;

#include <cmath>
#include <type_traits>
#include <utility>

export module Math;

export template <typename T>
    requires std::is_arithmetic_v<T>
auto add(const T& lhs, const T& rhs)
{
    return lhs + rhs;
}

```

and try to use it

```cpp
#include <print>
import Math;

auto main() -> int
{
    std::println("{}", add(2.0F, 1.0F));
}
```

it works fine.

When I switch `#include <print>` and `import Math;`, it still compiles fine and gives the same results. But clangd explodes in my face telling me that it's not fine with

```text
Too many errors emitted, stopping now (clang fatal_too_many_errors)

In included file: type alias template redefinition with different types ('__is_one_of<[6 * ...], (no argument)>' vs '__is_one_of<[6 * ...], __int128>')

Related information:

  * type_traits#808,11: Error occurred here
  * type_traits#808,11: Previous definition is here

 (clang redefinition_different_typedef)

```

Compiled against libstdc++ from gcc 15.0.1.

```text
$ clang --version
Ubuntu clang version 21.0.0 (++20250224081945+0770afb88ec1-1~exp1~20250224082103.749)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-21/bin
```

`.clangd`:
```yaml
CompileFlags:
  Add: 
 - -fprebuilt-module-path=build/CMakeFiles/MathModule.dir
```

Built with cmake
`CMakeLists.txt`:

```cmake
cmake_minimum_required(VERSION 3.28)

project(TemplateModulesTest)

# REF: selecting-a-specific-libstdc-version-with-clang
# https://stackoverflow.com/a/50964601/10263660

set(GCC_TOOLCHAIN_PATH "/usr/local/gcc/15.0.1")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --gcc-toolchain=${GCC_TOOLCHAIN_PATH}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --gcc-toolchain=${GCC_TOOLCHAIN_PATH}")

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

add_library(MathModule)
target_sources(MathModule
  PUBLIC
 FILE_SET cxx_modules TYPE CXX_MODULES FILES
 Math.cppm
)

set_target_properties(MathModule PROPERTIES CXX_SCAN_FOR_MODULES YES)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE MathModule)
```

Building first is required for clangd to find the module and not freak out about it not existing.

```bash
mkdir build
cmake -DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -G Ninja ..
ninja
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to