Skip to content

Commit 684955a

Browse files
committed
[Modules] [doc] Document the problem that we can't include headers before import declarations
1 parent e3b4c1b commit 684955a

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

clang/docs/StandardCPlusPlusModules.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,50 @@ and add the label ``clang:modules`` (if you have permissions for that).
616616

617617
For higher level support for proposals, you could visit https://clang.llvm.org/cxx_status.html.
618618

619+
Including headers after import is problematic
620+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
621+
622+
For example, the following example can be accept:
623+
624+
.. code-block:: c++
625+
626+
#include <iostream>
627+
import foo; // assume module 'foo' contain the declarations from `<iostream>`
628+
629+
int main(int argc, char *argv[])
630+
{
631+
std::cout << "Test\n";
632+
return 0;
633+
}
634+
635+
but it will get rejected if we reverse the order of ``#include <iostream>`` and
636+
``import foo;``:
637+
638+
.. code-block:: c++
639+
640+
import foo; // assume module 'foo' contain the declarations from `<iostream>`
641+
#include <iostream>
642+
643+
int main(int argc, char *argv[])
644+
{
645+
std::cout << "Test\n";
646+
return 0;
647+
}
648+
649+
Both of the above examples should be accepted.
650+
651+
This is a limitation in the implementation. In the first example,
652+
the compiler will see and parse <iostream> first then the compiler will see the import.
653+
So the ODR Checking and declarations merging will happen in the deserializer.
654+
In the second example, the compiler will see the import first and the include second.
655+
As a result, the ODR Checking and declarations merging will happen in the semantic analyzer.
656+
657+
So there is divergence in the implementation path. It might be understandable that why
658+
the orders matter here in the case.
659+
(Note that "understandable" is different from "makes sense").
660+
661+
This is tracked in: https://github.com/llvm/llvm-project/issues/61465
662+
619663
Ambiguous deduction guide
620664
~~~~~~~~~~~~~~~~~~~~~~~~~
621665

0 commit comments

Comments
 (0)