Open
Description
Changes to template function bodies in C++ modules don't trigger recompilation of dependent code, allowing invalid code to "compile" and run.
This issue is detrimental to the module workflow and could pose some security risks. Specifically,
- Builds silently succeed instead of an expected fail
- This issue persists across full rebuilds, since module caches are located in
~/.cache/ccache
(this took me a very long time to figure out) - It makes debugging template code de-facto impossible
- Any form of static warnings, assertions or other compile time failures may pass compilation even though they should not
Steps to Reproduce:
- Create interface module
module.ixx
export module somemodule;
volatile int i = 0; // To ensure test is not optimized out, probably not needed
export template<typename T>
int test() {
// static_assert(0); // Should fail at compile time
// *(volatile char*)nullptr = 0; // Should fail at run time
// We will make the above statements run and compile later, but for now we need to make it compile successfully once
return i;
}
- Create source file
test.cpp
import somemodule;
int main() {
return test<int>();
}
- Build and run application
clang++ -std=gnu++23 -x c++-module -fmodule-output=somemodule.pcm -c module.ixx -o module.o
clang++ -std=gnu++23 -fmodule-file=somemodule=somemodule.pcm -c test.cpp -o test.o
clang++ module.o test.o -o test
./test # should run without output, exiting with 0
- Modify the interface module code
export module somemodule;
volatile int i = 0; // To ensure test is not optimized out, probably not needed
export template<typename T>
int test() {
static_assert(0); // Should fail at compile time
*(volatile char*)nullptr = 0; // Should fail at run time
return i;
}
- Compile and run as in step 3
Expected Behavior:
Changes to template function bodies should trigger recompilation of dependent code, and invalid code (static_assert(0)) should fail to compile or fail at runtime (null pointer dereference).
Actual Behavior:
Changes to template function bodies are not detected during recompilation. The program builds successfully even with invalid code in template function bodies.
This is still the case after deleting the build directory and rebuilding.
Additional Notes:
- Issue affects both standalone template functions and class member template functions
- Non-template function changes are properly detected
- Reproducible with both CMake builds and direct compilation
- Affects function bodies only; changes to template parameters, function signatures or structures are detected
Platform:
- OS: Fedora 42
- Compiler: clang version 20.1.5
- C++ Standard: C++23