https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108488
Bug ID: 108488 Summary: segfault with -fmodules-ts and class-scope friend declaration first in uninstantiated template Product: gcc Version: 12.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: wendellcraigbaker at gmail dot com Target Milestone: --- Created attachment 54320 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54320&action=edit Complete C++ to exhibit the segfault The sequence of declarations causes a segfault in gcc 12.2.1 under -fmodules-ts template <typename> class k; ...more... struct d { ... void ck(k<int> const &); void ck(int); }; ...more... template <typename> class k { template <typename> friend class ca; }; ...more... auto e::operator+=(e &&cl) -> e & { for (auto bt : cl) ck(bt); return *this; } ...more... template <typename> class ca {}; ca<int> d; It is specific to -fmodules-ts It is specific to the declaration of friend class ca introduced at class scope. The exhibition and script show details. The compiler instance here stock-delivered Fedora 37 GCC 12.2.1 but the problem exists in older versions and also on trunk. https://godbolt.org/z/GGP4dxnjY https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:1,endLineNumber:34,positionColumn:1,positionLineNumber:34,selectionStartColumn:1,selectionStartLineNumber:34,startColumn:1,startLineNumber:34),source:'class+e%3B%0A%0Atemplate+%3Ctypename%3E+class+k%3B%0A%0Astruct+l+%7B%0A++int+operator*()%3B%0A++void+operator%2B%2B()%3B%0A++friend+bool+operator%3D%3D(l,+l)%3B%0A%7D%3B%0Astruct+d+%7B%0A++l+begin()%3B%0A++l+end()%3B%0A++void+ck(k%3Cint%3E+const+%26)%3B%0A++void+ck(int)%3B%0A%7D%3B%0A%0A%0Aclass+e+:+d+%7B%0A++auto+operator%2B%3D(e+%26%26)+-%3E+e+%26%3B%0A%7D%3B%0Atemplate+%3Ctypename%3E+class+k+%7B%0A++template+%3Ctypename%3E+friend+class+ca%3B%0A%7D%3B%0A%0A++auto+e::operator%2B%3D(e+%26%26cl)+-%3E+e+%26+%7B%0A++++for+(auto+bt+:+cl)%0A++++++ck(bt)%3B%0A++++return+*this%3B%0A++%7D%0A%0A%0Atemplate+%3Ctypename%3E+class+ca+%7B%7D%3B%0A%0Aca%3Cint%3E+d%3B%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:27.194244604316548,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+clang+3.5',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+gcc+(trunk)+(Compiler+%231)',t:'0')),header:(),k:47.805755395683455,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:gsnapshot,deviceViewOpen:'1',filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-std%3Dc%2B%2B23+-fmodules-ts',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+gcc+(trunk)+(Editor+%231)',t:'0')),k:25,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4 On goldbolt.org, against trunk, the error messaging is: ' tree check: expected class 'type', have 'declaration' (type_decl) in get_originating_module_decl, at cp/module.cc:18634 32 | template <typename> class ca {}; | ^~ 0x23aef7e internal_error(char const*, ...) ???:0 0x939ce7 tree_class_check_failed(tree_node const*, tree_code_class, char const*, int, char const*) ???:0 0xbc4c9b get_originating_module(tree_node*, bool) ???:0 0x23ce58c pp_format(pretty_printer*, text_info*) ???:0 0x23cf79d pp_format_verbatim(pretty_printer*, text_info*) ???:0 0x23cf885 pp_verbatim(pretty_printer*, char const*, ...) ???:0 0x23ad925 diagnostic_report_diagnostic(diagnostic_context*, diagnostic_info*) ???:0 0x23aef7e internal_error(char const*, ...) ???:0 0x939ce7 tree_class_check_failed(tree_node const*, tree_code_class, char const*, int, char const*) ???:0 0xbc4c9b get_originating_module(tree_node*, bool) ???:0 0xb6f28c decl_as_string(tree_node*, int) ???:0 0xb6f2c3 decl_as_dwarf_string(tree_node*, int) ???:0 0x1220e3c rest_of_type_compilation(tree_node*, int) ???:0 0xabf59e finish_struct_1(tree_node*) ???:0 0xca51c0 instantiate_class_template(tree_node*) ???:0 0xb258f3 start_decl_1(tree_node*, bool) ???:0 0xb48077 start_decl(cp_declarator const*, cp_decl_specifier_seq*, int, tree_node*, tree_node*, tree_node**) ???:0 0xc4e137 c_parse_file() ???:0 0xd89ad9 c_common_parse_file() ???:0 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. Compiler returned: 1