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

    <tr>
        <th>Summary</th>
        <td>
            [clang-modules] Duplicate symbols from unimported files
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    If you create a module containing two submodules, each with a single file, and attempt to include one submodule, then currently, all symbols from *all* submodules are imported into the translation unit (but any attempt to use the other submodule results in an error if it was from a file that was not `#included`). This presents an issue when you attempt to declare symbols with the same name as declared in an unused submodule.

As a concrete example I came across, the apple sysroot contains the module `Darwin`, which contains both the submodule `ncurses`, containing the function `echo`, and the submodule `net`, containing various internet related things. Attempting to `#include <net/if.h>` thus prevents you from defining any symbols called `echo`, despite `curses.h` not being transitively included.

For example, given the following file:
```
#include <foo.h> // foo.h is empty

int barfunc() {
    return 42;
}

void barstr() {}
void barnamespace() {}

int main() {
    return 0;
}

```

I receive the following error:
```
main.cc:3:5: error: functions that differ only in their return type cannot be overloaded
    3 | int barfunc() {
      | ~~~ ^
/usr/local/google/home/msta/demo/bar.h:1:6: note: previous declaration is here
    1 | void barfunc();
      | ~~~~ ^
main.cc:7:6: error: redefinition of 'barstr' as different kind of symbol
    7 | void barstr() {}
      |      ^
/usr/local/google/home/msta/demo/bar.h:2:13: note: previous definition is here
    2 | const char* barstr = "bar";
      | ^
main.cc:8:6: error: redefinition of 'barnamespace' as different kind of symbol
    8 | void barnamespace() {}
      | ^
/usr/local/google/home/msta/demo/bar.h:4:11: note: previous definition is here
    4 | namespace barnamespace {}
      |           ^
3 errors generated.
```

I've created a minimal example [here](https://github.com/matts1/demo/tree/duplicate-symbol).

Roughly speaking, symbols can be classified into three states:
* Visible (you can use it)
* Hidden (the compiler knows about this symbol, can give you diagnostics about it, but it, but won't let you use it)
* Nonexistent

The current state of things is incompatible with non-module code, as you can end up with code that does not compile because you attempted to reuse the name of a symbol that you never included.

I propose that hidden symbols should be able to be overridden. This would restore compatibility with existing non-module code.

As someone relatively new to llvm, I'm unsure how difficult this would be.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykV02P2zgP_jXKhZjAkcdx5pBDOtPgnct7WBR7lyU61laWDElOmkt_-4KSna92iu4uMJh8mCIfPqTIJyIEfbCIW1Z9YtXbQoyxc37bixjDatE4dd6-t3B2I0iPIiII6J0aDYJ0NgpttT1APDkIY5MfBMZfAYXs4KRjBwKCtgeD0GqD9EhYBSJG7IcI0YG20owKwVm8-iC72KEFOXqPNppzOmkMhHPfOBOg9a4HxnfCGMZ3N9FBeATdD85HVKBtdOQJohc2GBG1szBaHYHxTTNGEPZ8i2YMmMxd7NBfvYLHMJoYQFsQFtB750G3oCOcxARGpAwhdiJ_aV0Eti4YL6cUVfr0soQvnQ4weAxoYyB_OoQR4UQJE9M3eBRKQwnNaSdKCWAQPYKlfyLMVmqCN9oxoLqiX7Jix4rdLoCgqkmPEQG_iX4wCO8gkxfpXQgT7yAGehTOwTsX50qH9GgihK2LN-FP2qakXuHUadldLRs347xQyNaFlaMPGKYjtw3UIbSjlak8bF2g7NxkRe3yoyOMPzo5Cq_dSDWK6C1G8GgENUHstD2EJewyrymgu68NsPKVnPK9bpcdKz-zdQGxG1OdjqlOVJlUaIVtDki9MxdGCmNQPWBXGAYdE-Kc-bIjv9QZDSYY1JU66iOa83wT1FSuvfNzkcjXQR_RZqKcMe5Ex9OVKsmYAua_YnefVetcSggY3zO-h_QZdACi4pxDaRuhEZ4qwPiG8Rdg9SdW7AAAPMbRW3jmrKSvWP2WzxydVnQoRH9zJj2dH1F7hkFIfDSYQvZC24_iFQ_h7hIsdu_gUaI-4gMj6Wb-SAlFWkrJyl3Jyl3Fyt3F8tJ3IV9dpdsWPTibKkLutZ9BxfOAIIXNBQR3RG-coJudsZfA6lf4FZuQLL5__w6s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