Bug 110463 - [13/14 Regression] Mutable subobject is usable in a constant expression
Summary: [13/14 Regression] Mutable subobject is usable in a constant expression
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 13.1.0
: P3 normal
Target Milestone: 13.2
Assignee: Patrick Palka
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2023-06-28 17:15 UTC by Fedor Chelnokov
Modified: 2023-07-01 18:06 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 12.3.0
Known to fail: 13.1.0, 14.0
Last reconfirmed: 2023-06-28 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fedor Chelnokov 2023-06-28 17:15:51 UTC
The following program is invalid per https://timsong-cpp.github.io/cppwp/n4861/expr.const#4.8 (An object or reference is usable in constant expressions if it is ... a non-mutable subobject)

struct U {
    mutable int x = 2;
};

int main() {
    constexpr U u{};
    u.x = 1;
    static_assert( u.x == 2 ); // must fail, but ok in GCC
}

but GCC accepts it. Online demo: https://gcc.godbolt.org/z/n8MYzhbad
Comment 1 Patrick Palka 2023-06-28 17:29:41 UTC
Confirmed, started with r13-2701-g7107ea6fb933f1
Comment 2 GCC Commits 2023-06-29 20:10:48 UTC
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

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

commit r14-2198-gfd8a1be04d4cdbfefea457b99ed8404d77b35dd6
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Jun 29 16:02:04 2023 -0400

    c++: unpropagated CONSTRUCTOR_MUTABLE_POISON [PR110463]
    
    Here we're incorrectly accepting the mutable member accesses because
    cp_fold neglects to propagate CONSTRUCTOR_MUTABLE_POISON when folding a
    CONSTRUCTOR.
    
            PR c++/110463
    
    gcc/cp/ChangeLog:
    
            * cp-gimplify.cc (cp_fold) <case CONSTRUCTOR>: Propagate
            CONSTRUCTOR_MUTABLE_POISON.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/constexpr-mutable6.C: New test.
Comment 3 GCC Commits 2023-07-01 01:28:30 UTC
The releases/gcc-13 branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:8f2cafc03f645748109291710157fdeceac32ee1

commit r13-7516-g8f2cafc03f645748109291710157fdeceac32ee1
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Jun 29 16:02:04 2023 -0400

    c++: unpropagated CONSTRUCTOR_MUTABLE_POISON [PR110463]
    
    Here we're incorrectly accepting the mutable member accesses because
    cp_fold neglects to propagate CONSTRUCTOR_MUTABLE_POISON when folding a
    CONSTRUCTOR.
    
            PR c++/110463
    
    gcc/cp/ChangeLog:
    
            * cp-gimplify.cc (cp_fold) <case CONSTRUCTOR>: Propagate
            CONSTRUCTOR_MUTABLE_POISON.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/constexpr-mutable6.C: New test.
    
    (cherry picked from commit fd8a1be04d4cdbfefea457b99ed8404d77b35dd6)
Comment 4 Patrick Palka 2023-07-01 01:29:19 UTC
Should be fixed for GCC 13.2, thanks for the bug report.
Comment 5 Fedor Chelnokov 2023-07-01 18:06:27 UTC
Thanks a lot for a very quick fix!