Issue 108831
Summary [clang-19, rc4] [cxx-modules] error: declaration attached to named module can't be attached to other modules
Labels new issue
Assignees
Reporter Silverlan
    Repository with reduced code for reproduction: https://github.com/Silverlan/test_cxx_modules

I have a simple cxx-module library consisting of two module fragments, `a` and `b`.
`a.cppm`:
```cpp
module;

export module test_module.a;

export namespace test_module {
	class A {
	public:
#ifdef DEFINE_IN_CLASS
		class B* GetB() { return nullptr; }
#else
		class B* GetB();
#endif
	};
};
```

`b.cppm`:
```cpp
module;

export module test_module.b;

export namespace test_module {
	class B {
	public:

	};
};
```

The method in class `A` uses class `B` by forward declaration. The method is defined in a separate cpp-file, `a.cpp` (unless `DEFINE_IN_CLASS` is defined).
`a.cpp`:
```cpp
module;

module test_module.a;

import test_module.b;

#ifndef DEFINE_IN_CLASS
class test_module::B* test_module::A::GetB() { return nullptr; }
#endif
```

There are four test cases:
1) clang-18, `DEFINE_IN_CLASS` defined: Build is successful.
2) clang-18, `DEFINE_IN_CLASS` *not* defined: Build is successful.
3) clang-19, `DEFINE_IN_CLASS` defined: Build is successful.
3) clang-19, `DEFINE_IN_CLASS` *not* defined: Build fails with the following error:
```
FAILED: CMakeFiles/test_lib.dir/Debug/src/a.cpp.o 
/home/runner/work/test_cxx_modules/test_cxx_modules/LLVM-19.1.0-rc4-Linux-X64/bin/clang++ -DCMAKE_INTDIR=\"Debug\"  -g -std=gnu++20 -MD -MT CMakeFiles/test_lib.dir/Debug/src/a.cpp.o -MF CMakeFiles/test_lib.dir/Debug/src/a.cpp.o.d @CMakeFiles/test_lib.dir/Debug/src/a.cpp.o.modmap -o CMakeFiles/test_lib.dir/Debug/src/a.cpp.o -c /home/runner/work/test_cxx_modules/test_cxx_modules/src/a.cpp
In file included from /home/runner/work/test_cxx_modules/test_cxx_modules/src/a.cpp:9:
/home/runner/work/test_cxx_modules/test_cxx_modules/src/b.cppm:6:8: error: declaration 'B' attached to named module 'test_module.b' can't be attached to other modules
    6 |         class B {
      | ^
/home/runner/work/test_cxx_modules/test_cxx_modules/src/a.cppm:11:9: note: also found
   11 |                 class B* GetB();
      | ^
1 error generated.
ninja: build stopped: subcommand failed.
Error: Process completed with exit code 1.
```

Steps to reproduce:
1) Fork https://github.com/Silverlan/test_cxx_modules
2) Run the action/workflow `Clang Build Error`
3) You can see each test case in the workflow log:
![firefox_ZQY00qO7Dm](https://github.com/user-attachments/assets/37a4908e-0399-4670-bf61-64f4310534c5)

Alternatively you can also reproduce it locally by cloning the repository and building it with clang-19 (rc4). `DEFINE_IN_CLASS` can be enabled/disabled with CMake:
```
git clone https://github.com/Silverlan/test_cxx_modules
cd test_cxx_modules
mkdir -p build
cd build
cmake -DDEFINE_IN_CLASS=OFF -DCMAKE_C_COMPILER=../LLVM-19.1.0-rc4-Linux-X64/bin/clang -DCMAKE_CXX_COMPILER=../LLVM-19.1.0-rc4-Linux-X64/bin/clang++ -G "Ninja Multi-Config" -DCMAKE_BUILD_TYPE=Release ..
cmake --build .
```
(Replace the paths to clang-19 compiler. I've also only tested it with ninja.)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to