See [Serialization] Stop demote var definition as declaration by ChuanqiXu9 · Pull Request #172430 · llvm/llvm-project · GitHub for background
Cite @zygoloid 's comment:
The purpose of this mechanism was to maintain an AST invariant that other parts of Clang may reasonably be relying on – specifically that there is at most one definition in a redeclaration chain for a variable.
Ultimately we have a choice: either we make the modules code attempt to build an AST that “looks like” a non-modules AST, for example with at most one definition per variable, function, or class, and the rest of Clang gets to assume that normal traditional C++ rules are in effect, or we allow the AST to directly represent things like a variable that has multiple definitions (or a class with multiple definitions or an inline function with multiple definitions) and the complexity associated with dealing with those things gets distributed across all parts of Clang and tools that consume its ASTs.
So far we’e largely picked the first option. In part that’s because Clang’s modules system was originally a language extension, so keeping its impact contained made sense, and in part that’s because the intent of the modules system has historically mostly been around getting to the same state that a non-modules compilation would reach, but faster. Maybe it’s time to change that, given that modules is now a standard language feature. But I think this is probably something the Clang Area Team should consider and make a conscious decision on.
(If we choose to directly represent the post-modules state in the AST – for example, including the possibility of there being multiple definitions of entities in different TUs in the same AST – I’d encourage that you also model the multiple TUs themselves explicitly, by creating multiple distinctTranslationUnitDecls, one per module.)
Previously we can assume there is at most a definition in a redeclaration chain in a compilation unit. But after modules are introduced, technically we can have multiple same definition in the redeclaration chain.
Previously, for clang header modules, since it is an extension, clang header modules’s solution is, pretending there is at most a definition in the redeclaration chain.
But with C++20 modules, given it is a standard feature, maybe we should change the assumption. That is, there may be multiple definitions within a redeclaration chain, but these definitions must be the same.
This affects #172241, #64034, #149404 and not verified, but I suspected, #171548
Proposal
For clang developers, we can’t no longer assume there is at most one definition in a redeclaration chain. We need to assume there may be multiple definitions in a redeclaration chain, but we can assume these definitions are the same.