Bug 71093 - use of pseudo-destructor accepted in constant expression
Summary: use of pseudo-destructor accepted in constant expression
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 14.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
: 102623 119082 (view as bug list)
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2016-05-12 23:09 UTC by Martin Sebor
Modified: 2025-03-02 03:32 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 5.3.0, 6.1.0, 7.0
Last reconfirmed: 2021-08-11 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2016-05-12 23:09:33 UTC
Among the list of expressions that are not core constant expressions and thus are not usable in constexpr contexts is the call to a pseudo-destructor such as in the following (invalid) test case.  The test case is silently accepted by all versions of GCC when it should be rejected.

$ cat xx.cpp && gcc -S -Wall -Wextra -Wpedantic -o/dev/null xx.cpp
constexpr int f (const int *p)
{
  typedef int T;
  p->~T ();
  return *p;
}

constexpr int i = 0;
constexpr int j = f (&i);
Comment 1 Drea Pinski 2016-07-10 20:11:41 UTC
Confirmed.
Comment 2 Martin Sebor 2017-02-16 00:48:51 UTC
Setting Status to Confirmed per comment #1.
Comment 3 Drea Pinski 2021-08-11 22:59:30 UTC
Note p->~T() is valid for C++20 but not exactly.
Comment 4 GCC Commits 2023-12-13 16:48:23 UTC
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:90bc2d09b5bfcc913f79543c3b65202e7246e162

commit r14-6508-g90bc2d09b5bfcc913f79543c3b65202e7246e162
Author: Nathaniel Shead <nathanieloshead@gmail.com>
Date:   Fri Nov 3 12:18:29 2023 +1100

    c++: End lifetime of objects in constexpr after destructor call [PR71093]
    
    This patch adds checks for using objects after they've been manually
    destroyed via explicit destructor call. Currently this is only
    implemented for 'top-level' objects; FIELD_DECLs and individual elements
    of arrays will need a lot more work to track correctly and are left for
    a future patch.
    
    The other limitation is that destruction of parameter objects is checked
    too 'early', happening at the end of the function call rather than the
    end of the owning full-expression as they should be for consistency;
    see cpp2a/constexpr-lifetime2.C. This is because I wasn't able to find a
    good way to link the constructed parameter declarations with the
    variable declarations that are actually destroyed later on to propagate
    their lifetime status, so I'm leaving this for a later patch.
    
            PR c++/71093
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (constexpr_global_ctx::get_value_ptr): Don't
            return NULL_TREE for objects we're initializing.
            (constexpr_global_ctx::destroy_value): Rename from remove_value.
            Only mark real variables as outside lifetime.
            (constexpr_global_ctx::clear_value): New function.
            (destroy_value_checked): New function.
            (cxx_eval_call_expression): Defer complaining about non-constant
            arg0 for operator delete. Use remove_value_safe.
            (cxx_fold_indirect_ref_1): Handle conversion to 'as base' type.
            (outside_lifetime_error): Include name of object we're
            accessing.
            (cxx_eval_store_expression): Handle clobbers. Improve error
            messages.
            (cxx_eval_constant_expression): Use remove_value_safe. Clear
            bind variables before entering body.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1y/constexpr-lifetime1.C: Improve error message.
            * g++.dg/cpp1y/constexpr-lifetime2.C: Likewise.
            * g++.dg/cpp1y/constexpr-lifetime3.C: Likewise.
            * g++.dg/cpp1y/constexpr-lifetime4.C: Likewise.
            * g++.dg/cpp2a/bitfield2.C: Likewise.
            * g++.dg/cpp2a/constexpr-new3.C: Likewise. New check.
            * g++.dg/cpp1y/constexpr-lifetime7.C: New test.
            * g++.dg/cpp2a/constexpr-lifetime1.C: New test.
            * g++.dg/cpp2a/constexpr-lifetime2.C: New test.
    
    Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Comment 5 Jonathan Wakely 2023-12-15 11:26:51 UTC
This causes new ICEs for the libstdc++ test suite, see PR 113031
Comment 6 Drea Pinski 2024-08-06 07:49:43 UTC
*** Bug 102623 has been marked as a duplicate of this bug. ***
Comment 7 Drea Pinski 2024-08-06 07:50:00 UTC
Fixed.
Comment 8 Drea Pinski 2025-03-02 03:32:46 UTC
*** Bug 119082 has been marked as a duplicate of this bug. ***