-
Notifications
You must be signed in to change notification settings - Fork 12.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using import after #include may cause redeclaration errors #61329
Comments
@llvm/issue-subscribers-clang-modules |
Could you try the following one? #include <iostream>
import foo;
int main(int argc, char *argv[])
{
std::cout << "Test\n";
return 0;
} In short, your codes are correct but the compiler fails to handle it due to some implementation limitations/defects. In detail, if we include first and import then, the compiler will perform the checking and merging in the deserializer. But if we import first and include second, the compiler will perform the checking and merging in the semantic analyzer. So there is a divergence in the code paths in the implementation. I heard MSVC has the same problem. It is not your fault. |
Yes, switching the order to #include, then import causes the error to go away. I'm trying to think now if that will always be possible. It would be a problem if an include'd header had an import, but I guess that shouldn't happen. |
A header shouldn't contain an import. This should be a bad manner. (Although I heard some people don't agree on this.) |
There are specific rules for module TUs (currently clang does not diagnose these cases) :
(these two things we should fix soon, since we now track the module phase in the pre-processor). For a non-modular TU AFAIU, it is permitted to include textually a header that has an import - perhaps that is bad style, but I have no strong opinion except to say that maybe it is better to have consistent style between the module purview and an importer. |
FYI I ran into the same issue while testing libc++'s tests with the WIP version of the |
It is even not accurate to say "hard to fix it"... since it consists a lot of problems, e.g., we don't merge concepts well in Sema, we don't merge requires well in Sema ... This is a long list so it is hard to say when we can fix it. BTW, the deserializer has similar problems too. Just the deseriailizer does a better merging job than sema now.
I think it'd better to mention this in the document for modules. Let me try to take it. |
Thanks for the info @ChuanqiXu9. |
Let's track the issue in #61465 instead. |
I'm new to C++20 modules, so I don't know if this is supposed to work or not. It seems like a C++ file that uses modules and traditional
#include
s can get errors.Here is a minimal example, using a recent HEAD (Clang 17).
build.sh
(obviously needs to be adjusted a bit for your environment):foo.ccm
:main.cc
:Leads to these errors:
The text was updated successfully, but these errors were encountered: