<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/131814>131814</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[modules] Compilation success depends on order of declarations in the modulemap
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang,
clang:modules
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ldionne
</td>
</tr>
</table>
<pre>
Consider the following set of headers:
```c++
// myvector.h
#ifndef MYVECTOR_H
#define MYVECTOR_H
#include <bit_ref.h>
template <class _Allocator>
struct MyVector {
typedef bool value_type;
typedef unsigned long __storage_type;
friend class MyBitIterator<MyVector, false>;
};
#endif
```
```c++
// bit_ref.h
#ifndef BIT_REF_H
#define BIT_REF_H
#include <bit_ref_fwd.h>
#endif
```
```c++
// bit_ref_fwd.h
#ifndef BIT_REF_FWD_H
#define BIT_REF_FWD_H
template <class _Cp, bool _IsConst, typename _Cp::__storage_type = 0>
class MyBitIterator {};
#endif
```
And the following modulemap:
```
module mystd {
module myvector {
header "myvector.h"
export mystd.bit_ref
}
module bit_ref_fwd {
header "bit_ref_fwd.h"
}
module bit_ref {
header "bit_ref.h"
export mystd.bit_ref_fwd
}
}
```
The following fails to compile:
```
echo '#include <myvector.h>' | clang++ -x c++ - -std=c++17 -fmodules -fcxx-modules -Xclang -fmodules-local-submodule-visibility -fsyntax-only -I $(dirname ${0}) -fmodules-cache-path="$(mktemp -d)"
```
The error is:
```
While building module 'mystd' imported from <stdin>:1:
In file included from <module-includes>:1:
./repro-modules/myvector.h:11:16: error: missing '#include "bit_ref_fwd.h"'; 'MyBitIterator' must be declared before it is used
11 | friend class MyBitIterator<MyVector, false>;
| ^
./repro-modules/bit_ref_fwd.h:5:7: note: declaration here is not visible
5 | class MyBitIterator {};
| ^
<stdin>:1:10: fatal error: could not build module 'mystd'
1 | #include <myvector.h>
| ~~~~~~~~^
2 errors generated.
```
A few things can be observed:
1. If we move the definition of `bit_ref_fwd` and `bit_ref` *before* the definition of `myvector` in the modulemap, the issue goes away.
2. If we remove `export mystd.bit_ref` from the `myvector` module, the issue goes away.
3. If we add a direct include of `#include <bit_ref_fwd.h>` in `myvector.h`, the issue goes away.
Is this behavior intended? If the order of module definitions in a modulemap matters, there should be a warning that tells us when the compiler encounters something that has not been defined yet in the modulemap. If the order is irrelevant, then there's a bug in Clang.
This was discovered while investigating https://github.com/llvm/llvm-project/pull/127015.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykV0-P4j4S_TTmUgIlDpDmwCFNN9o-jFb6afSb3RNy4grxrmMj24Hmsp99VXZooIfp2T8IdYeyXfXq1asyCO_V3iCu2eKZLV4mYgiddWstlTUGJ7WV5_XGGq8kOggdQmu1tidl9uAxgG2hQyHReVZULIvvZZbeDePP9M4qxreMb6E_H7EJ1s26aCtUayS28O3vf75uvv_1j91fklliqwx-MqcDptGDRGDFplZh57Cddax4TesB-4MWIa42WngPu0pr24hgXdrkgxuaAN_Of0YYwEoCBxDOByQgtbUajkIPuCMTK-6XBxOpkqCt2cNu54N1Yn-_t3UKjYQU_9v5WYW3gC5B2FwCM76BVmiPBCseZOXL-BDzRCNVe0vl75i9snFL7PPb990fr9tPvN5Zf0Hrrj3JG2r_D0ijp0ewtj9efgHtY-VhVTcHIjAWa_fmSZyBDFQGI3qMG4qKFdV9iYAVL5ClnB7UJ6rhPypDZeSnTuitHDT24vBzE7CsSqvQn32QF8192I73UoSxnYBxftMunI-r-H6wLiRfs5FiWiLkMTBcXN_w_9D7fX3GAMnNZx9fnf8dOPJ_AzD9vefz-x2XrVDaQ7DQ2P6gND6kFJvOAuPlvXhvCCteGS-BlRtqRbNP4oTpOzSXR5j6IFnxMhryEqZtStvDtG3e36cfn_4WfVzXpzRV9NQPdTJMj8qrWmkVzjBt_dkE8T61Rp9h-gaMzxl_kspFbdKn8jkjHvjqxmMjmg6nBxE6VrwQo_FU_0-SP0wl46tE8wPq0DnrQD0Yw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