Bug 120800

Summary: [14/15/16 Regression] internal compiler error when using partially initialized class with private constructor and friend
Product: gcc Reporter: Oskari Alaranta <oskari.alaranta>
Component: c++Assignee: Jason Merrill <jason>
Status: RESOLVED FIXED    
Severity: normal CC: jason, ppalka, webrown.cpp
Priority: P3 Keywords: ice-on-valid-code
Version: 15.1.1   
Target Milestone: 14.4   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118285
Host: Target:
Build: Known to work: 14.2.0
Known to fail: 14.3.0, 15.1.0, 16.0 Last reconfirmed: 2025-06-24 00:00:00
Attachments: output of -freport-bug

Description Oskari Alaranta 2025-06-24 08:50:41 UTC
Created attachment 61698 [details]
output of -freport-bug

I updated my gcc toolchain to 15.1.0 and noticed my codebase failed to compile with an internal compiler error. I managed to simplify the code to just one template struct and a class with private constructor and friending the struct. The error only happens with --std=c++20 and later standards. Crash was also not specific to my custom gcc, but also reproducible on the gcc (15.1.1) available in Arch linux's repositories.

Output of -freport-bug is in an attachment.
Comment 1 Patrick Palka 2025-06-24 13:51:08 UTC
Confirmed, the ICE (with -std=c++20) started with r15-7260 "c++: constexpr VEC_INIT_EXPR [PR118285]", which was backported for 14.3.
Comment 2 GCC Commits 2025-07-31 18:23:18 UTC
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:f6462f664725844faa97ae7e8690e4fedee65788

commit r16-2691-gf6462f664725844faa97ae7e8690e4fedee65788
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jul 31 14:23:10 2025 -0400

    c++: constexpr, array, private ctor [PR120800]
    
    Here cxx_eval_vec_init_1 wants to recreate the default constructor call that
    we previously built and threw away in build_vec_init_elt, but we aren't in
    the same access context at this point.  Since we already checked access,
    let's just suppress access control here.
    
    Redoing overload resolution at constant evaluation time is sketchy, but
    should usually be fine for a default/copy constructor.
    
            PR c++/120800
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (cxx_eval_vec_init_1): Suppress access control.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/constexpr-array30.C: New test.
Comment 3 GCC Commits 2025-07-31 18:23:40 UTC
The releases/gcc-15 branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:df63b4a2f77acd6d10aad68fdfbf4d5b698dad5b

commit r15-10178-gdf63b4a2f77acd6d10aad68fdfbf4d5b698dad5b
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jul 31 14:23:31 2025 -0400

    c++: constexpr, array, private ctor [PR120800]
    
    Here cxx_eval_vec_init_1 wants to recreate the default constructor call that
    we previously built and threw away in build_vec_init_elt, but we aren't in
    the same access context at this point.  Since we already checked access,
    let's just suppress access control here.
    
    Redoing overload resolution at constant evaluation time is sketchy, but
    should usually be fine for a default/copy constructor.
    
            PR c++/120800
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (cxx_eval_vec_init_1): Suppress access control.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/constexpr-array30.C: New test.
    
    (cherry picked from commit f6462f664725844faa97ae7e8690e4fedee65788)
Comment 4 GCC Commits 2025-08-01 02:58:50 UTC
The releases/gcc-14 branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:000138675af3e1ee587b0fa17709ea62590860cb

commit r14-11928-g000138675af3e1ee587b0fa17709ea62590860cb
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jul 31 22:58:43 2025 -0400

    c++: constexpr, array, private ctor [PR120800]
    
    Here cxx_eval_vec_init_1 wants to recreate the default constructor call that
    we previously built and threw away in build_vec_init_elt, but we aren't in
    the same access context at this point.  Since we already checked access,
    let's just suppress access control here.
    
    Redoing overload resolution at constant evaluation time is sketchy, but
    should usually be fine for a default/copy constructor.
    
            PR c++/120800
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (cxx_eval_vec_init_1): Suppress access control.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/constexpr-array30.C: New test.
    
    (cherry picked from commit f6462f664725844faa97ae7e8690e4fedee65788)
Comment 5 Jason Merrill 2025-09-08 16:14:49 UTC
Fixed for 14.4/15.2/16.