https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122551
Bug ID: 122551
Summary: [modules] ICE when instantiating an imported friend
function in a module interface
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Keywords: ice-on-valid-code
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: nshead at gcc dot gnu.org
Blocks: 103524
Target Milestone: ---
Consider:
// a.cpp
export module M;
export template <typename T> struct S {
template <typename U> friend void f(U);
};
template <typename U> void f(U u) {}
// b.cpp
export module X;
import M;
void test() {
S<int>();
}
$ g++ -S -fmodules a.cpp b.cpp
b.cpp:1:8: internal compiler error: in finalize_dependencies, at
cp/module.cc:15594
1 | export module X;
| ^~~~~~
0x3e7e9ea internal_error(char const*, ...)
../../gcc/gcc/diagnostic-global-context.cc:787
0x3e8e718 fancy_abort(char const*, int, char const*)
../../gcc/gcc/diagnostics/context.cc:1806
0x1201a24 depset::hash::finalize_dependencies()
../../gcc/gcc/cp/module.cc:15594
0x1212002 module_state::write_begin(elf_out*, cpp_reader*,
module_state_config&, unsigned int&)
../../gcc/gcc/cp/module.cc:20584
0x121a8bf finish_module_processing(cpp_reader*)
../../gcc/gcc/cp/module.cc:23190
0x1125cdd c_parse_final_cleanups()
../../gcc/gcc/cp/decl2.cc:5822
0x15506d6 c_common_parse_file()
../../gcc/gcc/c-family/c-opts.cc:1436
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
This hits the following checking assertion added in r15-6890:
/* Bindings shouldn't refer to imported entities. */
if (CHECKING_P)
for (depset *entity : dep->deps)
gcc_checking_assert (!entity->is_import ());
The issue is that '::f' gets reinserted into the local binding table by
tsubst_friend_function, but still is marked as DECL_MODULE_IMPORT_P. Allowing
this through could end up with similar crashes to PR117397, though I haven't
attempted to craft any yet.
We need to either not make a binding for this entity or ensure that
tsubst_friend_function clears DECL_MODULE_IMPORT_P once it inserts the
function.
Referenced Bugs:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103524
[Bug 103524] [meta-bug] modules issue