llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.24k stars 11.66k forks source link

[clangd][modules] Clangd reports ODR violation when including iostream from (global module fragment of) two different modules #100924

Open alfaix opened 2 months ago

alfaix commented 2 months ago

Hey, I'm trying to get modules to work with clangd following this PR.

However, when I run clangd with the following files:

// b.cppm
module;
#include <iostream>
export module b;

export template <class T>
concept C = requires() {
  { T::foo } -> std::convertible_to<int>;
};

// c.cppm
module;

#include <iostream>

export module c;
import b;
class B {
public:
  static constexpr int foo = 1;
};

export template <class T>
  requires C<T>
class D {};

int main() { D<B> s; }

Put -std=c++23 -stdlib=libc++ and all the -fmodule things in compile_commands.json, clangd reports an ODR violation, saying the 2 definitions of std::error_category from the 2 includes are different: E[12:04:26.095] [module_odr_violation_missing_decl] Line 3: in included file: 'std::error_category::message' from module '' is not present in definition of 'std::error_category' in module 'b.<global>'

The setup is sensitive to small changes - e.g., removing std::convertible from the concept leads to the error disappearing. I put the repro in a small repo for convenience.

Tested with Ubuntu clang version 20.0.0 (++20240727042143+9a3e66e314e6-1~exp1~20240727042317.1828) straight out of apt.llvm.org snapshot repo.

alfaix commented 2 months ago

Could be related to https://github.com/llvm/llvm-project/issues/58540, although there the discussion is about importing header units.

alfaix commented 2 months ago

Ah upon closer examination seems to be same as #78850

That said, I thought the -fskip-odr-check-in-gmf should have fixed it for now

llvmbot commented 2 months ago

@llvm/issue-subscribers-clang-modules

Author: Arsen Kitov (alfaix)

Hey, I'm trying to get modules to work with clangd following [this PR](https://github.com/llvm/llvm-project/pull/66462). However, when I run `clangd` with the following files: ```cpp // b.cppm module; #include <iostream> export module b; export template <class T> concept C = requires() { { T::foo } -> std::convertible_to<int>; }; // c.cppm module; #include <iostream> export module c; import b; class B { public: static constexpr int foo = 1; }; export template <class T> requires C<T> class D {}; int main() { D<B> s; } ``` Put `-std=c++23 -stdlib=libc++` and all the `-fmodule` things in `compile_commands.json`, clangd reports an ODR violation, saying the 2 definitions of `std::error_category` from the 2 includes are different: `E[12:04:26.095] [module_odr_violation_missing_decl] Line 3: in included file: 'std::error_category::message' from module '' is not present in definition of 'std::error_category' in module 'b.<global>'` The setup is sensitive to small changes - e.g., removing std::convertible from the concept leads to the error disappearing. I put the repro in a [small repo](https://github.com/alfaix/clangd-odr-repro) for convenience. Tested with `Ubuntu clang version 20.0.0 (++20240727042143+9a3e66e314e6-1~exp1~20240727042317.1828)` straight out of apt.llvm.org snapshot repo.
llvmbot commented 2 months ago

@llvm/issue-subscribers-clangd

Author: Arsen Kitov (alfaix)

Hey, I'm trying to get modules to work with clangd following [this PR](https://github.com/llvm/llvm-project/pull/66462). However, when I run `clangd` with the following files: ```cpp // b.cppm module; #include <iostream> export module b; export template <class T> concept C = requires() { { T::foo } -> std::convertible_to<int>; }; // c.cppm module; #include <iostream> export module c; import b; class B { public: static constexpr int foo = 1; }; export template <class T> requires C<T> class D {}; int main() { D<B> s; } ``` Put `-std=c++23 -stdlib=libc++` and all the `-fmodule` things in `compile_commands.json`, clangd reports an ODR violation, saying the 2 definitions of `std::error_category` from the 2 includes are different: `E[12:04:26.095] [module_odr_violation_missing_decl] Line 3: in included file: 'std::error_category::message' from module '' is not present in definition of 'std::error_category' in module 'b.<global>'` The setup is sensitive to small changes - e.g., removing std::convertible from the concept leads to the error disappearing. I put the repro in a [small repo](https://github.com/alfaix/clangd-odr-repro) for convenience. Tested with `Ubuntu clang version 20.0.0 (++20240727042143+9a3e66e314e6-1~exp1~20240727042317.1828)` straight out of apt.llvm.org snapshot repo.
ChuanqiXu9 commented 1 month ago

Would you like to test again on trunk?