<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/126373>126373</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Template class friend declaration of template class gets attached to module which instantiates it
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          es1024
      </td>
    </tr>
</table>

<pre>
    When I have a template class in the global module fragment with a template class friend, and the class is instantiated inside a module, the friend declaration of the class appears to get attached to the module. This results in an error if two such modules (or module fragments) do this and one imports the other.

Reproducer (on godbolt: https://godbolt.org/z/61hTx776G):
```cpp
// test.h
template<typename T>
struct Test {
  template<typename U>
  friend class Test;
};

// module1.cppm
module;
#include "test.h"
export module module1;
export void f1(Test<int>) {}

// module2.cppm
module;
#include "test.h"
export module module2;
import module1;
export void f2(Test<float>) {}
```
yields
```
In module 'module1' imported from /app/module2.cppm:4:
test.h:4:16: error: declaration 'Test' attached to named module 'module1' cannot be attached to other modules
    4 |   friend class Test;
      |                ^
test.h:2:8: note: also found
    2 | struct Test {
      |        ^
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVMGOozgQ_ZriUpoIygkkBw5M92Q111VWeza4CF4ZG9mme3q_fmUgM53u7F4WRQrG9V69qnq2DEFfLXMNh69weM7kHAfnaw5FTvusdeqt_nNgi99xkC-MEiOPk5GRsTMyBNQW48B4Na6VBkenZsPYe3kd2UZ81XH4jOm9ZquAnlBatcA3rkQXorRRy8gqLbRKOVfaBEjBKxwVd0Z6GbWz6Pp3NHKaWPqA0eGVI8oYZTewSusUtJLt8DLogJ7DbOJShrTI3juPusf46jDM3bAFBwQ6Ov-xvAB0QpVYdVhKcZZRj5PzMSypXBzY7yBvIG9-58k7NXfsFzKLV6daZyKIBocYpwCiAToDnbeNnfNXoPPfQOeyGC4_qqr8DeiUwvIGynz9ddOUlgsQI4e4GyBvbg0H8RTfJrZyZLyA-AZ5E6Kfu4gXDhGh-gp5g_go_I81HG_tXnubYCASCqrn7eVn-rU9xa6bphHyZpvaGkRC287MihGINp1EkDf8I_Xr1tqNYgVtWy9OK-wLoOOa_UnbmMTRaSmgen4ggv6_CFpB6zz_Qxj9EtYbJz9Lu40K8uZNs1Hhw8fv9pYYqLrloWpzEivsvRsR6CynCeh8V6Bo9qshtmrWdVEmVy1uTi_vTwpQtYil6u5gpJGrxzI6aa2L2PIdYLH27XgsPkHcI1RP-K-OweVZQ-4eOHx7XwGBaI5Jt3XJkg1KExz2brZqY6GF5ZGTP2RYmX82O1O1UCdxkhnXRSVOuSiIymyoRVn0XLaChThyWciqLfddQWJ_3AvV8jHTNeV0yCk_FmUh9uWOTq2gQh3KUpTcqSPscx6lNjtjXsZ0dDMdwsx1QaWoRGZkyyYslyyR5VdcdpP5Ds-ZrxPoSztfA-xzo0MMv2iijobry6Mb9NMVeB905RjuZraN93XQ3fD-pg2oYzZ7U3-4h3Qc5nbXuRHonPRsf18m7_7iLgKdlyoC0Hkr86WmfwIAAP__zQ3_DQ">